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

enh: make run() awaitable and bubble exceptions rather than process.exit(1) #429

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,11 @@ Plop.prepare({
configPath: path.join(__dirname, 'plopfile.js'),
preload: argv.preload || [],
completion: argv.completion
}, env => Plop.execute(env, run));
}, env => Plop.execute(env, env => run(env).catch( err => {
console.error(err.message);
process.exit(1);
}))
);
```

And your `package.json` should look like the following:
Expand Down Expand Up @@ -576,7 +580,10 @@ Plop.prepare({
...env,
dest: process.cwd() // this will make the destination path to be based on the cwd when calling the wrapper
}
return run(options, undefined, true)
run(options, undefined, true).catch( err => {
console.error(err.message);
process.exit(1);
})
})
)
```
Expand Down
5 changes: 4 additions & 1 deletion packages/plop/bin/plop.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ Plop.prepare(
completion: argv.completion,
},
function (env) {
Plop.execute(env, run);
Plop.execute(env, (env, argv) => run(env, argv).catch( err => {
console.error(chalk.red("[ERROR]"), err.message);
process.exit(1);
}));
},
);
35 changes: 10 additions & 25 deletions packages/plop/src/plop.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ async function run(env, _, passArgsBeforeDashes) {
chalk.red("[PLOP] ") + "Something went wrong with reading your plop file",
e,
);
return;
throw e;
}
const generators = plop.getGeneratorList();
const generatorNames = generators.map((v) => v.name);
Expand All @@ -73,44 +73,33 @@ async function run(env, _, passArgsBeforeDashes) {
const runGeneratorByName = (name) => {
const generator = plop.getGenerator(name);
const bypassData = combineBypassData(generator, bypassArr, plopArgV);
doThePlop(generator, bypassData);
return doThePlop(generator, bypassData);
};

// hmmmm, couldn't identify a generator in the user's input
if (!generators.length) {
// no generators?! there's clearly something wrong here
console.error(chalk.red("[PLOP] ") + "No generator found in plopfile");
process.exit(1);
throw new Error("No generator found in plopfile");
} else if (!generatorName && generators.length === 1) {
// only one generator in this plopfile... let's assume they
// want to run that one!
runGeneratorByName(generatorNames[0]);
await runGeneratorByName(generatorNames[0]);
} else if (!generatorName && generators.length > 1 && !bypassArr.length) {
// more than one generator? we'll have to ask the user which
// one they want to run.
out
await out
.chooseOptionFromList(generators, plop.getWelcomeMessage())
.then(runGeneratorByName)
.catch((err) => {
console.error(
chalk.red("[PLOP] ") +
"Something went wrong with selecting a generator",
err,
);
});
} else if (generatorNames.includes(generatorName)) {
// we have found the generator, run it!
runGeneratorByName(generatorName);
await runGeneratorByName(generatorName);
} else {
// we just can't make sense of your input... sorry :-(
const fuzzyGenName = (generatorName + " " + args.join(" ")).trim();
console.error(
chalk.red("[PLOP] ") +
'Could not find a generator for "' +
throw new Error('Could not find a generator for "' +
fuzzyGenName +
'"',
);
process.exit(1);
}
return plop;
}
Expand All @@ -120,7 +109,7 @@ async function run(env, _, passArgsBeforeDashes) {
//
function doThePlop(generator, bypassArr) {
let failedActions = false;
generator
return generator
.runPrompts(bypassArr)
.then(async (answers) => {
return answers;
Expand Down Expand Up @@ -161,15 +150,11 @@ function doThePlop(generator, bypassArr) {
progressSpinner.start();
return generator
.runActions(answers, { onSuccess, onFailure, onComment })
.then(() => {
.finally(() => {
progressSpinner.stop();
if (failedActions) process.exit(1);
if (failedActions) throw new Error(`At least one action has failed.`);
});
})
.catch(function (err) {
console.error(chalk.red("[ERROR]"), err.message);
process.exit(1);
});
}

export { Plop, run, progressSpinner };
Loading