Skip to content

Commit

Permalink
Merge pull request #300 from Pike/error-on-failure
Browse files Browse the repository at this point in the history
Return error code on failures on actions
  • Loading branch information
crutchcorn authored Dec 3, 2021
2 parents c6d0485 + bb04cb8 commit 26d61c9
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/plop.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ async function run(env, _, passArgsBeforeDashes) {
// everybody to the plop!
//
function doThePlop(generator, bypassArr) {
let failedActions = false;
generator
.runPrompts(bypassArr)
.then(async (answers) => {
Expand Down Expand Up @@ -156,12 +157,16 @@ function doThePlop(generator, bypassArr) {
line += ` ${errMsg}`;
}
progressSpinner.fail(line);
failedActions = true;
progressSpinner.start();
};
progressSpinner.start();
return generator
.runActions(answers, { onSuccess, onFailure, onComment })
.then(() => progressSpinner.stop());
.then(() => {
progressSpinner.stop();
if (failedActions) process.exit(1);
});
})
.catch(function (err) {
console.error(chalk.red("[ERROR]"), err.message);
Expand Down
22 changes: 22 additions & 0 deletions tests/action-failure.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { resolve, dirname } from "node:path";
import { waitFor } from "cli-testing-library";
import { renderPlop } from "./render.js";

import { fileURLToPath } from "node:url";

const __dirname = dirname(fileURLToPath(import.meta.url));

test("should exit with code 1 when failed actions", async () => {
const result = await renderPlop([], {
cwd: resolve(__dirname, "./examples/action-failure"),
});
const { findByText, userEvent } = result;
expect(await findByText("What is your name?")).toBeTruthy();
userEvent.keyboard("Joe");
expect(await findByText("Joe")).toBeTruthy();
userEvent.keyboard("[Enter]");
const actionOutput = await findByText("Action failed");
await waitFor(() =>
expect(actionOutput.hasExit()).toStrictEqual({ exitCode: 1 })
);
});
7 changes: 7 additions & 0 deletions tests/examples/action-failure/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "plop-example-action-failure",
"type": "module",
"engines": {
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
}
}
17 changes: 17 additions & 0 deletions tests/examples/action-failure/plopfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export default function (plop) {
plop.setGenerator("test", {
description: "this is a test",
prompts: [
{
type: "input",
name: "name",
message: "What is your name?",
},
],
actions: [
() => {
throw new Error("Action failed");
},
],
});
}

0 comments on commit 26d61c9

Please sign in to comment.