From 905d79dbbbbbc5c371b97f6ac91a35aa1afa3c52 Mon Sep 17 00:00:00 2001 From: prisis Date: Fri, 12 Apr 2024 23:13:39 +0200 Subject: [PATCH] feat(#297): added docs and helper command for the typescript support --- README.md | 5 ++-- packages/plop/src/console-out.js | 24 +++++++++++++------ packages/plop/src/input-processing.js | 4 ++-- packages/plop/src/plop.d.ts | 1 + .../input-processing.spec.js.snap | 1 + .../tests/examples/typescript/tsconfig.json | 2 +- 6 files changed, 25 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index e5c5b68..0e63e5c 100644 --- a/README.md +++ b/README.md @@ -140,9 +140,10 @@ Because [context switching is expensive](https://www.petrikainulainen.net/softwa # Plopfile API The plopfile api is the collection of methods that are exposed by the `plop` object. Most of the work is done by [`setGenerator`](#setgenerator) but this section documents the other methods that you may also find useful in your plopfile. -## TypeScript Declarations +## TypeScript Support -`plop` bundles TypeScript declarations. Whether or not you write your plopfile in TypeScript, many editors will offer code assistance via these declarations. +`plop` bundles TypeScript declarations and supports `plopfile.{c|m}ts` out of the box, run `plop --init-ts` to create a `plopfile.ts` file. +Whether or not you write your plopfile in TypeScript, many editors will offer code assistance via these declarations. ```javascript // plopfile.ts diff --git a/packages/plop/src/console-out.js b/packages/plop/src/console-out.js index 7999df6..fe1e54e 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,7 +90,7 @@ function displayHelpScreen() { ); } -function createInitPlopfile(force = false) { +function createInitPlopfile(force = false, useTypescript = false) { var initString = "export default function (plop) {\n\n" + "\tplop.setGenerator('basics', {\n" + @@ -99,15 +100,24 @@ function createInitPlopfile(force = false) { "\t});\n\n" + "};"; - if (fs.existsSync(process.cwd() + "/plopfile.js") && force === false) { - throw Error('"plopfile.js" already exists at this location.'); + if (useTypescript) { + initString = "import type { NodePlopAPI } from 'plop'\n" + + "\n" + + "export default function (plop: NodePlopAPI) {\n" + + "\n" + + "}\n" + + "\n"; } - if (fs.existsSync(process.cwd() + "/plopfile.cjs") && force === false) { - throw Error('"plopfile.cjs" already exists at this location.'); - } + [`js`, `cjs`, `ts`, `cts`, `mts`].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..d887d75 100644 --- a/packages/plop/src/input-processing.js +++ b/packages/plop/src/input-processing.js @@ -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/src/plop.d.ts b/packages/plop/src/plop.d.ts index 0e31399..95b8dd8 100644 --- a/packages/plop/src/plop.d.ts +++ b/packages/plop/src/plop.d.ts @@ -12,6 +12,7 @@ export { PlopGenerator, NodePlopAPI, PlopGeneratorConfig, + Actions } from "node-plop"; export const Plop: Liftoff; 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 diff --git a/packages/plop/tests/examples/typescript/tsconfig.json b/packages/plop/tests/examples/typescript/tsconfig.json index dac20cc..97a51cf 100644 --- a/packages/plop/tests/examples/typescript/tsconfig.json +++ b/packages/plop/tests/examples/typescript/tsconfig.json @@ -8,7 +8,7 @@ "strict": true, "baseUrl": "./", "paths": { - "plop": ["../../src/plop.d.ts"] + "plop": ["../../../src/plop.d.ts"] } } }