diff --git a/README.md b/README.md index 1845fda..65aae9f 100644 --- a/README.md +++ b/README.md @@ -134,7 +134,7 @@ By default Plop actions keep your files safe by failing when things look fishy. Plop bundles TypeScript declarations and supports TypeScript plopfiles via [tsx loaders](https://github.com/privatenumber/tsx?tab=readme-ov-file#nodejs-loader), a feature of [NodeJS command line imports](https://nodejs.org/api/cli.html#--importmodule). -First, make a TypesScript plopfile: +First, make a TypesScript plopfile using `plop --init-ts` or by hand: ```ts // plopfile.ts diff --git a/packages/plop/src/console-out.js b/packages/plop/src/console-out.js index 7999df6..8b22dbe 100644 --- a/packages/plop/src/console-out.js +++ b/packages/plop/src/console-out.js @@ -62,6 +62,7 @@ function displayHelpScreen() { " -t, --show-type-names " + chalk.dim("Show type names instead of abbreviations"), " -i, --init " + chalk.dim("Generate a basic plopfile.js"), + " --init-ts " + chalk.dim("Generate a basic plopfile.ts"), " -v, --version " + chalk.dim("Print current version"), " -f, --force " + chalk.dim("Run the generator forcefully"), "", @@ -89,25 +90,39 @@ function displayHelpScreen() { ); } -function createInitPlopfile(force = false) { - var initString = - "export default function (plop) {\n\n" + - "\tplop.setGenerator('basics', {\n" + - "\t\tdescription: 'this is a skeleton plopfile',\n" + - "\t\tprompts: [],\n" + - "\t\tactions: []\n" + - "\t});\n\n" + - "};"; +function createInitPlopfile(force = false, useTypescript = false) { + var initString = (() => { + if (useTypescript) { + return ( + "import type { NodePlopAPI } from 'plop'\n" + + "\n" + + "export default async function (plop: NodePlopAPI) {\n" + + "\n" + + "}\n" + + "\n" + ); + } else { + return ( + "export default function (plop) {\n\n" + + "\tplop.setGenerator('basics', {\n" + + "\t\tdescription: 'this is a skeleton plopfile',\n" + + "\t\tprompts: [],\n" + + "\t\tactions: []\n" + + "\t});\n\n" + + "};" + ); + } + })(); - if (fs.existsSync(process.cwd() + "/plopfile.js") && force === false) { - throw Error('"plopfile.js" already exists at this location.'); - } - - if (fs.existsSync(process.cwd() + "/plopfile.cjs") && force === false) { - throw Error('"plopfile.cjs" already exists at this location.'); - } + [`js`, `cjs`, `ts`].forEach((ext) => { + const name = `plopfile.${ext}`; + if (fs.existsSync(process.cwd() + `/${name}`) && force === false) { + throw Error(`"${name}" already exists at this location.`); + } + }); - fs.writeFileSync(process.cwd() + "/plopfile.js", initString); + const outExt = useTypescript ? `ts` : `js`; + fs.writeFileSync(process.cwd() + `/plopfile.${outExt}`, initString); } const typeDisplay = { diff --git a/packages/plop/src/input-processing.js b/packages/plop/src/input-processing.js index 94189b9..3808523 100644 --- a/packages/plop/src/input-processing.js +++ b/packages/plop/src/input-processing.js @@ -18,11 +18,11 @@ function getBypassAndGenerator(plop, passArgsBeforeDashes) { const { plopArgV, eoaArg } = passArgsBeforeDashes ? { plopArgV: argv } : eoaIndex === -1 - ? { plopArgV: [] } - : { - plopArgV: minimist(args.slice(eoaIndex + 1, args.length)), - eoaArg: args[eoaIndex + 1], - }; + ? { plopArgV: [] } + : { + plopArgV: minimist(args.slice(eoaIndex + 1, args.length)), + eoaArg: args[eoaIndex + 1], + }; // locate the generator name based on input and take the rest of the // user's input as prompt bypass data to be passed into the generator @@ -70,10 +70,10 @@ function handleArgFlags(env) { } // handle request for initializing a new plopfile - if (argv.init || argv.i) { + if (argv.init || argv.i || argv[`init-ts`]) { const force = argv.force === true || argv.f === true || false; try { - out.createInitPlopfile(force); + out.createInitPlopfile(force, !!argv[`init-ts`]); process.exit(0); } catch (err) { console.error(chalk.red("[PLOP] ") + err.message); diff --git a/packages/plop/tests/__snapshots__/input-processing.spec.js.snap b/packages/plop/tests/__snapshots__/input-processing.spec.js.snap index 4342dda..e3bd294 100644 --- a/packages/plop/tests/__snapshots__/input-processing.spec.js.snap +++ b/packages/plop/tests/__snapshots__/input-processing.spec.js.snap @@ -11,6 +11,7 @@ Options: -h, --help Show this help display -t, --show-type-names Show type names instead of abbreviations -i, --init Generate a basic plopfile.js + --init-ts Generate a basic plopfile.ts -v, --version Print current version -f, --force Run the generator forcefully