Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
benallfree committed Apr 18, 2024
1 parent e7482dc commit 5eb75c3
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 25 deletions.
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

0 comments on commit 5eb75c3

Please sign in to comment.