Skip to content

Commit b6c1bb5

Browse files
authored
Merge pull request #434 from benallfree/feat/init-ts
feat: --init-ts
2 parents e7482dc + 5eb75c3 commit b6c1bb5

File tree

4 files changed

+41
-25
lines changed

4 files changed

+41
-25
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ By default Plop actions keep your files safe by failing when things look fishy.
134134

135135
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).
136136

137-
First, make a TypesScript plopfile:
137+
First, make a TypesScript plopfile using `plop --init-ts` or by hand:
138138

139139
```ts
140140
// plopfile.ts

packages/plop/src/console-out.js

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ function displayHelpScreen() {
6262
" -t, --show-type-names " +
6363
chalk.dim("Show type names instead of abbreviations"),
6464
" -i, --init " + chalk.dim("Generate a basic plopfile.js"),
65+
" --init-ts " + chalk.dim("Generate a basic plopfile.ts"),
6566
" -v, --version " + chalk.dim("Print current version"),
6667
" -f, --force " + chalk.dim("Run the generator forcefully"),
6768
"",
@@ -89,25 +90,39 @@ function displayHelpScreen() {
8990
);
9091
}
9192

92-
function createInitPlopfile(force = false) {
93-
var initString =
94-
"export default function (plop) {\n\n" +
95-
"\tplop.setGenerator('basics', {\n" +
96-
"\t\tdescription: 'this is a skeleton plopfile',\n" +
97-
"\t\tprompts: [],\n" +
98-
"\t\tactions: []\n" +
99-
"\t});\n\n" +
100-
"};";
93+
function createInitPlopfile(force = false, useTypescript = false) {
94+
var initString = (() => {
95+
if (useTypescript) {
96+
return (
97+
"import type { NodePlopAPI } from 'plop'\n" +
98+
"\n" +
99+
"export default async function (plop: NodePlopAPI) {\n" +
100+
"\n" +
101+
"}\n" +
102+
"\n"
103+
);
104+
} else {
105+
return (
106+
"export default function (plop) {\n\n" +
107+
"\tplop.setGenerator('basics', {\n" +
108+
"\t\tdescription: 'this is a skeleton plopfile',\n" +
109+
"\t\tprompts: [],\n" +
110+
"\t\tactions: []\n" +
111+
"\t});\n\n" +
112+
"};"
113+
);
114+
}
115+
})();
101116

102-
if (fs.existsSync(process.cwd() + "/plopfile.js") && force === false) {
103-
throw Error('"plopfile.js" already exists at this location.');
104-
}
105-
106-
if (fs.existsSync(process.cwd() + "/plopfile.cjs") && force === false) {
107-
throw Error('"plopfile.cjs" already exists at this location.');
108-
}
117+
[`js`, `cjs`, `ts`].forEach((ext) => {
118+
const name = `plopfile.${ext}`;
119+
if (fs.existsSync(process.cwd() + `/${name}`) && force === false) {
120+
throw Error(`"${name}" already exists at this location.`);
121+
}
122+
});
109123

110-
fs.writeFileSync(process.cwd() + "/plopfile.js", initString);
124+
const outExt = useTypescript ? `ts` : `js`;
125+
fs.writeFileSync(process.cwd() + `/plopfile.${outExt}`, initString);
111126
}
112127

113128
const typeDisplay = {

packages/plop/src/input-processing.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ function getBypassAndGenerator(plop, passArgsBeforeDashes) {
1818
const { plopArgV, eoaArg } = passArgsBeforeDashes
1919
? { plopArgV: argv }
2020
: eoaIndex === -1
21-
? { plopArgV: [] }
22-
: {
23-
plopArgV: minimist(args.slice(eoaIndex + 1, args.length)),
24-
eoaArg: args[eoaIndex + 1],
25-
};
21+
? { plopArgV: [] }
22+
: {
23+
plopArgV: minimist(args.slice(eoaIndex + 1, args.length)),
24+
eoaArg: args[eoaIndex + 1],
25+
};
2626

2727
// locate the generator name based on input and take the rest of the
2828
// user's input as prompt bypass data to be passed into the generator
@@ -70,10 +70,10 @@ function handleArgFlags(env) {
7070
}
7171

7272
// handle request for initializing a new plopfile
73-
if (argv.init || argv.i) {
73+
if (argv.init || argv.i || argv[`init-ts`]) {
7474
const force = argv.force === true || argv.f === true || false;
7575
try {
76-
out.createInitPlopfile(force);
76+
out.createInitPlopfile(force, !!argv[`init-ts`]);
7777
process.exit(0);
7878
} catch (err) {
7979
console.error(chalk.red("[PLOP] ") + err.message);

packages/plop/tests/__snapshots__/input-processing.spec.js.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Options:
1111
-h, --help Show this help display
1212
-t, --show-type-names Show type names instead of abbreviations
1313
-i, --init Generate a basic plopfile.js
14+
--init-ts Generate a basic plopfile.ts
1415
-v, --version Print current version
1516
-f, --force Run the generator forcefully
1617

0 commit comments

Comments
 (0)