Skip to content

Commit

Permalink
feat(plopjs#297): added docs and helper command for the typescript su…
Browse files Browse the repository at this point in the history
…pport
  • Loading branch information
prisis committed Apr 12, 2024
1 parent 894d91b commit 905d79d
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 12 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 17 additions & 7 deletions packages/plop/src/console-out.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
"",
Expand Down Expand Up @@ -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" +
Expand All @@ -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 = {
Expand Down
4 changes: 2 additions & 2 deletions packages/plop/src/input-processing.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions packages/plop/src/plop.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export {
PlopGenerator,
NodePlopAPI,
PlopGeneratorConfig,
Actions
} from "node-plop";

export const Plop: Liftoff;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/plop/tests/examples/typescript/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"strict": true,
"baseUrl": "./",
"paths": {
"plop": ["../../src/plop.d.ts"]
"plop": ["../../../src/plop.d.ts"]
}
}
}

0 comments on commit 905d79d

Please sign in to comment.