@biorate/hygen 0.3.0 → 0.5.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/README.md +14 -14
- package/dist/cjs/src/config-resolver.js +4 -11
- package/dist/cjs/src/constants/config.js +5 -0
- package/dist/cjs/src/constants/engine.js +4 -0
- package/dist/cjs/src/constants/hooks.js +18 -0
- package/dist/cjs/src/constants/index.js +19 -0
- package/dist/cjs/src/engine.js +14 -14
- package/dist/cjs/src/errors.js +19 -1
- package/dist/cjs/src/help.js +3 -3
- package/dist/cjs/src/params-schema/index.js +11 -0
- package/dist/cjs/src/params-schema/prompts.js +34 -0
- package/dist/cjs/src/params-schema/registry.js +92 -0
- package/dist/cjs/src/params.js +21 -5
- package/dist/cjs/src/picker.js +55 -0
- package/dist/cjs/src/prompt.js +39 -69
- package/dist/cjs/src/ts-node-register.js +36 -0
- package/dist/cjs/src/types/config.js +2 -0
- package/dist/cjs/src/types/helpers-ns.js +2 -0
- package/dist/cjs/src/types/hooks.js +2 -0
- package/dist/cjs/src/types/index.js +28 -0
- package/dist/cjs/src/types/inflection.js +2 -0
- package/dist/cjs/src/types/logger.js +2 -0
- package/dist/cjs/src/types/ops.js +2 -0
- package/dist/cjs/src/types/params-schema.js +2 -0
- package/dist/cjs/src/types/params.js +2 -0
- package/dist/cjs/src/types/prompter-ns.js +2 -0
- package/dist/cjs/src/types/prompter.js +2 -0
- package/dist/cjs/src/types/results.js +2 -0
- package/dist/esm/src/config-resolver.js +3 -9
- package/dist/esm/src/constants/config.js +2 -0
- package/dist/esm/src/constants/engine.js +1 -0
- package/dist/esm/src/constants/hooks.js +15 -0
- package/dist/esm/src/constants/index.js +3 -0
- package/dist/esm/src/engine.js +10 -10
- package/dist/esm/src/errors.js +15 -0
- package/dist/esm/src/help.js +1 -1
- package/dist/esm/src/params-schema/index.js +2 -0
- package/dist/esm/src/params-schema/prompts.js +30 -0
- package/dist/esm/src/params-schema/registry.js +84 -0
- package/dist/esm/src/params.js +19 -2
- package/dist/esm/src/picker.js +48 -0
- package/dist/esm/src/prompt.js +34 -64
- package/dist/esm/src/ts-node-register.js +11 -0
- package/dist/esm/src/types/config.js +1 -0
- package/dist/esm/src/types/helpers-ns.js +1 -0
- package/dist/esm/src/types/hooks.js +1 -0
- package/dist/esm/src/types/index.js +12 -0
- package/dist/esm/src/types/inflection.js +1 -0
- package/dist/esm/src/types/logger.js +1 -0
- package/dist/esm/src/types/ops.js +1 -0
- package/dist/esm/src/types/params-schema.js +1 -0
- package/dist/esm/src/types/params.js +1 -0
- package/dist/esm/src/types/prompter-ns.js +1 -0
- package/dist/esm/src/types/prompter.js +1 -0
- package/dist/esm/src/types/results.js +1 -0
- package/dist/types/src/config-resolver.d.ts +0 -1
- package/dist/types/src/constants/config.d.ts +2 -0
- package/dist/types/src/constants/engine.d.ts +1 -0
- package/dist/types/src/constants/hooks.d.ts +4 -0
- package/dist/types/src/constants/index.d.ts +3 -0
- package/dist/types/src/context.d.ts +1 -1
- package/dist/types/src/errors.d.ts +9 -0
- package/dist/types/src/helpers.d.ts +1 -13
- package/dist/types/src/index.d.ts +1 -0
- package/dist/types/src/ops/anchors.d.ts +0 -3
- package/dist/types/src/ops/index.d.ts +1 -2
- package/dist/types/src/params-schema/index.d.ts +2 -0
- package/dist/types/src/params-schema/prompts.d.ts +2 -0
- package/dist/types/src/params-schema/registry.d.ts +6 -0
- package/dist/types/src/params.d.ts +1 -2
- package/dist/types/src/picker.d.ts +2 -0
- package/dist/types/src/prompt.d.ts +4 -18
- package/dist/types/src/ts-node-register.d.ts +2 -0
- package/dist/types/src/types/attributes.d.ts +18 -0
- package/dist/types/src/types/config.d.ts +26 -0
- package/dist/types/src/types/helpers-ns.d.ts +4 -0
- package/dist/types/src/types/hooks.d.ts +39 -0
- package/dist/types/src/types/index.d.ts +12 -0
- package/dist/types/src/types/inflection.d.ts +12 -0
- package/dist/types/src/types/logger.d.ts +8 -0
- package/dist/types/src/types/ops.d.ts +7 -0
- package/dist/types/src/types/params-schema.d.ts +9 -0
- package/dist/types/src/types/params.d.ts +14 -0
- package/dist/types/src/types/prompter-ns.d.ts +4 -0
- package/dist/types/src/types/prompter.d.ts +3 -0
- package/dist/types/src/types/results.d.ts +27 -0
- package/package.json +2 -2
- package/dist/types/src/types.d.ts +0 -121
- /package/dist/cjs/src/{types.js → types/attributes.js} +0 -0
- /package/dist/esm/src/{types.js → types/attributes.js} +0 -0
package/dist/cjs/src/prompt.js
CHANGED
|
@@ -26,31 +26,37 @@ 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.
|
|
29
|
+
exports.normalizeProjectHooks = exports.resolveActionHooks = exports.validateParamsSchema = 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
|
-
const help_1 = require("./help");
|
|
34
33
|
const errors_1 = require("./errors");
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
try {
|
|
48
|
-
await Promise.resolve().then(() => __importStar(require('ts-node/register/transpile-only')));
|
|
34
|
+
const ts_node_register_1 = __importDefault(require("./ts-node-register"));
|
|
35
|
+
const params_schema_1 = require("./params-schema");
|
|
36
|
+
const errors_2 = require("./errors");
|
|
37
|
+
const validateParamsSchema = (schema) => {
|
|
38
|
+
if (!schema)
|
|
39
|
+
return undefined;
|
|
40
|
+
if (!Array.isArray(schema)) {
|
|
41
|
+
throw new Error('the params export must be an array of parameter descriptors');
|
|
42
|
+
}
|
|
43
|
+
for (const descriptor of schema) {
|
|
44
|
+
if (typeof descriptor?.name !== 'string' || descriptor.name === '') {
|
|
45
|
+
throw new Error('every parameter descriptor must declare a non-empty string name');
|
|
49
46
|
}
|
|
50
|
-
|
|
51
|
-
|
|
47
|
+
const { type } = descriptor;
|
|
48
|
+
if (typeof type === 'function')
|
|
49
|
+
continue;
|
|
50
|
+
if (type !== 'string' && type !== 'number' && type !== 'boolean') {
|
|
51
|
+
throw new errors_2.ParamUnknownTypeError(descriptor.name, String(type));
|
|
52
52
|
}
|
|
53
53
|
}
|
|
54
|
+
return schema;
|
|
55
|
+
};
|
|
56
|
+
exports.validateParamsSchema = validateParamsSchema;
|
|
57
|
+
const constants_1 = require("./constants");
|
|
58
|
+
const loadHookModule = async (file) => {
|
|
59
|
+
await (0, ts_node_register_1.default)(file, errors_1.HookTsNodeError);
|
|
54
60
|
return (await Promise.resolve(`${file}`).then(s => __importStar(require(s))));
|
|
55
61
|
};
|
|
56
62
|
const legacyAdapter = (raw) => ({
|
|
@@ -66,10 +72,10 @@ const legacyAdapter = (raw) => ({
|
|
|
66
72
|
},
|
|
67
73
|
});
|
|
68
74
|
const loadResolvedHooks = async (actionfolder) => {
|
|
69
|
-
const file =
|
|
75
|
+
const file = constants_1.HOOK_FILES.map((f) => path_1.default.resolve(path_1.default.join(actionfolder, f))).find((f) => fs_1.default.existsSync(f));
|
|
70
76
|
if (!file)
|
|
71
77
|
return { instance: {}, kind: 'none' };
|
|
72
|
-
const isClassFile =
|
|
78
|
+
const isClassFile = constants_1.ACTION_HOOK_FILES.includes(path_1.default.basename(file));
|
|
73
79
|
const module = await loadHookModule(file);
|
|
74
80
|
if (!isClassFile) {
|
|
75
81
|
const raw = module.default ?? module;
|
|
@@ -86,6 +92,7 @@ const loadResolvedHooks = async (actionfolder) => {
|
|
|
86
92
|
instance: new exported(),
|
|
87
93
|
kind: 'class',
|
|
88
94
|
file,
|
|
95
|
+
params: (0, exports.validateParamsSchema)(module.params),
|
|
89
96
|
};
|
|
90
97
|
}
|
|
91
98
|
catch (cause) {
|
|
@@ -109,7 +116,7 @@ const normalizeProjectHooks = (hooks, logger) => {
|
|
|
109
116
|
return {};
|
|
110
117
|
const normalized = {};
|
|
111
118
|
for (const [key, value] of Object.entries(hooks)) {
|
|
112
|
-
if (!LIFECYCLE_METHODS.includes(key)) {
|
|
119
|
+
if (!constants_1.LIFECYCLE_METHODS.includes(key)) {
|
|
113
120
|
logger.warn(`Ignoring unknown project hook '${key}' in .hygen.* configuration.`);
|
|
114
121
|
continue;
|
|
115
122
|
}
|
|
@@ -128,60 +135,23 @@ const prompt = async (createPrompter, actionfolder, args, options = {}) => {
|
|
|
128
135
|
if (resolved.kind === 'legacy' && legacy?.params) {
|
|
129
136
|
return legacy.params({ args, h: helpers_1.default });
|
|
130
137
|
}
|
|
138
|
+
if (resolved.kind === 'class' && resolved.params?.length) {
|
|
139
|
+
return (0, params_schema_1.promptFromParams)(resolved.params, createPrompter, args);
|
|
140
|
+
}
|
|
131
141
|
if (!resolved.instance.prompt)
|
|
132
142
|
return {};
|
|
133
143
|
const prompter = createPrompter();
|
|
144
|
+
const lazyHelpers = options.h;
|
|
134
145
|
return resolved.instance.prompt({
|
|
135
146
|
prompter,
|
|
136
147
|
args,
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
}
|
|
144
|
-
const pickGeneratorAction = async (config) => {
|
|
145
|
-
const { templates, logger } = config;
|
|
146
|
-
if (!templates || !fs_1.default.existsSync(templates)) {
|
|
147
|
-
logger.log(`No generators found.
|
|
148
|
-
|
|
149
|
-
The interactive picker needs a _templates folder here, or anywhere up the folder tree.
|
|
150
|
-
Create one with: $ hygen init self
|
|
151
|
-
`);
|
|
152
|
-
return null;
|
|
153
|
-
}
|
|
154
|
-
const generators = (0, help_1.pickableGenerators)(templates);
|
|
155
|
-
if (generators.length === 0) {
|
|
156
|
-
logger.log(`No generators found.
|
|
157
|
-
|
|
158
|
-
Every top-level folder in _templates should hold at least one action subfolder.
|
|
159
|
-
Create one with: $ hygen with-prompt new my-generator
|
|
160
|
-
`);
|
|
161
|
-
return null;
|
|
162
|
-
}
|
|
163
|
-
const prompter = config.createPrompter();
|
|
164
|
-
const generatorAnswer = await prompter.prompt({
|
|
165
|
-
type: 'select',
|
|
166
|
-
name: 'generator',
|
|
167
|
-
message: 'Select a generator:',
|
|
168
|
-
choices: generators,
|
|
169
|
-
});
|
|
170
|
-
const generator = answerValue(generatorAnswer, 'generator');
|
|
171
|
-
if (!generator)
|
|
172
|
-
return null;
|
|
173
|
-
const actions = (0, help_1.generatorActions)(templates, generator);
|
|
174
|
-
const actionPrompter = config.createPrompter();
|
|
175
|
-
const actionAnswer = await actionPrompter.prompt({
|
|
176
|
-
type: 'select',
|
|
177
|
-
name: 'action',
|
|
178
|
-
message: `Select an action for ${generator}:`,
|
|
179
|
-
choices: actions,
|
|
148
|
+
get h() {
|
|
149
|
+
if (resolved.kind !== 'class')
|
|
150
|
+
return helpers_1.default;
|
|
151
|
+
if (typeof lazyHelpers === 'function')
|
|
152
|
+
return lazyHelpers();
|
|
153
|
+
return lazyHelpers ?? helpers_1.default;
|
|
154
|
+
},
|
|
180
155
|
});
|
|
181
|
-
const action = answerValue(actionAnswer, 'action');
|
|
182
|
-
if (!action)
|
|
183
|
-
return null;
|
|
184
|
-
return { generator, action };
|
|
185
156
|
};
|
|
186
|
-
exports.pickGeneratorAction = pickGeneratorAction;
|
|
187
157
|
exports.default = prompt;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
const registerTsNode = async (file, error) => {
|
|
27
|
+
if (!/\.ts$/.test(file))
|
|
28
|
+
return;
|
|
29
|
+
try {
|
|
30
|
+
await Promise.resolve().then(() => __importStar(require('ts-node/register/transpile-only')));
|
|
31
|
+
}
|
|
32
|
+
catch (cause) {
|
|
33
|
+
throw new error(file, cause);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
exports.default = registerTsNode;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./logger"), exports);
|
|
18
|
+
__exportStar(require("./prompter"), exports);
|
|
19
|
+
__exportStar(require("./prompter-ns"), exports);
|
|
20
|
+
__exportStar(require("./attributes"), exports);
|
|
21
|
+
__exportStar(require("./helpers-ns"), exports);
|
|
22
|
+
__exportStar(require("./hooks"), exports);
|
|
23
|
+
__exportStar(require("./config"), exports);
|
|
24
|
+
__exportStar(require("./results"), exports);
|
|
25
|
+
__exportStar(require("./params"), exports);
|
|
26
|
+
__exportStar(require("./params-schema"), exports);
|
|
27
|
+
__exportStar(require("./ops"), exports);
|
|
28
|
+
__exportStar(require("./inflection"), exports);
|
|
@@ -2,18 +2,12 @@ import path from 'path';
|
|
|
2
2
|
import fs from 'fs-extra';
|
|
3
3
|
import { ConfigResolver } from './config.js';
|
|
4
4
|
import { ConfigTsNodeError } from './errors.js';
|
|
5
|
-
|
|
5
|
+
import registerTsNode from './ts-node-register.js';
|
|
6
|
+
import { CONFIG_FILES } from './constants/index.js';
|
|
6
7
|
const configResolver = new ConfigResolver(CONFIG_FILES, {
|
|
7
8
|
exists: (f) => fs.pathExists(f),
|
|
8
9
|
load: async (f) => {
|
|
9
|
-
|
|
10
|
-
try {
|
|
11
|
-
await import('ts-node/register/transpile-only');
|
|
12
|
-
}
|
|
13
|
-
catch (cause) {
|
|
14
|
-
throw new ConfigTsNodeError(f, cause);
|
|
15
|
-
}
|
|
16
|
-
}
|
|
10
|
+
await registerTsNode(f, ConfigTsNodeError);
|
|
17
11
|
return await import(f);
|
|
18
12
|
},
|
|
19
13
|
none: (_) => ({}),
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const RUN_SCOPE = '*';
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export const ACTION_HOOK_FILES = ['hooks.ts', 'hooks.js', 'hooks.mjs', 'hooks.cjs'];
|
|
2
|
+
export const LEGACY_HOOK_FILES = [
|
|
3
|
+
'index.js',
|
|
4
|
+
'index.ts',
|
|
5
|
+
'prompt.cjs',
|
|
6
|
+
'prompt.js',
|
|
7
|
+
'prompt.ts',
|
|
8
|
+
];
|
|
9
|
+
export const HOOK_FILES = [...ACTION_HOOK_FILES, ...LEGACY_HOOK_FILES];
|
|
10
|
+
export const LIFECYCLE_METHODS = [
|
|
11
|
+
'beforeAll',
|
|
12
|
+
'beforeEach',
|
|
13
|
+
'afterEach',
|
|
14
|
+
'afterAll',
|
|
15
|
+
];
|
package/dist/esm/src/engine.js
CHANGED
|
@@ -2,15 +2,16 @@ import path from 'path';
|
|
|
2
2
|
import fs from 'fs-extra';
|
|
3
3
|
import context from './context.js';
|
|
4
4
|
import params from './params.js';
|
|
5
|
-
import prompt, { normalizeProjectHooks,
|
|
5
|
+
import prompt, { normalizeProjectHooks, resolveActionHooks } from './prompt.js';
|
|
6
|
+
import { pickGeneratorAction } from './picker.js';
|
|
6
7
|
import { ShowHelpError, KeepGoingError } from './errors.js';
|
|
7
8
|
import { isHelpFlag, printUsage } from './cli-help.js';
|
|
9
|
+
import { RUN_SCOPE } from './constants/index.js';
|
|
8
10
|
const isInteractive = ({ interactive }) => interactive === true
|
|
9
11
|
? true
|
|
10
12
|
: interactive === false
|
|
11
13
|
? false
|
|
12
14
|
: Boolean(process.stdin.isTTY && process.stdout.isTTY);
|
|
13
|
-
const RUN_SCOPE = '*';
|
|
14
15
|
const engine = async (argv, config) => {
|
|
15
16
|
const { cwd, templates, logger } = config;
|
|
16
17
|
let base = Object.assign(await params(config, argv), { cwd });
|
|
@@ -45,7 +46,8 @@ const engine = async (argv, config) => {
|
|
|
45
46
|
const execute = (await import('./execute.js')).execute;
|
|
46
47
|
const render = (await import('./render.js')).render;
|
|
47
48
|
const projectHooks = normalizeProjectHooks(config.hooks, logger);
|
|
48
|
-
|
|
49
|
+
let hookHelpers;
|
|
50
|
+
const getHookHelpers = () => (hookHelpers ?? (hookHelpers = context({ _: base._, ...cleanArgv }, config).h));
|
|
49
51
|
const hookCache = new Map();
|
|
50
52
|
const actionStates = new Map();
|
|
51
53
|
const makeCtx = (action, name, subaction, args) => ({
|
|
@@ -56,7 +58,9 @@ const engine = async (argv, config) => {
|
|
|
56
58
|
...(subaction !== undefined ? { subaction } : {}),
|
|
57
59
|
...(name !== undefined ? { name } : {}),
|
|
58
60
|
args,
|
|
59
|
-
h
|
|
61
|
+
get h() {
|
|
62
|
+
return getHookHelpers();
|
|
63
|
+
},
|
|
60
64
|
logger,
|
|
61
65
|
dry: Boolean(base.dry),
|
|
62
66
|
});
|
|
@@ -95,10 +99,6 @@ const engine = async (argv, config) => {
|
|
|
95
99
|
action,
|
|
96
100
|
...(name !== undefined ? { name } : {}),
|
|
97
101
|
};
|
|
98
|
-
const pairStatus = {
|
|
99
|
-
action,
|
|
100
|
-
...(name !== undefined ? { name } : {}),
|
|
101
|
-
};
|
|
102
102
|
if (!(await fs.pathExists(actionfolder))) {
|
|
103
103
|
const err = new ShowHelpError(`I can't find action '${action}' for generator '${generator}'.
|
|
104
104
|
|
|
@@ -113,7 +113,7 @@ const engine = async (argv, config) => {
|
|
|
113
113
|
if (!keepGoing)
|
|
114
114
|
throw err;
|
|
115
115
|
failures.push({ ...pair, error: err.message });
|
|
116
|
-
statuses.push({ ...
|
|
116
|
+
statuses.push({ ...pair, status: 'failed' });
|
|
117
117
|
continue;
|
|
118
118
|
}
|
|
119
119
|
if (projectBeforeAllError) {
|
|
@@ -164,7 +164,7 @@ const engine = async (argv, config) => {
|
|
|
164
164
|
if (!pairError) {
|
|
165
165
|
try {
|
|
166
166
|
const promptArgs = await prompt(config.createPrompter, actionfolder, hookArgs, {
|
|
167
|
-
h:
|
|
167
|
+
h: getHookHelpers,
|
|
168
168
|
cache: hookCache,
|
|
169
169
|
});
|
|
170
170
|
const args = Object.assign({
|
package/dist/esm/src/errors.js
CHANGED
|
@@ -109,3 +109,18 @@ export class NameFlagRemovedError extends BaseError {
|
|
|
109
109
|
`set HYGEN_LEGACY_NAME=1, or pass legacyNameFlag: true in the RunnerConfig.`, [shown]);
|
|
110
110
|
}
|
|
111
111
|
}
|
|
112
|
+
export class ParamCoercionError extends BaseError {
|
|
113
|
+
constructor(name, type, raw) {
|
|
114
|
+
super(`cannot coerce the value [%s] of the '--%s' parameter to the expected type [%s]`, [String(raw), name, type]);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
export class ParamRequiredError extends BaseError {
|
|
118
|
+
constructor(name) {
|
|
119
|
+
super(`the parameter '--%s' is required but was not supplied`, [name]);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
export class ParamUnknownTypeError extends BaseError {
|
|
123
|
+
constructor(name, type) {
|
|
124
|
+
super(`the parameter '--%s' declares an unknown type [%s]`, [name, type]);
|
|
125
|
+
}
|
|
126
|
+
}
|
package/dist/esm/src/help.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import fs from 'fs';
|
|
2
2
|
import path from 'path';
|
|
3
3
|
import chalk from 'chalk';
|
|
4
|
-
import { DEFAULT_ACTION } from './
|
|
4
|
+
import { DEFAULT_ACTION } from './constants/index.js';
|
|
5
5
|
const readVersion = () => {
|
|
6
6
|
let dir = `${process.platform === 'win32' ? '' : '/'}${/file:\/{2,3}(.+)\/[^/]/.exec(import.meta.url)[1]}`;
|
|
7
7
|
while (true) {
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { coerceParam, promptTypeOf, validateParams } from './registry.js';
|
|
2
|
+
export const promptFromParams = async (schema, createPrompter, args) => {
|
|
3
|
+
const values = {};
|
|
4
|
+
const questions = [];
|
|
5
|
+
for (const descriptor of schema) {
|
|
6
|
+
const raw = args[descriptor.name];
|
|
7
|
+
if (raw !== undefined) {
|
|
8
|
+
values[descriptor.name] = coerceParam(descriptor, raw);
|
|
9
|
+
continue;
|
|
10
|
+
}
|
|
11
|
+
if (!descriptor.required)
|
|
12
|
+
continue;
|
|
13
|
+
questions.push({
|
|
14
|
+
type: promptTypeOf(descriptor),
|
|
15
|
+
name: descriptor.name,
|
|
16
|
+
message: descriptor.message ?? `Enter a value for ${descriptor.name}:`,
|
|
17
|
+
...(descriptor.default !== undefined ? { initial: descriptor.default } : {}),
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
if (questions.length > 0) {
|
|
21
|
+
const prompter = createPrompter();
|
|
22
|
+
const answers = await prompter.prompt(questions);
|
|
23
|
+
const answerMap = (answers ?? {});
|
|
24
|
+
for (const question of questions) {
|
|
25
|
+
const answered = answerMap[question.name];
|
|
26
|
+
values[question.name] = answered !== undefined ? answered : question.initial;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return validateParams(schema, values);
|
|
30
|
+
};
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { ParamCoercionError, ParamRequiredError, ParamUnknownTypeError } from '../errors.js';
|
|
2
|
+
const BUILTIN_TYPES = {
|
|
3
|
+
string: {
|
|
4
|
+
coerce: (raw) => {
|
|
5
|
+
if (typeof raw === 'string')
|
|
6
|
+
return raw;
|
|
7
|
+
if (typeof raw === 'number' || typeof raw === 'boolean')
|
|
8
|
+
return String(raw);
|
|
9
|
+
throw new Error(String(raw));
|
|
10
|
+
},
|
|
11
|
+
check: (value) => typeof value === 'string',
|
|
12
|
+
promptType: 'input',
|
|
13
|
+
},
|
|
14
|
+
number: {
|
|
15
|
+
coerce: (raw) => {
|
|
16
|
+
if (typeof raw === 'number')
|
|
17
|
+
return raw;
|
|
18
|
+
if (typeof raw === 'string' && raw.trim() !== '' && !Number.isNaN(Number(raw))) {
|
|
19
|
+
return Number(raw);
|
|
20
|
+
}
|
|
21
|
+
throw new Error(String(raw));
|
|
22
|
+
},
|
|
23
|
+
check: (value) => typeof value === 'number' && !Number.isNaN(value),
|
|
24
|
+
promptType: 'numeral',
|
|
25
|
+
},
|
|
26
|
+
boolean: {
|
|
27
|
+
coerce: (raw) => {
|
|
28
|
+
if (typeof raw === 'boolean')
|
|
29
|
+
return raw;
|
|
30
|
+
if (raw === 'true')
|
|
31
|
+
return true;
|
|
32
|
+
if (raw === 'false')
|
|
33
|
+
return false;
|
|
34
|
+
throw new Error(String(raw));
|
|
35
|
+
},
|
|
36
|
+
check: (value) => typeof value === 'boolean',
|
|
37
|
+
promptType: 'confirm',
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
export const resolveType = (descriptor) => {
|
|
41
|
+
const { name, type } = descriptor;
|
|
42
|
+
if (typeof type === 'function')
|
|
43
|
+
return type;
|
|
44
|
+
const def = BUILTIN_TYPES[type];
|
|
45
|
+
if (!def)
|
|
46
|
+
throw new ParamUnknownTypeError(name, String(type));
|
|
47
|
+
return def.coerce;
|
|
48
|
+
};
|
|
49
|
+
export const promptTypeOf = (descriptor) => {
|
|
50
|
+
const { type } = descriptor;
|
|
51
|
+
if (typeof type === 'function')
|
|
52
|
+
return 'input';
|
|
53
|
+
return BUILTIN_TYPES[type]?.promptType ?? 'input';
|
|
54
|
+
};
|
|
55
|
+
export const coerceParam = (descriptor, raw) => {
|
|
56
|
+
try {
|
|
57
|
+
return resolveType(descriptor)(raw);
|
|
58
|
+
}
|
|
59
|
+
catch (cause) {
|
|
60
|
+
const typeName = typeof descriptor.type === 'function' ? 'function' : descriptor.type;
|
|
61
|
+
throw new ParamCoercionError(descriptor.name, typeName, raw);
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
export const checkParam = (descriptor, value) => {
|
|
65
|
+
const { type } = descriptor;
|
|
66
|
+
if (typeof type === 'function')
|
|
67
|
+
return true;
|
|
68
|
+
return BUILTIN_TYPES[type]?.check(value) ?? true;
|
|
69
|
+
};
|
|
70
|
+
export const validateParams = (schema, values) => {
|
|
71
|
+
for (const descriptor of schema) {
|
|
72
|
+
const value = values[descriptor.name];
|
|
73
|
+
if (value === undefined) {
|
|
74
|
+
if (descriptor.required)
|
|
75
|
+
throw new ParamRequiredError(descriptor.name);
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
if (!checkParam(descriptor, value)) {
|
|
79
|
+
const typeName = typeof descriptor.type === 'function' ? 'function' : descriptor.type;
|
|
80
|
+
throw new ParamCoercionError(descriptor.name, typeName, value);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return values;
|
|
84
|
+
};
|
package/dist/esm/src/params.js
CHANGED
|
@@ -2,7 +2,7 @@ import path from 'path';
|
|
|
2
2
|
import yargs from 'yargs-parser';
|
|
3
3
|
import fs from 'fs-extra';
|
|
4
4
|
import { CliArgumentError, NameFlagRemovedError, TemplateNotFoundError } from './errors.js';
|
|
5
|
-
|
|
5
|
+
import { DEFAULT_ACTION } from './constants/index.js';
|
|
6
6
|
const splitList = (value) => value ? value.split(',') : undefined;
|
|
7
7
|
const resolvePositionals = async (templates, args) => {
|
|
8
8
|
let [generator, action, name] = args;
|
|
@@ -34,9 +34,26 @@ const resolvePositionals = async (templates, args) => {
|
|
|
34
34
|
}
|
|
35
35
|
return { generator, actions: actionList, names: nameList };
|
|
36
36
|
};
|
|
37
|
-
const params = async ({ templates, legacyNameFlag }, externalArgv) => {
|
|
37
|
+
const params = async ({ templates, legacyNameFlag, argCase: argCaseOption }, externalArgv) => {
|
|
38
38
|
const argv = yargs(externalArgv);
|
|
39
39
|
const { _, ...cleanArgv } = argv;
|
|
40
|
+
const normalizeKey = (key) => key
|
|
41
|
+
.replace(/([a-z0-9])([A-Z])/g, '$1_$2')
|
|
42
|
+
.replace(/[-\s]+/g, '_')
|
|
43
|
+
.toUpperCase();
|
|
44
|
+
const RESERVED_KEYS = new Set(['name', 'generator', 'action', 'ts']);
|
|
45
|
+
const argCase = argCaseOption ?? 'keep';
|
|
46
|
+
if (argCase === 'upper-underscore') {
|
|
47
|
+
for (const key of Object.keys(cleanArgv)) {
|
|
48
|
+
if (RESERVED_KEYS.has(key))
|
|
49
|
+
continue;
|
|
50
|
+
const normalized = normalizeKey(key);
|
|
51
|
+
if (normalized !== key) {
|
|
52
|
+
cleanArgv[normalized] = cleanArgv[key];
|
|
53
|
+
delete cleanArgv[key];
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
40
57
|
const legacyGate = process.env.HYGEN_LEGACY_NAME === '1' || legacyNameFlag === true;
|
|
41
58
|
if (Array.isArray(cleanArgv.name) && !legacyGate) {
|
|
42
59
|
throw new CliArgumentError("repeating the '--name' flag is not supported; use a comma-separated positional name list instead (e.g. 'hygen <gen> <action> a,b').");
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import { pickableGenerators, generatorActions } from './help.js';
|
|
3
|
+
const answerValue = (answer, name) => {
|
|
4
|
+
const value = answer[name];
|
|
5
|
+
return typeof value === 'string' && value.length > 0 ? value : undefined;
|
|
6
|
+
};
|
|
7
|
+
export const pickGeneratorAction = async (config) => {
|
|
8
|
+
const { templates, logger } = config;
|
|
9
|
+
if (!templates || !fs.existsSync(templates)) {
|
|
10
|
+
logger.log(`No generators found.
|
|
11
|
+
|
|
12
|
+
The interactive picker needs a _templates folder here, or anywhere up the folder tree.
|
|
13
|
+
Create one with: $ hygen init self
|
|
14
|
+
`);
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
const generators = pickableGenerators(templates);
|
|
18
|
+
if (generators.length === 0) {
|
|
19
|
+
logger.log(`No generators found.
|
|
20
|
+
|
|
21
|
+
Every top-level folder in _templates should hold at least one action subfolder.
|
|
22
|
+
Create one with: $ hygen with-prompt new my-generator
|
|
23
|
+
`);
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
const prompter = config.createPrompter();
|
|
27
|
+
const generatorAnswer = await prompter.prompt({
|
|
28
|
+
type: 'select',
|
|
29
|
+
name: 'generator',
|
|
30
|
+
message: 'Select a generator:',
|
|
31
|
+
choices: generators,
|
|
32
|
+
});
|
|
33
|
+
const generator = answerValue(generatorAnswer, 'generator');
|
|
34
|
+
if (!generator)
|
|
35
|
+
return null;
|
|
36
|
+
const actions = generatorActions(templates, generator);
|
|
37
|
+
const actionPrompter = config.createPrompter();
|
|
38
|
+
const actionAnswer = await actionPrompter.prompt({
|
|
39
|
+
type: 'select',
|
|
40
|
+
name: 'action',
|
|
41
|
+
message: `Select an action for ${generator}:`,
|
|
42
|
+
choices: actions,
|
|
43
|
+
});
|
|
44
|
+
const action = answerValue(actionAnswer, 'action');
|
|
45
|
+
if (!action)
|
|
46
|
+
return null;
|
|
47
|
+
return { generator, action };
|
|
48
|
+
};
|