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: add onProgress hook #430

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
3 changes: 2 additions & 1 deletion packages/node-plop/src/generator-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export default function (plopfileApi, flags) {
onSuccess = noop, // runs after each successful action
onFailure = noop, // runs after each failed action
onComment = noop, // runs for each comment line in the actions array
onProgress = noop, // runs when custom actions update progress text
} = hooks;
const changes = []; // array of changed made by the actions
const failures = []; // array of actions that failed
Expand Down Expand Up @@ -77,7 +78,7 @@ export default function (plopfileApi, flags) {
}

const actionIsFunction = typeof action === "function";
const actionCfg = actionIsFunction ? { type: "function" } : action;
const actionCfg = actionIsFunction ? { type: "function", onComment, onProgress } : action;
const actionLogic = actionIsFunction
? action
: actionTypes[actionCfg.type];
Expand Down
19 changes: 15 additions & 4 deletions packages/node-plop/tests/lifecycle-hooks/lifecycle-hooks.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,32 @@ describe("lifecycle-hooks", function () {
expect(onFailure.called).toBe(0);
});

test("Lifecycle hook test (onComment)", async function () {
test("Lifecycle hook test (onComment/onProgress)", async function () {
const plop = await nodePlop();
const onSuccess = () => onSuccess.called++;
onSuccess.called = 0;
const onFailure = () => onFailure.called++;
onFailure.called = 0;
const onComment = () => onComment.called++;
onComment.called = 0;
const onProgress = () => onProgress.called++;
onProgress.called = 0;

await plop
.setGenerator("", { actions: ["yes", () => "yes", errAction, "yes"] })
.runActions({}, { onSuccess, onFailure, onComment });
.setGenerator("", { actions: [
"yes",
() => "yes",
(_, { onProgress } )=>{
onProgress(`progress`)
return "yes"
},
errAction, "yes"]
})
.runActions({}, { onSuccess, onFailure, onComment, onProgress });

expect(onSuccess.called).toBe(1);
expect(onSuccess.called).toBe(2);
expect(onFailure.called).toBe(1);
expect(onComment.called).toBe(1);
expect(onProgress.called).toBe(1);
});
});
19 changes: 18 additions & 1 deletion packages/node-plop/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ interface PlopActionHooksChanges {
}

interface PlopActionHooks {
onProgress?: (msg: string) => void;
onComment?: (msg: string) => void;
onSuccess?: (change: PlopActionHooksChanges) => void;
onFailure?: (failure: PlopActionHooksFailures) => void;
Expand Down Expand Up @@ -192,9 +193,25 @@ export interface CustomActionConfig<TypeString extends string>
[key: string]: any;
}

export interface CustomActionConfigMaterialized<TypeString extends string> extends CustomActionConfig<TypeString> {
/**
* Output a comment
* @param msg
* @returns
*/
onComment: (msg: string)=>void

/**
* Output a progess update on the same line as the active spinner
* @param msg
* @returns
*/
onProgress: (msg: string)=>void
}

export type CustomActionFunction = (
answers: Answers,
config: CustomActionConfig<string>,
config: CustomActionConfigMaterialized<string>,
plopfileApi: NodePlopAPI,
) => Promise<string> | string;

Expand Down
8 changes: 7 additions & 1 deletion packages/node-plop/types/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,17 @@ generator.runPrompts(["test"]).then((answers) => {
const onComment = (): void => {
console.log("Start");
};
const onProgress = (): void => {
// console.log("Progress");
};
const onSuccess = (): void => {
console.log("This worked!");
};
const onFailure = (): void => {
console.log("Failed");
};
return generator
.runActions(answers, { onSuccess, onFailure, onComment })
.runActions(answers, { onSuccess, onFailure, onComment, onProgress })
.then(() => {
console.log("Done");
});
Expand Down Expand Up @@ -348,6 +351,9 @@ _ = async () => {
onComment: (msg) => {
console.log(msg);
},
onProgress: (msg) => {
console.log(msg);
},
})
.then(() => {
console.log("Test");
Expand Down
3 changes: 2 additions & 1 deletion packages/node-plop/types/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"moduleResolution": "Node"
}
},
"include": ["*.ts"]
}
9 changes: 8 additions & 1 deletion packages/plop/src/plop.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,13 @@ function doThePlop(generator, bypassArr) {
})
.then((answers) => {
const noMap = argv["show-type-names"] || argv.t;
const onProgress = (msg) => {
if(argv.progress!==false) {
progressSpinner.text = msg
} else {
console.log(msg)
}
}
const onComment = (msg) => {
progressSpinner.info(msg);
progressSpinner.start();
Expand Down Expand Up @@ -160,7 +167,7 @@ function doThePlop(generator, bypassArr) {
};
progressSpinner.start();
return generator
.runActions(answers, { onSuccess, onFailure, onComment })
.runActions(answers, { onSuccess, onFailure, onComment, onProgress })
.then(() => {
progressSpinner.stop();
if (failedActions) process.exit(1);
Expand Down
Loading