Skip to content

Commit 35cb78e

Browse files
committed
wip
1 parent b797aaf commit 35cb78e

File tree

5 files changed

+51
-9
lines changed

5 files changed

+51
-9
lines changed

packages/node-plop/src/generator-runner.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export default function (plopfileApi, flags) {
4242
onSuccess = noop, // runs after each successful action
4343
onFailure = noop, // runs after each failed action
4444
onComment = noop, // runs for each comment line in the actions array
45+
onProgress = noop, // runs when custom actions update progress text
4546
} = hooks;
4647
const changes = []; // array of changed made by the actions
4748
const failures = []; // array of actions that failed
@@ -53,7 +54,7 @@ export default function (plopfileApi, flags) {
5354

5455
// if action is a function, run it to get our array of actions
5556
if (typeof actions === "function") {
56-
actions = actions(data);
57+
actions = await actions(data);
5758
}
5859

5960
// if actions are not defined... we cannot proceed.
@@ -77,7 +78,7 @@ export default function (plopfileApi, flags) {
7778
}
7879

7980
const actionIsFunction = typeof action === "function";
80-
const actionCfg = actionIsFunction ? { type: "function" } : action;
81+
const actionCfg = actionIsFunction ? { type: "function", onComment, onProgress } : action;
8182
const actionLogic = actionIsFunction
8283
? action
8384
: actionTypes[actionCfg.type];

packages/node-plop/tests/lifecycle-hooks/lifecycle-hooks.spec.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,21 +55,32 @@ describe("lifecycle-hooks", function () {
5555
expect(onFailure.called).toBe(0);
5656
});
5757

58-
test("Lifecycle hook test (onComment)", async function () {
58+
test("Lifecycle hook test (onComment/onProgress)", async function () {
5959
const plop = await nodePlop();
6060
const onSuccess = () => onSuccess.called++;
6161
onSuccess.called = 0;
6262
const onFailure = () => onFailure.called++;
6363
onFailure.called = 0;
6464
const onComment = () => onComment.called++;
6565
onComment.called = 0;
66+
const onProgress = () => onProgress.called++;
67+
onProgress.called = 0;
6668

6769
await plop
68-
.setGenerator("", { actions: ["yes", () => "yes", errAction, "yes"] })
69-
.runActions({}, { onSuccess, onFailure, onComment });
70+
.setGenerator("", { actions: [
71+
"yes",
72+
() => "yes",
73+
(_, { onProgress } )=>{
74+
onProgress(`progress`)
75+
return "yes"
76+
},
77+
errAction, "yes"]
78+
})
79+
.runActions({}, { onSuccess, onFailure, onComment, onProgress });
7080

71-
expect(onSuccess.called).toBe(1);
81+
expect(onSuccess.called).toBe(2);
7282
expect(onFailure.called).toBe(1);
7383
expect(onComment.called).toBe(1);
84+
expect(onProgress.called).toBe(1);
7485
});
7586
});

packages/node-plop/types/index.d.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ interface PlopActionHooksChanges {
119119
}
120120

121121
interface PlopActionHooks {
122+
onProgress?: (msg: string) => void;
122123
onComment?: (msg: string) => void;
123124
onSuccess?: (change: PlopActionHooksChanges) => void;
124125
onFailure?: (failure: PlopActionHooksFailures) => void;
@@ -192,9 +193,25 @@ export interface CustomActionConfig<TypeString extends string>
192193
[key: string]: any;
193194
}
194195

196+
export interface CustomActionConfigMaterialized<TypeString extends string> extends CustomActionConfig<TypeString> {
197+
/**
198+
* Output a comment
199+
* @param msg
200+
* @returns
201+
*/
202+
onComment: (msg: string)=>void
203+
204+
/**
205+
* Output a progess update on the same line as the active spinner
206+
* @param msg
207+
* @returns
208+
*/
209+
onProgress: (msg: string)=>void
210+
}
211+
195212
export type CustomActionFunction = (
196213
answers: Answers,
197-
config: CustomActionConfig<string>,
214+
config: CustomActionConfigMaterialized<string>,
198215
plopfileApi: NodePlopAPI,
199216
) => Promise<string> | string;
200217

packages/node-plop/types/test.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,17 @@ generator.runPrompts(["test"]).then((answers) => {
3131
const onComment = (): void => {
3232
console.log("Start");
3333
};
34+
const onProgress = (): void => {
35+
console.log("Progress");
36+
};
3437
const onSuccess = (): void => {
3538
console.log("This worked!");
3639
};
3740
const onFailure = (): void => {
3841
console.log("Failed");
3942
};
4043
return generator
41-
.runActions(answers, { onSuccess, onFailure, onComment })
44+
.runActions(answers, { onSuccess, onFailure, onComment, onProgress })
4245
.then(() => {
4346
console.log("Done");
4447
});
@@ -348,6 +351,9 @@ _ = async () => {
348351
onComment: (msg) => {
349352
console.log(msg);
350353
},
354+
onProgress: (msg) => {
355+
console.log(msg);
356+
}
351357
})
352358
.then(() => {
353359
console.log("Test");

packages/plop/src/plop.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,13 @@ function doThePlop(generator, bypassArr) {
127127
})
128128
.then((answers) => {
129129
const noMap = argv["show-type-names"] || argv.t;
130+
const onProgress = (msg) => {
131+
if(argv.progress) {
132+
progressSpinner.text = msg
133+
} else {
134+
console.log(msg)
135+
}
136+
}
130137
const onComment = (msg) => {
131138
progressSpinner.info(msg);
132139
progressSpinner.start();
@@ -160,7 +167,7 @@ function doThePlop(generator, bypassArr) {
160167
};
161168
progressSpinner.start();
162169
return generator
163-
.runActions(answers, { onSuccess, onFailure, onComment })
170+
.runActions(answers, { onSuccess, onFailure, onComment, onProgress })
164171
.then(() => {
165172
progressSpinner.stop();
166173
if (failedActions) process.exit(1);

0 commit comments

Comments
 (0)