@biorate/hygen 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/src/engine.js +209 -54
- package/dist/cjs/src/errors.js +16 -1
- package/dist/cjs/src/prompt.js +101 -25
- package/dist/cjs/src/render.js +4 -0
- package/dist/esm/src/engine.js +210 -55
- package/dist/esm/src/errors.js +13 -0
- package/dist/esm/src/prompt.js +98 -24
- package/dist/esm/src/render.js +4 -0
- package/dist/types/src/errors.d.ts +6 -0
- package/dist/types/src/prompt.d.ts +18 -2
- package/dist/types/src/types.d.ts +27 -0
- package/package.json +2 -2
package/dist/cjs/src/engine.js
CHANGED
|
@@ -29,6 +29,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
29
29
|
exports.KeepGoingError = exports.ShowHelpError = exports.engine = void 0;
|
|
30
30
|
const path_1 = __importDefault(require("path"));
|
|
31
31
|
const fs_extra_1 = __importDefault(require("fs-extra"));
|
|
32
|
+
const context_1 = __importDefault(require("./context"));
|
|
32
33
|
const params_1 = __importDefault(require("./params"));
|
|
33
34
|
const prompt_1 = __importStar(require("./prompt"));
|
|
34
35
|
const errors_1 = require("./errors");
|
|
@@ -40,6 +41,7 @@ const isInteractive = ({ interactive }) => interactive === true
|
|
|
40
41
|
: interactive === false
|
|
41
42
|
? false
|
|
42
43
|
: Boolean(process.stdin.isTTY && process.stdout.isTTY);
|
|
44
|
+
const RUN_SCOPE = '*';
|
|
43
45
|
const engine = async (argv, config) => {
|
|
44
46
|
const { cwd, templates, logger } = config;
|
|
45
47
|
let base = Object.assign(await (0, params_1.default)(config, argv), { cwd });
|
|
@@ -67,23 +69,69 @@ const engine = async (argv, config) => {
|
|
|
67
69
|
logger.log(`Loaded templates: ${templates.replace(`${cwd}/`, '')}`);
|
|
68
70
|
const nameList = names ?? [undefined];
|
|
69
71
|
const keepGoing = Boolean(cleanArgv['keep-going']);
|
|
72
|
+
const singleRunAll = nameList.length === 1 && actions.length === 1;
|
|
70
73
|
const failures = [];
|
|
71
74
|
const results = [];
|
|
72
75
|
const statuses = [];
|
|
73
76
|
const execute = (await Promise.resolve().then(() => __importStar(require('./execute')))).execute;
|
|
74
77
|
const render = (await Promise.resolve().then(() => __importStar(require('./render')))).render;
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
78
|
+
const projectHooks = (0, prompt_1.normalizeProjectHooks)(config.hooks, logger);
|
|
79
|
+
const hookHelpers = (0, context_1.default)({}, config).h;
|
|
80
|
+
const hookCache = new Map();
|
|
81
|
+
const actionStates = new Map();
|
|
82
|
+
const makeCtx = (action, name, subaction, args) => ({
|
|
83
|
+
cwd,
|
|
84
|
+
templates: base.templates,
|
|
85
|
+
generator: generator,
|
|
86
|
+
action,
|
|
87
|
+
...(subaction !== undefined ? { subaction } : {}),
|
|
88
|
+
...(name !== undefined ? { name } : {}),
|
|
89
|
+
args,
|
|
90
|
+
h: hookHelpers,
|
|
91
|
+
logger,
|
|
92
|
+
dry: Boolean(base.dry),
|
|
93
|
+
});
|
|
94
|
+
const actionState = (action, subaction) => {
|
|
95
|
+
let state = actionStates.get(action);
|
|
96
|
+
if (!state) {
|
|
97
|
+
state = { started: false, hooks: {}, subaction, results: [] };
|
|
98
|
+
actionStates.set(action, state);
|
|
99
|
+
}
|
|
100
|
+
return state;
|
|
101
|
+
};
|
|
102
|
+
const reportPairError = (error, pair, pairLabel) => {
|
|
103
|
+
if (singleRunAll)
|
|
104
|
+
throw error;
|
|
105
|
+
if (!keepGoing)
|
|
106
|
+
throw new errors_1.ShowHelpError(`${pairLabel} failed: ${error.message}`);
|
|
107
|
+
failures.push({ ...pair, error: error.message });
|
|
108
|
+
statuses.push({ ...pair, status: 'failed' });
|
|
109
|
+
};
|
|
110
|
+
let projectBeforeAllError;
|
|
111
|
+
try {
|
|
112
|
+
await projectHooks.beforeAll?.(makeCtx(RUN_SCOPE, undefined, undefined, { ...cleanArgv }));
|
|
113
|
+
}
|
|
114
|
+
catch (err) {
|
|
115
|
+
projectBeforeAllError = err;
|
|
116
|
+
}
|
|
117
|
+
let pendingError;
|
|
118
|
+
const afterAllErrors = [];
|
|
119
|
+
try {
|
|
120
|
+
for (const name of nameList) {
|
|
121
|
+
for (const action of actions) {
|
|
122
|
+
const [mainAction, subaction] = action.split(':');
|
|
123
|
+
const actionfolder = path_1.default.join(base.templates, generator, mainAction);
|
|
124
|
+
const pairLabel = `action '${action}'${name !== undefined ? ` for name '${name}'` : ''}`;
|
|
125
|
+
const pair = {
|
|
126
|
+
action,
|
|
127
|
+
...(name !== undefined ? { name } : {}),
|
|
128
|
+
};
|
|
129
|
+
const pairStatus = {
|
|
130
|
+
action,
|
|
131
|
+
...(name !== undefined ? { name } : {}),
|
|
132
|
+
};
|
|
133
|
+
if (!(await fs_extra_1.default.pathExists(actionfolder))) {
|
|
134
|
+
const err = new errors_1.ShowHelpError(`I can't find action '${action}' for generator '${generator}'.
|
|
87
135
|
|
|
88
136
|
You can try:
|
|
89
137
|
1. 'hygen init self' to initialize your project, and
|
|
@@ -91,55 +139,162 @@ const engine = async (argv, config) => {
|
|
|
91
139
|
|
|
92
140
|
Check out the quickstart for more: http://www.hygen.io/docs/quick-start
|
|
93
141
|
`);
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
142
|
+
if (singleRunAll)
|
|
143
|
+
throw err;
|
|
144
|
+
if (!keepGoing)
|
|
145
|
+
throw err;
|
|
146
|
+
failures.push({ ...pair, error: err.message });
|
|
147
|
+
statuses.push({ ...pairStatus, status: 'failed' });
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
if (projectBeforeAllError) {
|
|
151
|
+
reportPairError(projectBeforeAllError, pair, pairLabel);
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
const state = actionState(action, subaction);
|
|
155
|
+
if (!state.started) {
|
|
156
|
+
state.started = true;
|
|
157
|
+
try {
|
|
158
|
+
state.hooks = (await (0, prompt_1.resolveActionHooks)(actionfolder, hookCache)).instance;
|
|
159
|
+
await state.hooks.beforeAll?.(makeCtx(action, undefined, subaction, { ...cleanArgv }));
|
|
160
|
+
}
|
|
161
|
+
catch (err) {
|
|
162
|
+
state.beforeAllError = err;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
if (state.beforeAllError) {
|
|
166
|
+
reportPairError(state.beforeAllError, pair, pairLabel);
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
const hookArgs = {
|
|
108
170
|
...(name ? { name } : {}),
|
|
109
171
|
...cleanArgv,
|
|
110
|
-
}
|
|
111
|
-
const
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
172
|
+
};
|
|
173
|
+
const pairResults = [];
|
|
174
|
+
const afterErrors = [];
|
|
175
|
+
let pairStatuses = [];
|
|
176
|
+
let pairError;
|
|
177
|
+
for (const run of [
|
|
178
|
+
projectHooks.beforeEach
|
|
179
|
+
? () => projectHooks.beforeEach(makeCtx(action, name, subaction, hookArgs))
|
|
180
|
+
: undefined,
|
|
181
|
+
state.hooks.beforeEach
|
|
182
|
+
? () => state.hooks.beforeEach(makeCtx(action, name, subaction, hookArgs))
|
|
183
|
+
: undefined,
|
|
184
|
+
]) {
|
|
185
|
+
if (!run)
|
|
186
|
+
continue;
|
|
187
|
+
try {
|
|
188
|
+
await run();
|
|
189
|
+
}
|
|
190
|
+
catch (err) {
|
|
191
|
+
pairError = err;
|
|
192
|
+
break;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
if (!pairError) {
|
|
196
|
+
try {
|
|
197
|
+
const promptArgs = await (0, prompt_1.default)(config.createPrompter, actionfolder, hookArgs, {
|
|
198
|
+
h: hookHelpers,
|
|
199
|
+
cache: hookCache,
|
|
200
|
+
});
|
|
201
|
+
const args = Object.assign({
|
|
202
|
+
templates: base.templates,
|
|
203
|
+
actionfolder,
|
|
204
|
+
generator,
|
|
205
|
+
action,
|
|
206
|
+
subaction,
|
|
207
|
+
}, { _: base._, ts: base.ts }, cleanArgv, name ? { name } : {}, promptArgs);
|
|
208
|
+
const el = await execute(await render(args, config), args, config);
|
|
209
|
+
for (const pairResult of el.results) {
|
|
210
|
+
const annotated = {
|
|
211
|
+
...pairResult,
|
|
212
|
+
action,
|
|
213
|
+
...(name !== undefined ? { name } : {}),
|
|
214
|
+
};
|
|
215
|
+
results.push(annotated);
|
|
216
|
+
pairResults.push(annotated);
|
|
217
|
+
}
|
|
218
|
+
pairStatuses = el.statuses;
|
|
219
|
+
}
|
|
220
|
+
catch (err) {
|
|
221
|
+
pairError = err;
|
|
222
|
+
}
|
|
125
223
|
}
|
|
126
|
-
|
|
224
|
+
for (const run of [
|
|
225
|
+
state.hooks.afterEach
|
|
226
|
+
? () => state.hooks.afterEach(makeCtx(action, name, subaction, hookArgs), pairResults)
|
|
227
|
+
: undefined,
|
|
228
|
+
projectHooks.afterEach
|
|
229
|
+
? () => projectHooks.afterEach(makeCtx(action, name, subaction, hookArgs), pairResults)
|
|
230
|
+
: undefined,
|
|
231
|
+
]) {
|
|
232
|
+
if (!run)
|
|
233
|
+
continue;
|
|
234
|
+
try {
|
|
235
|
+
await run();
|
|
236
|
+
}
|
|
237
|
+
catch (err) {
|
|
238
|
+
afterErrors.push(err);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
state.results.push(...pairResults);
|
|
242
|
+
if (pairError) {
|
|
243
|
+
reportPairError(pairError, pair, pairLabel);
|
|
244
|
+
if (keepGoing) {
|
|
245
|
+
for (const err of afterErrors)
|
|
246
|
+
failures.push({ ...pair, error: err.message });
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
else if (afterErrors.length > 0) {
|
|
250
|
+
statuses.push(...pairStatuses);
|
|
251
|
+
reportPairError(afterErrors[0], pair, pairLabel);
|
|
252
|
+
if (keepGoing) {
|
|
253
|
+
for (const err of afterErrors.slice(1))
|
|
254
|
+
failures.push({ ...pair, error: err.message });
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
else {
|
|
258
|
+
statuses.push(...pairStatuses);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
catch (err) {
|
|
264
|
+
pendingError = err;
|
|
265
|
+
}
|
|
266
|
+
finally {
|
|
267
|
+
for (const [action, state] of actionStates) {
|
|
268
|
+
if (!state.started)
|
|
269
|
+
continue;
|
|
270
|
+
try {
|
|
271
|
+
await state.hooks.afterAll?.(makeCtx(action, undefined, state.subaction, { ...cleanArgv }), state.results);
|
|
127
272
|
}
|
|
128
273
|
catch (err) {
|
|
129
|
-
|
|
130
|
-
if (singleRun)
|
|
131
|
-
throw err;
|
|
132
|
-
if (!keepGoing) {
|
|
133
|
-
throw new errors_1.ShowHelpError(`${pairLabel} failed: ${e.message}`);
|
|
134
|
-
}
|
|
135
|
-
failures.push({
|
|
136
|
-
action,
|
|
137
|
-
...(name !== undefined ? { name } : {}),
|
|
138
|
-
error: e.message,
|
|
139
|
-
});
|
|
140
|
-
statuses.push({ ...statusPair, status: 'failed' });
|
|
274
|
+
afterAllErrors.push(err);
|
|
141
275
|
}
|
|
142
276
|
}
|
|
277
|
+
try {
|
|
278
|
+
await projectHooks.afterAll?.(makeCtx(RUN_SCOPE, undefined, undefined, { ...cleanArgv }), results);
|
|
279
|
+
}
|
|
280
|
+
catch (err) {
|
|
281
|
+
afterAllErrors.push(err);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
if (pendingError) {
|
|
285
|
+
for (const err of afterAllErrors)
|
|
286
|
+
logger.err(`afterAll failed: ${err.message}`);
|
|
287
|
+
throw pendingError;
|
|
288
|
+
}
|
|
289
|
+
if (afterAllErrors.length > 0) {
|
|
290
|
+
if (singleRunAll)
|
|
291
|
+
throw afterAllErrors[0];
|
|
292
|
+
if (!keepGoing)
|
|
293
|
+
throw new errors_1.ShowHelpError(`afterAll failed: ${afterAllErrors[0].message}`);
|
|
294
|
+
for (const err of afterAllErrors) {
|
|
295
|
+
failures.push({ action: RUN_SCOPE, error: err.message });
|
|
296
|
+
statuses.push({ action: RUN_SCOPE, status: 'failed' });
|
|
297
|
+
}
|
|
143
298
|
}
|
|
144
299
|
if (failures.length > 0) {
|
|
145
300
|
throw new errors_1.KeepGoingError(failures, results, statuses);
|
package/dist/cjs/src/errors.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.NameFlagRemovedError = exports.CliArgumentError = exports.SetupRepositoryError = exports.UnknownTransformError = exports.ConfigTsNodeError = exports.GlobMirrorConflictError = exports.PathMacroResolutionError = exports.ShellCommandError = exports.TemplateConfigError = exports.InjectionAnchorNotFoundError = exports.InjectionTargetNotFoundError = exports.TemplateNotFoundError = exports.TemplateRenderError = exports.KeepGoingError = exports.ShowHelpError = void 0;
|
|
3
|
+
exports.NameFlagRemovedError = exports.CliArgumentError = exports.SetupRepositoryError = exports.UnknownTransformError = exports.HookModuleError = exports.HookTsNodeError = exports.ConfigTsNodeError = exports.GlobMirrorConflictError = exports.PathMacroResolutionError = exports.ShellCommandError = exports.TemplateConfigError = exports.InjectionAnchorNotFoundError = exports.InjectionTargetNotFoundError = exports.TemplateNotFoundError = exports.TemplateRenderError = exports.KeepGoingError = exports.ShowHelpError = void 0;
|
|
4
4
|
const errors_1 = require("@biorate/errors");
|
|
5
5
|
class ShowHelpError extends errors_1.BaseError {
|
|
6
6
|
constructor(message) {
|
|
@@ -83,6 +83,21 @@ class ConfigTsNodeError extends errors_1.BaseError {
|
|
|
83
83
|
}
|
|
84
84
|
}
|
|
85
85
|
exports.ConfigTsNodeError = ConfigTsNodeError;
|
|
86
|
+
class HookTsNodeError extends errors_1.BaseError {
|
|
87
|
+
constructor(file, cause) {
|
|
88
|
+
super('TypeScript hook [%s] requires ts-node: install it with `npm i -D ts-node` (error: [%s])', [file, cause?.message ?? cause]);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
exports.HookTsNodeError = HookTsNodeError;
|
|
92
|
+
class HookModuleError extends errors_1.BaseError {
|
|
93
|
+
constructor(file, detail) {
|
|
94
|
+
super('Hook file [%s] must export a class as its default export (received [%s])', [
|
|
95
|
+
file,
|
|
96
|
+
detail,
|
|
97
|
+
]);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
exports.HookModuleError = HookModuleError;
|
|
86
101
|
class UnknownTransformError extends errors_1.BaseError {
|
|
87
102
|
constructor(name) {
|
|
88
103
|
super('Unknown transform [%s]: register it under the `transforms` config key', [
|
package/dist/cjs/src/prompt.js
CHANGED
|
@@ -26,40 +26,116 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
26
26
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
27
|
};
|
|
28
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
exports.pickGeneratorAction = void 0;
|
|
29
|
+
exports.pickGeneratorAction = exports.normalizeProjectHooks = exports.resolveActionHooks = exports.HOOK_FILES = exports.LEGACY_HOOK_FILES = exports.ACTION_HOOK_FILES = void 0;
|
|
30
30
|
const path_1 = __importDefault(require("path"));
|
|
31
31
|
const fs_1 = __importDefault(require("fs"));
|
|
32
32
|
const helpers_1 = __importDefault(require("./helpers"));
|
|
33
33
|
const help_1 = require("./help");
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
34
|
+
const errors_1 = require("./errors");
|
|
35
|
+
exports.ACTION_HOOK_FILES = ['hooks.ts', 'hooks.js', 'hooks.mjs', 'hooks.cjs'];
|
|
36
|
+
exports.LEGACY_HOOK_FILES = [
|
|
37
|
+
'index.js',
|
|
38
|
+
'index.ts',
|
|
39
|
+
'prompt.cjs',
|
|
40
|
+
'prompt.js',
|
|
41
|
+
'prompt.ts',
|
|
42
|
+
];
|
|
43
|
+
exports.HOOK_FILES = [...exports.ACTION_HOOK_FILES, ...exports.LEGACY_HOOK_FILES];
|
|
44
|
+
const LIFECYCLE_METHODS = ['beforeAll', 'beforeEach', 'afterEach', 'afterAll'];
|
|
45
|
+
const loadHookModule = async (file) => {
|
|
46
|
+
if (/\.ts$/.test(file)) {
|
|
47
|
+
try {
|
|
48
|
+
await Promise.resolve().then(() => __importStar(require('ts-node/register/transpile-only')));
|
|
49
|
+
}
|
|
50
|
+
catch (cause) {
|
|
51
|
+
throw new errors_1.HookTsNodeError(file, cause);
|
|
52
|
+
}
|
|
41
53
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
54
|
+
return (await Promise.resolve(`${file}`).then(s => __importStar(require(s))));
|
|
55
|
+
};
|
|
56
|
+
const legacyAdapter = (raw) => ({
|
|
57
|
+
prompt: async ({ prompter, args }) => {
|
|
58
|
+
if (raw?.params)
|
|
59
|
+
return raw.params({ args, h: helpers_1.default });
|
|
60
|
+
if (raw?.prompt) {
|
|
61
|
+
return raw.prompt({ prompter, inquirer: prompter, args, h: helpers_1.default });
|
|
62
|
+
}
|
|
63
|
+
return prompter.prompt(raw.filter((p) => args[p.name] === undefined ||
|
|
64
|
+
args[p.name] === null ||
|
|
65
|
+
args[p.name]?.length === 0));
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
const loadResolvedHooks = async (actionfolder) => {
|
|
69
|
+
const file = exports.HOOK_FILES.map((f) => path_1.default.resolve(path_1.default.join(actionfolder, f))).find((f) => fs_1.default.existsSync(f));
|
|
70
|
+
if (!file)
|
|
71
|
+
return { instance: {}, kind: 'none' };
|
|
72
|
+
const isClassFile = exports.ACTION_HOOK_FILES.includes(path_1.default.basename(file));
|
|
73
|
+
const module = await loadHookModule(file);
|
|
74
|
+
if (!isClassFile) {
|
|
75
|
+
const raw = module.default ?? module;
|
|
76
|
+
return { instance: legacyAdapter(raw), kind: 'legacy', file, source: raw };
|
|
45
77
|
}
|
|
46
|
-
|
|
47
|
-
if (
|
|
48
|
-
|
|
78
|
+
const exported = module.default;
|
|
79
|
+
if (typeof exported !== 'function') {
|
|
80
|
+
throw new errors_1.HookModuleError(file, exported === undefined
|
|
81
|
+
? 'no default export'
|
|
82
|
+
: `default export is ${typeof exported}`);
|
|
49
83
|
}
|
|
50
|
-
|
|
51
|
-
return
|
|
84
|
+
try {
|
|
85
|
+
return {
|
|
86
|
+
instance: new exported(),
|
|
87
|
+
kind: 'class',
|
|
88
|
+
file,
|
|
89
|
+
};
|
|
52
90
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
91
|
+
catch (cause) {
|
|
92
|
+
throw new errors_1.HookModuleError(file, `default export could not be constructed: ${cause?.message ?? cause}`);
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
const resolveActionHooks = (actionfolder, cache) => {
|
|
96
|
+
const key = path_1.default.resolve(actionfolder);
|
|
97
|
+
if (!cache)
|
|
98
|
+
return loadResolvedHooks(key);
|
|
99
|
+
const cached = cache.get(key);
|
|
100
|
+
if (cached)
|
|
101
|
+
return cached;
|
|
102
|
+
const pending = loadResolvedHooks(key);
|
|
103
|
+
cache.set(key, pending);
|
|
104
|
+
return pending;
|
|
105
|
+
};
|
|
106
|
+
exports.resolveActionHooks = resolveActionHooks;
|
|
107
|
+
const normalizeProjectHooks = (hooks, logger) => {
|
|
108
|
+
if (!hooks || typeof hooks !== 'object')
|
|
109
|
+
return {};
|
|
110
|
+
const normalized = {};
|
|
111
|
+
for (const [key, value] of Object.entries(hooks)) {
|
|
112
|
+
if (!LIFECYCLE_METHODS.includes(key)) {
|
|
113
|
+
logger.warn(`Ignoring unknown project hook '${key}' in .hygen.* configuration.`);
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
if (typeof value !== 'function') {
|
|
117
|
+
logger.warn(`Ignoring non-function project hook '${key}' in .hygen.* configuration.`);
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
normalized[key] = value;
|
|
121
|
+
}
|
|
122
|
+
return normalized;
|
|
123
|
+
};
|
|
124
|
+
exports.normalizeProjectHooks = normalizeProjectHooks;
|
|
125
|
+
const prompt = async (createPrompter, actionfolder, args, options = {}) => {
|
|
126
|
+
const resolved = await (0, exports.resolveActionHooks)(actionfolder, options.cache);
|
|
127
|
+
const legacy = resolved.source;
|
|
128
|
+
if (resolved.kind === 'legacy' && legacy?.params) {
|
|
129
|
+
return legacy.params({ args, h: helpers_1.default });
|
|
61
130
|
}
|
|
62
|
-
|
|
131
|
+
if (!resolved.instance.prompt)
|
|
132
|
+
return {};
|
|
133
|
+
const prompter = createPrompter();
|
|
134
|
+
return resolved.instance.prompt({
|
|
135
|
+
prompter,
|
|
136
|
+
args,
|
|
137
|
+
h: resolved.kind === 'class' ? options.h ?? helpers_1.default : helpers_1.default,
|
|
138
|
+
});
|
|
63
139
|
};
|
|
64
140
|
const answerValue = (answer, name) => {
|
|
65
141
|
const value = answer[name];
|
package/dist/cjs/src/render.js
CHANGED
|
@@ -33,6 +33,10 @@ const evaluateSkip = (rendered) => {
|
|
|
33
33
|
const map = (f) => (arr) => arr.map(f);
|
|
34
34
|
const filter = (f) => (arr) => arr.filter(f);
|
|
35
35
|
const ignores = [
|
|
36
|
+
'hooks.ts',
|
|
37
|
+
'hooks.js',
|
|
38
|
+
'hooks.mjs',
|
|
39
|
+
'hooks.cjs',
|
|
36
40
|
'prompt.js',
|
|
37
41
|
'index.js',
|
|
38
42
|
'prompt.ts',
|
package/dist/esm/src/engine.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import path from 'path';
|
|
2
2
|
import fs from 'fs-extra';
|
|
3
|
+
import context from './context.js';
|
|
3
4
|
import params from './params.js';
|
|
4
|
-
import prompt, { pickGeneratorAction } from './prompt.js';
|
|
5
|
+
import prompt, { normalizeProjectHooks, pickGeneratorAction, resolveActionHooks, } from './prompt.js';
|
|
5
6
|
import { ShowHelpError, KeepGoingError } from './errors.js';
|
|
6
7
|
import { isHelpFlag, printUsage } from './cli-help.js';
|
|
7
8
|
const isInteractive = ({ interactive }) => interactive === true
|
|
@@ -9,6 +10,7 @@ const isInteractive = ({ interactive }) => interactive === true
|
|
|
9
10
|
: interactive === false
|
|
10
11
|
? false
|
|
11
12
|
: Boolean(process.stdin.isTTY && process.stdout.isTTY);
|
|
13
|
+
const RUN_SCOPE = '*';
|
|
12
14
|
const engine = async (argv, config) => {
|
|
13
15
|
const { cwd, templates, logger } = config;
|
|
14
16
|
let base = Object.assign(await params(config, argv), { cwd });
|
|
@@ -36,23 +38,69 @@ const engine = async (argv, config) => {
|
|
|
36
38
|
logger.log(`Loaded templates: ${templates.replace(`${cwd}/`, '')}`);
|
|
37
39
|
const nameList = names ?? [undefined];
|
|
38
40
|
const keepGoing = Boolean(cleanArgv['keep-going']);
|
|
41
|
+
const singleRunAll = nameList.length === 1 && actions.length === 1;
|
|
39
42
|
const failures = [];
|
|
40
43
|
const results = [];
|
|
41
44
|
const statuses = [];
|
|
42
45
|
const execute = (await import('./execute.js')).execute;
|
|
43
46
|
const render = (await import('./render.js')).render;
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
47
|
+
const projectHooks = normalizeProjectHooks(config.hooks, logger);
|
|
48
|
+
const hookHelpers = context({}, config).h;
|
|
49
|
+
const hookCache = new Map();
|
|
50
|
+
const actionStates = new Map();
|
|
51
|
+
const makeCtx = (action, name, subaction, args) => ({
|
|
52
|
+
cwd,
|
|
53
|
+
templates: base.templates,
|
|
54
|
+
generator: generator,
|
|
55
|
+
action,
|
|
56
|
+
...(subaction !== undefined ? { subaction } : {}),
|
|
57
|
+
...(name !== undefined ? { name } : {}),
|
|
58
|
+
args,
|
|
59
|
+
h: hookHelpers,
|
|
60
|
+
logger,
|
|
61
|
+
dry: Boolean(base.dry),
|
|
62
|
+
});
|
|
63
|
+
const actionState = (action, subaction) => {
|
|
64
|
+
let state = actionStates.get(action);
|
|
65
|
+
if (!state) {
|
|
66
|
+
state = { started: false, hooks: {}, subaction, results: [] };
|
|
67
|
+
actionStates.set(action, state);
|
|
68
|
+
}
|
|
69
|
+
return state;
|
|
70
|
+
};
|
|
71
|
+
const reportPairError = (error, pair, pairLabel) => {
|
|
72
|
+
if (singleRunAll)
|
|
73
|
+
throw error;
|
|
74
|
+
if (!keepGoing)
|
|
75
|
+
throw new ShowHelpError(`${pairLabel} failed: ${error.message}`);
|
|
76
|
+
failures.push({ ...pair, error: error.message });
|
|
77
|
+
statuses.push({ ...pair, status: 'failed' });
|
|
78
|
+
};
|
|
79
|
+
let projectBeforeAllError;
|
|
80
|
+
try {
|
|
81
|
+
await projectHooks.beforeAll?.(makeCtx(RUN_SCOPE, undefined, undefined, { ...cleanArgv }));
|
|
82
|
+
}
|
|
83
|
+
catch (err) {
|
|
84
|
+
projectBeforeAllError = err;
|
|
85
|
+
}
|
|
86
|
+
let pendingError;
|
|
87
|
+
const afterAllErrors = [];
|
|
88
|
+
try {
|
|
89
|
+
for (const name of nameList) {
|
|
90
|
+
for (const action of actions) {
|
|
91
|
+
const [mainAction, subaction] = action.split(':');
|
|
92
|
+
const actionfolder = path.join(base.templates, generator, mainAction);
|
|
93
|
+
const pairLabel = `action '${action}'${name !== undefined ? ` for name '${name}'` : ''}`;
|
|
94
|
+
const pair = {
|
|
95
|
+
action,
|
|
96
|
+
...(name !== undefined ? { name } : {}),
|
|
97
|
+
};
|
|
98
|
+
const pairStatus = {
|
|
99
|
+
action,
|
|
100
|
+
...(name !== undefined ? { name } : {}),
|
|
101
|
+
};
|
|
102
|
+
if (!(await fs.pathExists(actionfolder))) {
|
|
103
|
+
const err = new ShowHelpError(`I can't find action '${action}' for generator '${generator}'.
|
|
56
104
|
|
|
57
105
|
You can try:
|
|
58
106
|
1. 'hygen init self' to initialize your project, and
|
|
@@ -60,55 +108,162 @@ const engine = async (argv, config) => {
|
|
|
60
108
|
|
|
61
109
|
Check out the quickstart for more: http://www.hygen.io/docs/quick-start
|
|
62
110
|
`);
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
111
|
+
if (singleRunAll)
|
|
112
|
+
throw err;
|
|
113
|
+
if (!keepGoing)
|
|
114
|
+
throw err;
|
|
115
|
+
failures.push({ ...pair, error: err.message });
|
|
116
|
+
statuses.push({ ...pairStatus, status: 'failed' });
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
if (projectBeforeAllError) {
|
|
120
|
+
reportPairError(projectBeforeAllError, pair, pairLabel);
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
const state = actionState(action, subaction);
|
|
124
|
+
if (!state.started) {
|
|
125
|
+
state.started = true;
|
|
126
|
+
try {
|
|
127
|
+
state.hooks = (await resolveActionHooks(actionfolder, hookCache)).instance;
|
|
128
|
+
await state.hooks.beforeAll?.(makeCtx(action, undefined, subaction, { ...cleanArgv }));
|
|
129
|
+
}
|
|
130
|
+
catch (err) {
|
|
131
|
+
state.beforeAllError = err;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
if (state.beforeAllError) {
|
|
135
|
+
reportPairError(state.beforeAllError, pair, pairLabel);
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
const hookArgs = {
|
|
77
139
|
...(name ? { name } : {}),
|
|
78
140
|
...cleanArgv,
|
|
79
|
-
}
|
|
80
|
-
const
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
141
|
+
};
|
|
142
|
+
const pairResults = [];
|
|
143
|
+
const afterErrors = [];
|
|
144
|
+
let pairStatuses = [];
|
|
145
|
+
let pairError;
|
|
146
|
+
for (const run of [
|
|
147
|
+
projectHooks.beforeEach
|
|
148
|
+
? () => projectHooks.beforeEach(makeCtx(action, name, subaction, hookArgs))
|
|
149
|
+
: undefined,
|
|
150
|
+
state.hooks.beforeEach
|
|
151
|
+
? () => state.hooks.beforeEach(makeCtx(action, name, subaction, hookArgs))
|
|
152
|
+
: undefined,
|
|
153
|
+
]) {
|
|
154
|
+
if (!run)
|
|
155
|
+
continue;
|
|
156
|
+
try {
|
|
157
|
+
await run();
|
|
158
|
+
}
|
|
159
|
+
catch (err) {
|
|
160
|
+
pairError = err;
|
|
161
|
+
break;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
if (!pairError) {
|
|
165
|
+
try {
|
|
166
|
+
const promptArgs = await prompt(config.createPrompter, actionfolder, hookArgs, {
|
|
167
|
+
h: hookHelpers,
|
|
168
|
+
cache: hookCache,
|
|
169
|
+
});
|
|
170
|
+
const args = Object.assign({
|
|
171
|
+
templates: base.templates,
|
|
172
|
+
actionfolder,
|
|
173
|
+
generator,
|
|
174
|
+
action,
|
|
175
|
+
subaction,
|
|
176
|
+
}, { _: base._, ts: base.ts }, cleanArgv, name ? { name } : {}, promptArgs);
|
|
177
|
+
const el = await execute(await render(args, config), args, config);
|
|
178
|
+
for (const pairResult of el.results) {
|
|
179
|
+
const annotated = {
|
|
180
|
+
...pairResult,
|
|
181
|
+
action,
|
|
182
|
+
...(name !== undefined ? { name } : {}),
|
|
183
|
+
};
|
|
184
|
+
results.push(annotated);
|
|
185
|
+
pairResults.push(annotated);
|
|
186
|
+
}
|
|
187
|
+
pairStatuses = el.statuses;
|
|
188
|
+
}
|
|
189
|
+
catch (err) {
|
|
190
|
+
pairError = err;
|
|
191
|
+
}
|
|
94
192
|
}
|
|
95
|
-
|
|
193
|
+
for (const run of [
|
|
194
|
+
state.hooks.afterEach
|
|
195
|
+
? () => state.hooks.afterEach(makeCtx(action, name, subaction, hookArgs), pairResults)
|
|
196
|
+
: undefined,
|
|
197
|
+
projectHooks.afterEach
|
|
198
|
+
? () => projectHooks.afterEach(makeCtx(action, name, subaction, hookArgs), pairResults)
|
|
199
|
+
: undefined,
|
|
200
|
+
]) {
|
|
201
|
+
if (!run)
|
|
202
|
+
continue;
|
|
203
|
+
try {
|
|
204
|
+
await run();
|
|
205
|
+
}
|
|
206
|
+
catch (err) {
|
|
207
|
+
afterErrors.push(err);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
state.results.push(...pairResults);
|
|
211
|
+
if (pairError) {
|
|
212
|
+
reportPairError(pairError, pair, pairLabel);
|
|
213
|
+
if (keepGoing) {
|
|
214
|
+
for (const err of afterErrors)
|
|
215
|
+
failures.push({ ...pair, error: err.message });
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
else if (afterErrors.length > 0) {
|
|
219
|
+
statuses.push(...pairStatuses);
|
|
220
|
+
reportPairError(afterErrors[0], pair, pairLabel);
|
|
221
|
+
if (keepGoing) {
|
|
222
|
+
for (const err of afterErrors.slice(1))
|
|
223
|
+
failures.push({ ...pair, error: err.message });
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
else {
|
|
227
|
+
statuses.push(...pairStatuses);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
catch (err) {
|
|
233
|
+
pendingError = err;
|
|
234
|
+
}
|
|
235
|
+
finally {
|
|
236
|
+
for (const [action, state] of actionStates) {
|
|
237
|
+
if (!state.started)
|
|
238
|
+
continue;
|
|
239
|
+
try {
|
|
240
|
+
await state.hooks.afterAll?.(makeCtx(action, undefined, state.subaction, { ...cleanArgv }), state.results);
|
|
96
241
|
}
|
|
97
242
|
catch (err) {
|
|
98
|
-
|
|
99
|
-
if (singleRun)
|
|
100
|
-
throw err;
|
|
101
|
-
if (!keepGoing) {
|
|
102
|
-
throw new ShowHelpError(`${pairLabel} failed: ${e.message}`);
|
|
103
|
-
}
|
|
104
|
-
failures.push({
|
|
105
|
-
action,
|
|
106
|
-
...(name !== undefined ? { name } : {}),
|
|
107
|
-
error: e.message,
|
|
108
|
-
});
|
|
109
|
-
statuses.push({ ...statusPair, status: 'failed' });
|
|
243
|
+
afterAllErrors.push(err);
|
|
110
244
|
}
|
|
111
245
|
}
|
|
246
|
+
try {
|
|
247
|
+
await projectHooks.afterAll?.(makeCtx(RUN_SCOPE, undefined, undefined, { ...cleanArgv }), results);
|
|
248
|
+
}
|
|
249
|
+
catch (err) {
|
|
250
|
+
afterAllErrors.push(err);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
if (pendingError) {
|
|
254
|
+
for (const err of afterAllErrors)
|
|
255
|
+
logger.err(`afterAll failed: ${err.message}`);
|
|
256
|
+
throw pendingError;
|
|
257
|
+
}
|
|
258
|
+
if (afterAllErrors.length > 0) {
|
|
259
|
+
if (singleRunAll)
|
|
260
|
+
throw afterAllErrors[0];
|
|
261
|
+
if (!keepGoing)
|
|
262
|
+
throw new ShowHelpError(`afterAll failed: ${afterAllErrors[0].message}`);
|
|
263
|
+
for (const err of afterAllErrors) {
|
|
264
|
+
failures.push({ action: RUN_SCOPE, error: err.message });
|
|
265
|
+
statuses.push({ action: RUN_SCOPE, status: 'failed' });
|
|
266
|
+
}
|
|
112
267
|
}
|
|
113
268
|
if (failures.length > 0) {
|
|
114
269
|
throw new KeepGoingError(failures, results, statuses);
|
package/dist/esm/src/errors.js
CHANGED
|
@@ -69,6 +69,19 @@ export class ConfigTsNodeError extends BaseError {
|
|
|
69
69
|
super('TypeScript config [%s] requires ts-node: install it with `npm i -D ts-node` (error: [%s])', [file, cause?.message ?? cause]);
|
|
70
70
|
}
|
|
71
71
|
}
|
|
72
|
+
export class HookTsNodeError extends BaseError {
|
|
73
|
+
constructor(file, cause) {
|
|
74
|
+
super('TypeScript hook [%s] requires ts-node: install it with `npm i -D ts-node` (error: [%s])', [file, cause?.message ?? cause]);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
export class HookModuleError extends BaseError {
|
|
78
|
+
constructor(file, detail) {
|
|
79
|
+
super('Hook file [%s] must export a class as its default export (received [%s])', [
|
|
80
|
+
file,
|
|
81
|
+
detail,
|
|
82
|
+
]);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
72
85
|
export class UnknownTransformError extends BaseError {
|
|
73
86
|
constructor(name) {
|
|
74
87
|
super('Unknown transform [%s]: register it under the `transforms` config key', [
|
package/dist/esm/src/prompt.js
CHANGED
|
@@ -2,35 +2,109 @@ import path from 'path';
|
|
|
2
2
|
import fs from 'fs';
|
|
3
3
|
import helpers from './helpers.js';
|
|
4
4
|
import { pickableGenerators, generatorActions } from './help.js';
|
|
5
|
-
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
5
|
+
import { HookModuleError, HookTsNodeError } from './errors.js';
|
|
6
|
+
export const ACTION_HOOK_FILES = ['hooks.ts', 'hooks.js', 'hooks.mjs', 'hooks.cjs'];
|
|
7
|
+
export const LEGACY_HOOK_FILES = [
|
|
8
|
+
'index.js',
|
|
9
|
+
'index.ts',
|
|
10
|
+
'prompt.cjs',
|
|
11
|
+
'prompt.js',
|
|
12
|
+
'prompt.ts',
|
|
13
|
+
];
|
|
14
|
+
export const HOOK_FILES = [...ACTION_HOOK_FILES, ...LEGACY_HOOK_FILES];
|
|
15
|
+
const LIFECYCLE_METHODS = ['beforeAll', 'beforeEach', 'afterEach', 'afterAll'];
|
|
16
|
+
const loadHookModule = async (file) => {
|
|
17
|
+
if (/\.ts$/.test(file)) {
|
|
18
|
+
try {
|
|
19
|
+
await import('ts-node/register/transpile-only');
|
|
20
|
+
}
|
|
21
|
+
catch (cause) {
|
|
22
|
+
throw new HookTsNodeError(file, cause);
|
|
23
|
+
}
|
|
12
24
|
}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
25
|
+
return (await import(file));
|
|
26
|
+
};
|
|
27
|
+
const legacyAdapter = (raw) => ({
|
|
28
|
+
prompt: async ({ prompter, args }) => {
|
|
29
|
+
if (raw?.params)
|
|
30
|
+
return raw.params({ args, h: helpers });
|
|
31
|
+
if (raw?.prompt) {
|
|
32
|
+
return raw.prompt({ prompter, inquirer: prompter, args, h: helpers });
|
|
33
|
+
}
|
|
34
|
+
return prompter.prompt(raw.filter((p) => args[p.name] === undefined ||
|
|
35
|
+
args[p.name] === null ||
|
|
36
|
+
args[p.name]?.length === 0));
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
const loadResolvedHooks = async (actionfolder) => {
|
|
40
|
+
const file = HOOK_FILES.map((f) => path.resolve(path.join(actionfolder, f))).find((f) => fs.existsSync(f));
|
|
41
|
+
if (!file)
|
|
42
|
+
return { instance: {}, kind: 'none' };
|
|
43
|
+
const isClassFile = ACTION_HOOK_FILES.includes(path.basename(file));
|
|
44
|
+
const module = await loadHookModule(file);
|
|
45
|
+
if (!isClassFile) {
|
|
46
|
+
const raw = module.default ?? module;
|
|
47
|
+
return { instance: legacyAdapter(raw), kind: 'legacy', file, source: raw };
|
|
16
48
|
}
|
|
17
|
-
|
|
18
|
-
if (
|
|
19
|
-
|
|
49
|
+
const exported = module.default;
|
|
50
|
+
if (typeof exported !== 'function') {
|
|
51
|
+
throw new HookModuleError(file, exported === undefined
|
|
52
|
+
? 'no default export'
|
|
53
|
+
: `default export is ${typeof exported}`);
|
|
20
54
|
}
|
|
21
|
-
|
|
22
|
-
return
|
|
55
|
+
try {
|
|
56
|
+
return {
|
|
57
|
+
instance: new exported(),
|
|
58
|
+
kind: 'class',
|
|
59
|
+
file,
|
|
60
|
+
};
|
|
23
61
|
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
62
|
+
catch (cause) {
|
|
63
|
+
throw new HookModuleError(file, `default export could not be constructed: ${cause?.message ?? cause}`);
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
export const resolveActionHooks = (actionfolder, cache) => {
|
|
67
|
+
const key = path.resolve(actionfolder);
|
|
68
|
+
if (!cache)
|
|
69
|
+
return loadResolvedHooks(key);
|
|
70
|
+
const cached = cache.get(key);
|
|
71
|
+
if (cached)
|
|
72
|
+
return cached;
|
|
73
|
+
const pending = loadResolvedHooks(key);
|
|
74
|
+
cache.set(key, pending);
|
|
75
|
+
return pending;
|
|
76
|
+
};
|
|
77
|
+
export const normalizeProjectHooks = (hooks, logger) => {
|
|
78
|
+
if (!hooks || typeof hooks !== 'object')
|
|
79
|
+
return {};
|
|
80
|
+
const normalized = {};
|
|
81
|
+
for (const [key, value] of Object.entries(hooks)) {
|
|
82
|
+
if (!LIFECYCLE_METHODS.includes(key)) {
|
|
83
|
+
logger.warn(`Ignoring unknown project hook '${key}' in .hygen.* configuration.`);
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
if (typeof value !== 'function') {
|
|
87
|
+
logger.warn(`Ignoring non-function project hook '${key}' in .hygen.* configuration.`);
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
normalized[key] = value;
|
|
32
91
|
}
|
|
33
|
-
return
|
|
92
|
+
return normalized;
|
|
93
|
+
};
|
|
94
|
+
const prompt = async (createPrompter, actionfolder, args, options = {}) => {
|
|
95
|
+
const resolved = await resolveActionHooks(actionfolder, options.cache);
|
|
96
|
+
const legacy = resolved.source;
|
|
97
|
+
if (resolved.kind === 'legacy' && legacy?.params) {
|
|
98
|
+
return legacy.params({ args, h: helpers });
|
|
99
|
+
}
|
|
100
|
+
if (!resolved.instance.prompt)
|
|
101
|
+
return {};
|
|
102
|
+
const prompter = createPrompter();
|
|
103
|
+
return resolved.instance.prompt({
|
|
104
|
+
prompter,
|
|
105
|
+
args,
|
|
106
|
+
h: resolved.kind === 'class' ? options.h ?? helpers : helpers,
|
|
107
|
+
});
|
|
34
108
|
};
|
|
35
109
|
const answerValue = (answer, name) => {
|
|
36
110
|
const value = answer[name];
|
package/dist/esm/src/render.js
CHANGED
|
@@ -27,6 +27,10 @@ const evaluateSkip = (rendered) => {
|
|
|
27
27
|
const map = (f) => (arr) => arr.map(f);
|
|
28
28
|
const filter = (f) => (arr) => arr.filter(f);
|
|
29
29
|
const ignores = [
|
|
30
|
+
'hooks.ts',
|
|
31
|
+
'hooks.js',
|
|
32
|
+
'hooks.mjs',
|
|
33
|
+
'hooks.cjs',
|
|
30
34
|
'prompt.js',
|
|
31
35
|
'index.js',
|
|
32
36
|
'prompt.ts',
|
|
@@ -36,6 +36,12 @@ export declare class GlobMirrorConflictError extends BaseError {
|
|
|
36
36
|
export declare class ConfigTsNodeError extends BaseError {
|
|
37
37
|
constructor(file: string, cause: unknown);
|
|
38
38
|
}
|
|
39
|
+
export declare class HookTsNodeError extends BaseError {
|
|
40
|
+
constructor(file: string, cause: unknown);
|
|
41
|
+
}
|
|
42
|
+
export declare class HookModuleError extends BaseError {
|
|
43
|
+
constructor(file: string, detail: string);
|
|
44
|
+
}
|
|
39
45
|
export declare class UnknownTransformError extends BaseError {
|
|
40
46
|
constructor(name: string);
|
|
41
47
|
}
|
|
@@ -1,5 +1,21 @@
|
|
|
1
|
-
import type { Prompter, RunnerConfig } from './types';
|
|
2
|
-
declare const
|
|
1
|
+
import type { ActionHooks, Helpers, Prompter, ProjectHooks, RunnerConfig } from './types';
|
|
2
|
+
export declare const ACTION_HOOK_FILES: string[];
|
|
3
|
+
export declare const LEGACY_HOOK_FILES: string[];
|
|
4
|
+
export declare const HOOK_FILES: string[];
|
|
5
|
+
export type HookKind = 'class' | 'legacy' | 'none';
|
|
6
|
+
export interface ResolvedActionHooks {
|
|
7
|
+
instance: ActionHooks;
|
|
8
|
+
kind: HookKind;
|
|
9
|
+
file?: string;
|
|
10
|
+
source?: unknown;
|
|
11
|
+
}
|
|
12
|
+
export type HookCache = Map<string, Promise<ResolvedActionHooks>>;
|
|
13
|
+
export declare const resolveActionHooks: (actionfolder: string, cache?: HookCache) => Promise<ResolvedActionHooks>;
|
|
14
|
+
export declare const normalizeProjectHooks: (hooks: unknown, logger: RunnerConfig["logger"]) => ProjectHooks;
|
|
15
|
+
declare const prompt: <Q, T>(createPrompter: () => Prompter<Q, T>, actionfolder: string, args: Record<string, any>, options?: {
|
|
16
|
+
h?: Helpers;
|
|
17
|
+
cache?: HookCache;
|
|
18
|
+
}) => Promise<T | object>;
|
|
3
19
|
export interface PickResult {
|
|
4
20
|
generator: string;
|
|
5
21
|
action: string;
|
|
@@ -31,6 +31,32 @@ export type Helper = (...args: unknown[]) => unknown;
|
|
|
31
31
|
export type Helpers = Record<string, unknown>;
|
|
32
32
|
export type Transform = (content: string, locals: Record<string, unknown>) => string | Promise<string>;
|
|
33
33
|
export type Transforms = Record<string, Transform>;
|
|
34
|
+
export interface HookContext {
|
|
35
|
+
cwd: string;
|
|
36
|
+
templates: string;
|
|
37
|
+
generator: string;
|
|
38
|
+
action: string;
|
|
39
|
+
subaction?: string;
|
|
40
|
+
name?: string;
|
|
41
|
+
args: Record<string, unknown>;
|
|
42
|
+
h: Helpers;
|
|
43
|
+
logger: Logger;
|
|
44
|
+
dry: boolean;
|
|
45
|
+
}
|
|
46
|
+
export type HookMethod = (ctx: HookContext) => unknown | Promise<unknown>;
|
|
47
|
+
export interface ProjectHooks {
|
|
48
|
+
beforeAll?: HookMethod;
|
|
49
|
+
beforeEach?: HookMethod;
|
|
50
|
+
afterEach?: (ctx: HookContext, results: ActionResult[]) => unknown | Promise<unknown>;
|
|
51
|
+
afterAll?: (ctx: HookContext, results: ActionResult[]) => unknown | Promise<unknown>;
|
|
52
|
+
}
|
|
53
|
+
export interface ActionHooks extends ProjectHooks {
|
|
54
|
+
prompt?: (input: {
|
|
55
|
+
prompter: Prompter<any, any>;
|
|
56
|
+
args: Record<string, unknown>;
|
|
57
|
+
h: Helpers;
|
|
58
|
+
}) => unknown;
|
|
59
|
+
}
|
|
34
60
|
export interface RunnerConfig {
|
|
35
61
|
exec: (sh: string, body: string) => void;
|
|
36
62
|
templates?: string;
|
|
@@ -40,6 +66,7 @@ export interface RunnerConfig {
|
|
|
40
66
|
debug?: boolean;
|
|
41
67
|
helpers?: Helpers | ((locals: Record<string, unknown>, config: RunnerContextConfig) => Helpers);
|
|
42
68
|
transforms?: Transforms;
|
|
69
|
+
hooks?: ProjectHooks;
|
|
43
70
|
localsDefaults?: Record<string, unknown>;
|
|
44
71
|
createPrompter: <Q, T>() => Prompter<Q, T>;
|
|
45
72
|
legacyNameFlag?: boolean;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@biorate/hygen",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "The scalable code generator that saves you time. Fork of jondot/hygen",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"template",
|
|
@@ -80,5 +80,5 @@
|
|
|
80
80
|
"@types/inflection": "1.13.0",
|
|
81
81
|
"@types/yargs-parser": "21.0.3"
|
|
82
82
|
},
|
|
83
|
-
"gitHead": "
|
|
83
|
+
"gitHead": "d73673a1ec66c0aa22f95373b958ea5c98adbfa4"
|
|
84
84
|
}
|