Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: --init-ts #434

Merged
merged 1 commit into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
49 changes: 32 additions & 17 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,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 = {
Expand Down
14 changes: 7 additions & 7 deletions packages/plop/src/input-processing.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down 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
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
Loading