@biorate/hygen 0.4.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 +8 -11
- 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 +31 -68
- 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 +4 -7
- 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 +26 -63
- 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 +3 -17
- 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/README.md
CHANGED
|
@@ -106,20 +106,20 @@ For the full API reference see the [typedoc](https://biorate.github.io/core/modu
|
|
|
106
106
|
|
|
107
107
|
The complete user guide lives in the package's local `docs/` pages. README is for the overview, quick start, and embedding; `docs/` is the full guide; typedoc is the generated API reference.
|
|
108
108
|
|
|
109
|
-
| Page | Contents
|
|
110
|
-
| ---------------------------------------- |
|
|
111
|
-
| [quick-start](./docs/quick-start.md) | CLI installation, `init`, generator creation, `--version`.
|
|
112
|
-
| [templates](./docs/templates.md) | Template & frontmatter reference: `add`, `inject`, `sh`, anchors, `replace`, escaping.
|
|
113
|
-
| [generators](./docs/generators.md) | Building and organizing reusable generators.
|
|
114
|
-
| [prompts](./docs/prompts.md) | Prompt hooks reference: file names, `params`/`prompt`/array forms, filtering, TS.
|
|
115
|
-
| [extensibility](./docs/extensibility.md) | Extending hygen via the project config file: helpers, logger, template location, shell executor. |
|
|
116
|
-
| [packages](./docs/packages.md) | Sharing compiled generators as publishable packages (`hygen-add`).
|
|
117
|
-
| [create](./docs/create.md) | Turning existing code into a generator (`hygen-create`).
|
|
118
|
-
| [faq](./docs/faq.md) | Why hygen and its position among generator toolkits.
|
|
119
|
-
| [express](./docs/express.md) | Express.js app generation pattern.
|
|
120
|
-
| [redux](./docs/redux.md) | Redux (ducks) app generation pattern.
|
|
121
|
-
| [react-native](./docs/react-native.md) | React Native app generation pattern.
|
|
122
|
-
| [standalone](./docs/standalone.md) | Standalone binary distribution.
|
|
109
|
+
| Page | Contents |
|
|
110
|
+
| ---------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
111
|
+
| [quick-start](./docs/quick-start.md) | CLI installation, `init`, generator creation, `--version`. |
|
|
112
|
+
| [templates](./docs/templates.md) | Template & frontmatter reference: `add`, `inject`, `sh`, anchors, `replace`, escaping. |
|
|
113
|
+
| [generators](./docs/generators.md) | Building and organizing reusable generators. |
|
|
114
|
+
| [prompts](./docs/prompts.md) | Prompt hooks reference: file names, `params`/`prompt`/array forms, filtering, TS. |
|
|
115
|
+
| [extensibility](./docs/extensibility.md) | Extending hygen via the project config file: helpers, logger, template location, shell executor; action hook classes, the declarative `params` schema and `argCase` flag normalization. |
|
|
116
|
+
| [packages](./docs/packages.md) | Sharing compiled generators as publishable packages (`hygen-add`). |
|
|
117
|
+
| [create](./docs/create.md) | Turning existing code into a generator (`hygen-create`). |
|
|
118
|
+
| [faq](./docs/faq.md) | Why hygen and its position among generator toolkits. |
|
|
119
|
+
| [express](./docs/express.md) | Express.js app generation pattern. |
|
|
120
|
+
| [redux](./docs/redux.md) | Redux (ducks) app generation pattern. |
|
|
121
|
+
| [react-native](./docs/react-native.md) | React Native app generation pattern. |
|
|
122
|
+
| [standalone](./docs/standalone.md) | Standalone binary distribution. |
|
|
123
123
|
|
|
124
124
|
## Differences from upstream
|
|
125
125
|
|
|
@@ -26,23 +26,16 @@ 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.CONFIG_FILES = void 0;
|
|
30
29
|
const path_1 = __importDefault(require("path"));
|
|
31
30
|
const fs_extra_1 = __importDefault(require("fs-extra"));
|
|
32
31
|
const config_1 = require("./config");
|
|
33
32
|
const errors_1 = require("./errors");
|
|
34
|
-
|
|
35
|
-
const
|
|
33
|
+
const ts_node_register_1 = __importDefault(require("./ts-node-register"));
|
|
34
|
+
const constants_1 = require("./constants");
|
|
35
|
+
const configResolver = new config_1.ConfigResolver(constants_1.CONFIG_FILES, {
|
|
36
36
|
exists: (f) => fs_extra_1.default.pathExists(f),
|
|
37
37
|
load: async (f) => {
|
|
38
|
-
|
|
39
|
-
try {
|
|
40
|
-
await Promise.resolve().then(() => __importStar(require('ts-node/register/transpile-only')));
|
|
41
|
-
}
|
|
42
|
-
catch (cause) {
|
|
43
|
-
throw new errors_1.ConfigTsNodeError(f, cause);
|
|
44
|
-
}
|
|
45
|
-
}
|
|
38
|
+
await (0, ts_node_register_1.default)(f, errors_1.ConfigTsNodeError);
|
|
46
39
|
return await Promise.resolve(`${f}`).then(s => __importStar(require(s)));
|
|
47
40
|
},
|
|
48
41
|
none: (_) => ({}),
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LIFECYCLE_METHODS = exports.HOOK_FILES = exports.LEGACY_HOOK_FILES = exports.ACTION_HOOK_FILES = void 0;
|
|
4
|
+
exports.ACTION_HOOK_FILES = ['hooks.ts', 'hooks.js', 'hooks.mjs', 'hooks.cjs'];
|
|
5
|
+
exports.LEGACY_HOOK_FILES = [
|
|
6
|
+
'index.js',
|
|
7
|
+
'index.ts',
|
|
8
|
+
'prompt.cjs',
|
|
9
|
+
'prompt.js',
|
|
10
|
+
'prompt.ts',
|
|
11
|
+
];
|
|
12
|
+
exports.HOOK_FILES = [...exports.ACTION_HOOK_FILES, ...exports.LEGACY_HOOK_FILES];
|
|
13
|
+
exports.LIFECYCLE_METHODS = [
|
|
14
|
+
'beforeAll',
|
|
15
|
+
'beforeEach',
|
|
16
|
+
'afterEach',
|
|
17
|
+
'afterAll',
|
|
18
|
+
];
|
|
@@ -0,0 +1,19 @@
|
|
|
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("./hooks"), exports);
|
|
18
|
+
__exportStar(require("./config"), exports);
|
|
19
|
+
__exportStar(require("./engine"), exports);
|
package/dist/cjs/src/engine.js
CHANGED
|
@@ -32,16 +32,17 @@ const fs_extra_1 = __importDefault(require("fs-extra"));
|
|
|
32
32
|
const context_1 = __importDefault(require("./context"));
|
|
33
33
|
const params_1 = __importDefault(require("./params"));
|
|
34
34
|
const prompt_1 = __importStar(require("./prompt"));
|
|
35
|
+
const picker_1 = require("./picker");
|
|
35
36
|
const errors_1 = require("./errors");
|
|
36
37
|
Object.defineProperty(exports, "ShowHelpError", { enumerable: true, get: function () { return errors_1.ShowHelpError; } });
|
|
37
38
|
Object.defineProperty(exports, "KeepGoingError", { enumerable: true, get: function () { return errors_1.KeepGoingError; } });
|
|
38
39
|
const cli_help_1 = require("./cli-help");
|
|
40
|
+
const constants_1 = require("./constants");
|
|
39
41
|
const isInteractive = ({ interactive }) => interactive === true
|
|
40
42
|
? true
|
|
41
43
|
: interactive === false
|
|
42
44
|
? false
|
|
43
45
|
: Boolean(process.stdin.isTTY && process.stdout.isTTY);
|
|
44
|
-
const RUN_SCOPE = '*';
|
|
45
46
|
const engine = async (argv, config) => {
|
|
46
47
|
const { cwd, templates, logger } = config;
|
|
47
48
|
let base = Object.assign(await (0, params_1.default)(config, argv), { cwd });
|
|
@@ -50,7 +51,7 @@ const engine = async (argv, config) => {
|
|
|
50
51
|
(0, cli_help_1.printUsage)(logger);
|
|
51
52
|
}
|
|
52
53
|
if (!generator && isInteractive(config) && templates && fs_extra_1.default.pathExistsSync(templates)) {
|
|
53
|
-
const picked = await (0,
|
|
54
|
+
const picked = await (0, picker_1.pickGeneratorAction)(config);
|
|
54
55
|
if (picked) {
|
|
55
56
|
base = Object.assign(await (0, params_1.default)(config, [picked.generator, picked.action, ...argv]), {
|
|
56
57
|
cwd,
|
|
@@ -112,7 +113,7 @@ const engine = async (argv, config) => {
|
|
|
112
113
|
};
|
|
113
114
|
let projectBeforeAllError;
|
|
114
115
|
try {
|
|
115
|
-
await projectHooks.beforeAll?.(makeCtx(RUN_SCOPE, undefined, undefined, { ...cleanArgv }));
|
|
116
|
+
await projectHooks.beforeAll?.(makeCtx(constants_1.RUN_SCOPE, undefined, undefined, { ...cleanArgv }));
|
|
116
117
|
}
|
|
117
118
|
catch (err) {
|
|
118
119
|
projectBeforeAllError = err;
|
|
@@ -129,10 +130,6 @@ const engine = async (argv, config) => {
|
|
|
129
130
|
action,
|
|
130
131
|
...(name !== undefined ? { name } : {}),
|
|
131
132
|
};
|
|
132
|
-
const pairStatus = {
|
|
133
|
-
action,
|
|
134
|
-
...(name !== undefined ? { name } : {}),
|
|
135
|
-
};
|
|
136
133
|
if (!(await fs_extra_1.default.pathExists(actionfolder))) {
|
|
137
134
|
const err = new errors_1.ShowHelpError(`I can't find action '${action}' for generator '${generator}'.
|
|
138
135
|
|
|
@@ -147,7 +144,7 @@ const engine = async (argv, config) => {
|
|
|
147
144
|
if (!keepGoing)
|
|
148
145
|
throw err;
|
|
149
146
|
failures.push({ ...pair, error: err.message });
|
|
150
|
-
statuses.push({ ...
|
|
147
|
+
statuses.push({ ...pair, status: 'failed' });
|
|
151
148
|
continue;
|
|
152
149
|
}
|
|
153
150
|
if (projectBeforeAllError) {
|
|
@@ -278,7 +275,7 @@ const engine = async (argv, config) => {
|
|
|
278
275
|
}
|
|
279
276
|
}
|
|
280
277
|
try {
|
|
281
|
-
await projectHooks.afterAll?.(makeCtx(RUN_SCOPE, undefined, undefined, { ...cleanArgv }), results);
|
|
278
|
+
await projectHooks.afterAll?.(makeCtx(constants_1.RUN_SCOPE, undefined, undefined, { ...cleanArgv }), results);
|
|
282
279
|
}
|
|
283
280
|
catch (err) {
|
|
284
281
|
afterAllErrors.push(err);
|
|
@@ -295,8 +292,8 @@ const engine = async (argv, config) => {
|
|
|
295
292
|
if (!keepGoing)
|
|
296
293
|
throw new errors_1.ShowHelpError(`afterAll failed: ${afterAllErrors[0].message}`);
|
|
297
294
|
for (const err of afterAllErrors) {
|
|
298
|
-
failures.push({ action: RUN_SCOPE, error: err.message });
|
|
299
|
-
statuses.push({ action: RUN_SCOPE, status: 'failed' });
|
|
295
|
+
failures.push({ action: constants_1.RUN_SCOPE, error: err.message });
|
|
296
|
+
statuses.push({ action: constants_1.RUN_SCOPE, status: 'failed' });
|
|
300
297
|
}
|
|
301
298
|
}
|
|
302
299
|
if (failures.length > 0) {
|
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.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;
|
|
3
|
+
exports.ParamUnknownTypeError = exports.ParamRequiredError = exports.ParamCoercionError = 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) {
|
|
@@ -129,3 +129,21 @@ class NameFlagRemovedError extends errors_1.BaseError {
|
|
|
129
129
|
}
|
|
130
130
|
}
|
|
131
131
|
exports.NameFlagRemovedError = NameFlagRemovedError;
|
|
132
|
+
class ParamCoercionError extends errors_1.BaseError {
|
|
133
|
+
constructor(name, type, raw) {
|
|
134
|
+
super(`cannot coerce the value [%s] of the '--%s' parameter to the expected type [%s]`, [String(raw), name, type]);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
exports.ParamCoercionError = ParamCoercionError;
|
|
138
|
+
class ParamRequiredError extends errors_1.BaseError {
|
|
139
|
+
constructor(name) {
|
|
140
|
+
super(`the parameter '--%s' is required but was not supplied`, [name]);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
exports.ParamRequiredError = ParamRequiredError;
|
|
144
|
+
class ParamUnknownTypeError extends errors_1.BaseError {
|
|
145
|
+
constructor(name, type) {
|
|
146
|
+
super(`the parameter '--%s' declares an unknown type [%s]`, [name, type]);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
exports.ParamUnknownTypeError = ParamUnknownTypeError;
|
package/dist/cjs/src/help.js
CHANGED
|
@@ -7,7 +7,7 @@ exports.isVersionFlag = exports.VERSION = exports.printHelp = exports.generatorA
|
|
|
7
7
|
const fs_1 = __importDefault(require("fs"));
|
|
8
8
|
const path_1 = __importDefault(require("path"));
|
|
9
9
|
const chalk_1 = __importDefault(require("chalk"));
|
|
10
|
-
const
|
|
10
|
+
const constants_1 = require("./constants");
|
|
11
11
|
const readVersion = () => {
|
|
12
12
|
let dir = __dirname;
|
|
13
13
|
while (true) {
|
|
@@ -80,8 +80,8 @@ const printHelp = (templates, logger) => {
|
|
|
80
80
|
return;
|
|
81
81
|
}
|
|
82
82
|
Object.entries(availableActions(templates)).forEach(([k, v]) => {
|
|
83
|
-
logger.log(`${chalk_1.default.bold(k)}: ${v.find((a) => a ===
|
|
84
|
-
.filter((a) => a !==
|
|
83
|
+
logger.log(`${chalk_1.default.bold(k)}: ${v.find((a) => a === constants_1.DEFAULT_ACTION) ? `${k}${v.length > 1 ? ',' : ''} ` : ''}${v
|
|
84
|
+
.filter((a) => a !== constants_1.DEFAULT_ACTION)
|
|
85
85
|
.map((a) => `${k} ${a}`)
|
|
86
86
|
.join(', ')}`);
|
|
87
87
|
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.promptFromParams = exports.validateParams = exports.resolveType = exports.promptTypeOf = exports.checkParam = exports.coerceParam = void 0;
|
|
4
|
+
var registry_1 = require("./registry");
|
|
5
|
+
Object.defineProperty(exports, "coerceParam", { enumerable: true, get: function () { return registry_1.coerceParam; } });
|
|
6
|
+
Object.defineProperty(exports, "checkParam", { enumerable: true, get: function () { return registry_1.checkParam; } });
|
|
7
|
+
Object.defineProperty(exports, "promptTypeOf", { enumerable: true, get: function () { return registry_1.promptTypeOf; } });
|
|
8
|
+
Object.defineProperty(exports, "resolveType", { enumerable: true, get: function () { return registry_1.resolveType; } });
|
|
9
|
+
Object.defineProperty(exports, "validateParams", { enumerable: true, get: function () { return registry_1.validateParams; } });
|
|
10
|
+
var prompts_1 = require("./prompts");
|
|
11
|
+
Object.defineProperty(exports, "promptFromParams", { enumerable: true, get: function () { return prompts_1.promptFromParams; } });
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.promptFromParams = void 0;
|
|
4
|
+
const registry_1 = require("./registry");
|
|
5
|
+
const promptFromParams = async (schema, createPrompter, args) => {
|
|
6
|
+
const values = {};
|
|
7
|
+
const questions = [];
|
|
8
|
+
for (const descriptor of schema) {
|
|
9
|
+
const raw = args[descriptor.name];
|
|
10
|
+
if (raw !== undefined) {
|
|
11
|
+
values[descriptor.name] = (0, registry_1.coerceParam)(descriptor, raw);
|
|
12
|
+
continue;
|
|
13
|
+
}
|
|
14
|
+
if (!descriptor.required)
|
|
15
|
+
continue;
|
|
16
|
+
questions.push({
|
|
17
|
+
type: (0, registry_1.promptTypeOf)(descriptor),
|
|
18
|
+
name: descriptor.name,
|
|
19
|
+
message: descriptor.message ?? `Enter a value for ${descriptor.name}:`,
|
|
20
|
+
...(descriptor.default !== undefined ? { initial: descriptor.default } : {}),
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
if (questions.length > 0) {
|
|
24
|
+
const prompter = createPrompter();
|
|
25
|
+
const answers = await prompter.prompt(questions);
|
|
26
|
+
const answerMap = (answers ?? {});
|
|
27
|
+
for (const question of questions) {
|
|
28
|
+
const answered = answerMap[question.name];
|
|
29
|
+
values[question.name] = answered !== undefined ? answered : question.initial;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return (0, registry_1.validateParams)(schema, values);
|
|
33
|
+
};
|
|
34
|
+
exports.promptFromParams = promptFromParams;
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validateParams = exports.checkParam = exports.coerceParam = exports.promptTypeOf = exports.resolveType = void 0;
|
|
4
|
+
const errors_1 = require("../errors");
|
|
5
|
+
const BUILTIN_TYPES = {
|
|
6
|
+
string: {
|
|
7
|
+
coerce: (raw) => {
|
|
8
|
+
if (typeof raw === 'string')
|
|
9
|
+
return raw;
|
|
10
|
+
if (typeof raw === 'number' || typeof raw === 'boolean')
|
|
11
|
+
return String(raw);
|
|
12
|
+
throw new Error(String(raw));
|
|
13
|
+
},
|
|
14
|
+
check: (value) => typeof value === 'string',
|
|
15
|
+
promptType: 'input',
|
|
16
|
+
},
|
|
17
|
+
number: {
|
|
18
|
+
coerce: (raw) => {
|
|
19
|
+
if (typeof raw === 'number')
|
|
20
|
+
return raw;
|
|
21
|
+
if (typeof raw === 'string' && raw.trim() !== '' && !Number.isNaN(Number(raw))) {
|
|
22
|
+
return Number(raw);
|
|
23
|
+
}
|
|
24
|
+
throw new Error(String(raw));
|
|
25
|
+
},
|
|
26
|
+
check: (value) => typeof value === 'number' && !Number.isNaN(value),
|
|
27
|
+
promptType: 'numeral',
|
|
28
|
+
},
|
|
29
|
+
boolean: {
|
|
30
|
+
coerce: (raw) => {
|
|
31
|
+
if (typeof raw === 'boolean')
|
|
32
|
+
return raw;
|
|
33
|
+
if (raw === 'true')
|
|
34
|
+
return true;
|
|
35
|
+
if (raw === 'false')
|
|
36
|
+
return false;
|
|
37
|
+
throw new Error(String(raw));
|
|
38
|
+
},
|
|
39
|
+
check: (value) => typeof value === 'boolean',
|
|
40
|
+
promptType: 'confirm',
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
const resolveType = (descriptor) => {
|
|
44
|
+
const { name, type } = descriptor;
|
|
45
|
+
if (typeof type === 'function')
|
|
46
|
+
return type;
|
|
47
|
+
const def = BUILTIN_TYPES[type];
|
|
48
|
+
if (!def)
|
|
49
|
+
throw new errors_1.ParamUnknownTypeError(name, String(type));
|
|
50
|
+
return def.coerce;
|
|
51
|
+
};
|
|
52
|
+
exports.resolveType = resolveType;
|
|
53
|
+
const promptTypeOf = (descriptor) => {
|
|
54
|
+
const { type } = descriptor;
|
|
55
|
+
if (typeof type === 'function')
|
|
56
|
+
return 'input';
|
|
57
|
+
return BUILTIN_TYPES[type]?.promptType ?? 'input';
|
|
58
|
+
};
|
|
59
|
+
exports.promptTypeOf = promptTypeOf;
|
|
60
|
+
const coerceParam = (descriptor, raw) => {
|
|
61
|
+
try {
|
|
62
|
+
return (0, exports.resolveType)(descriptor)(raw);
|
|
63
|
+
}
|
|
64
|
+
catch (cause) {
|
|
65
|
+
const typeName = typeof descriptor.type === 'function' ? 'function' : descriptor.type;
|
|
66
|
+
throw new errors_1.ParamCoercionError(descriptor.name, typeName, raw);
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
exports.coerceParam = coerceParam;
|
|
70
|
+
const checkParam = (descriptor, value) => {
|
|
71
|
+
const { type } = descriptor;
|
|
72
|
+
if (typeof type === 'function')
|
|
73
|
+
return true;
|
|
74
|
+
return BUILTIN_TYPES[type]?.check(value) ?? true;
|
|
75
|
+
};
|
|
76
|
+
exports.checkParam = checkParam;
|
|
77
|
+
const validateParams = (schema, values) => {
|
|
78
|
+
for (const descriptor of schema) {
|
|
79
|
+
const value = values[descriptor.name];
|
|
80
|
+
if (value === undefined) {
|
|
81
|
+
if (descriptor.required)
|
|
82
|
+
throw new errors_1.ParamRequiredError(descriptor.name);
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
if (!(0, exports.checkParam)(descriptor, value)) {
|
|
86
|
+
const typeName = typeof descriptor.type === 'function' ? 'function' : descriptor.type;
|
|
87
|
+
throw new errors_1.ParamCoercionError(descriptor.name, typeName, value);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return values;
|
|
91
|
+
};
|
|
92
|
+
exports.validateParams = validateParams;
|
package/dist/cjs/src/params.js
CHANGED
|
@@ -3,12 +3,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.DEFAULT_ACTION = void 0;
|
|
7
6
|
const path_1 = __importDefault(require("path"));
|
|
8
7
|
const yargs_parser_1 = __importDefault(require("yargs-parser"));
|
|
9
8
|
const fs_extra_1 = __importDefault(require("fs-extra"));
|
|
10
9
|
const errors_1 = require("./errors");
|
|
11
|
-
|
|
10
|
+
const constants_1 = require("./constants");
|
|
12
11
|
const splitList = (value) => value ? value.split(',') : undefined;
|
|
13
12
|
const resolvePositionals = async (templates, args) => {
|
|
14
13
|
let [generator, action, name] = args;
|
|
@@ -34,15 +33,32 @@ const resolvePositionals = async (templates, args) => {
|
|
|
34
33
|
}
|
|
35
34
|
}
|
|
36
35
|
if (generator &&
|
|
37
|
-
(await fs_extra_1.default.pathExists(path_1.default.join(templates, generator,
|
|
36
|
+
(await fs_extra_1.default.pathExists(path_1.default.join(templates, generator, constants_1.DEFAULT_ACTION)))) {
|
|
38
37
|
[generator, name] = args;
|
|
39
|
-
return { generator, actions: [
|
|
38
|
+
return { generator, actions: [constants_1.DEFAULT_ACTION], names: splitList(name) };
|
|
40
39
|
}
|
|
41
40
|
return { generator, actions: actionList, names: nameList };
|
|
42
41
|
};
|
|
43
|
-
const params = async ({ templates, legacyNameFlag }, externalArgv) => {
|
|
42
|
+
const params = async ({ templates, legacyNameFlag, argCase: argCaseOption }, externalArgv) => {
|
|
44
43
|
const argv = (0, yargs_parser_1.default)(externalArgv);
|
|
45
44
|
const { _, ...cleanArgv } = argv;
|
|
45
|
+
const normalizeKey = (key) => key
|
|
46
|
+
.replace(/([a-z0-9])([A-Z])/g, '$1_$2')
|
|
47
|
+
.replace(/[-\s]+/g, '_')
|
|
48
|
+
.toUpperCase();
|
|
49
|
+
const RESERVED_KEYS = new Set(['name', 'generator', 'action', 'ts']);
|
|
50
|
+
const argCase = argCaseOption ?? 'keep';
|
|
51
|
+
if (argCase === 'upper-underscore') {
|
|
52
|
+
for (const key of Object.keys(cleanArgv)) {
|
|
53
|
+
if (RESERVED_KEYS.has(key))
|
|
54
|
+
continue;
|
|
55
|
+
const normalized = normalizeKey(key);
|
|
56
|
+
if (normalized !== key) {
|
|
57
|
+
cleanArgv[normalized] = cleanArgv[key];
|
|
58
|
+
delete cleanArgv[key];
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
46
62
|
const legacyGate = process.env.HYGEN_LEGACY_NAME === '1' || legacyNameFlag === true;
|
|
47
63
|
if (Array.isArray(cleanArgv.name) && !legacyGate) {
|
|
48
64
|
throw new errors_1.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,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.pickGeneratorAction = void 0;
|
|
7
|
+
const fs_1 = __importDefault(require("fs"));
|
|
8
|
+
const help_1 = require("./help");
|
|
9
|
+
const answerValue = (answer, name) => {
|
|
10
|
+
const value = answer[name];
|
|
11
|
+
return typeof value === 'string' && value.length > 0 ? value : undefined;
|
|
12
|
+
};
|
|
13
|
+
const pickGeneratorAction = async (config) => {
|
|
14
|
+
const { templates, logger } = config;
|
|
15
|
+
if (!templates || !fs_1.default.existsSync(templates)) {
|
|
16
|
+
logger.log(`No generators found.
|
|
17
|
+
|
|
18
|
+
The interactive picker needs a _templates folder here, or anywhere up the folder tree.
|
|
19
|
+
Create one with: $ hygen init self
|
|
20
|
+
`);
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
const generators = (0, help_1.pickableGenerators)(templates);
|
|
24
|
+
if (generators.length === 0) {
|
|
25
|
+
logger.log(`No generators found.
|
|
26
|
+
|
|
27
|
+
Every top-level folder in _templates should hold at least one action subfolder.
|
|
28
|
+
Create one with: $ hygen with-prompt new my-generator
|
|
29
|
+
`);
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
const prompter = config.createPrompter();
|
|
33
|
+
const generatorAnswer = await prompter.prompt({
|
|
34
|
+
type: 'select',
|
|
35
|
+
name: 'generator',
|
|
36
|
+
message: 'Select a generator:',
|
|
37
|
+
choices: generators,
|
|
38
|
+
});
|
|
39
|
+
const generator = answerValue(generatorAnswer, 'generator');
|
|
40
|
+
if (!generator)
|
|
41
|
+
return null;
|
|
42
|
+
const actions = (0, help_1.generatorActions)(templates, generator);
|
|
43
|
+
const actionPrompter = config.createPrompter();
|
|
44
|
+
const actionAnswer = await actionPrompter.prompt({
|
|
45
|
+
type: 'select',
|
|
46
|
+
name: 'action',
|
|
47
|
+
message: `Select an action for ${generator}:`,
|
|
48
|
+
choices: actions,
|
|
49
|
+
});
|
|
50
|
+
const action = answerValue(actionAnswer, 'action');
|
|
51
|
+
if (!action)
|
|
52
|
+
return null;
|
|
53
|
+
return { generator, action };
|
|
54
|
+
};
|
|
55
|
+
exports.pickGeneratorAction = pickGeneratorAction;
|
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,6 +135,9 @@ 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,51 +154,4 @@ const prompt = async (createPrompter, actionfolder, args, options = {}) => {
|
|
|
144
154
|
},
|
|
145
155
|
});
|
|
146
156
|
};
|
|
147
|
-
const answerValue = (answer, name) => {
|
|
148
|
-
const value = answer[name];
|
|
149
|
-
return typeof value === 'string' && value.length > 0 ? value : undefined;
|
|
150
|
-
};
|
|
151
|
-
const pickGeneratorAction = async (config) => {
|
|
152
|
-
const { templates, logger } = config;
|
|
153
|
-
if (!templates || !fs_1.default.existsSync(templates)) {
|
|
154
|
-
logger.log(`No generators found.
|
|
155
|
-
|
|
156
|
-
The interactive picker needs a _templates folder here, or anywhere up the folder tree.
|
|
157
|
-
Create one with: $ hygen init self
|
|
158
|
-
`);
|
|
159
|
-
return null;
|
|
160
|
-
}
|
|
161
|
-
const generators = (0, help_1.pickableGenerators)(templates);
|
|
162
|
-
if (generators.length === 0) {
|
|
163
|
-
logger.log(`No generators found.
|
|
164
|
-
|
|
165
|
-
Every top-level folder in _templates should hold at least one action subfolder.
|
|
166
|
-
Create one with: $ hygen with-prompt new my-generator
|
|
167
|
-
`);
|
|
168
|
-
return null;
|
|
169
|
-
}
|
|
170
|
-
const prompter = config.createPrompter();
|
|
171
|
-
const generatorAnswer = await prompter.prompt({
|
|
172
|
-
type: 'select',
|
|
173
|
-
name: 'generator',
|
|
174
|
-
message: 'Select a generator:',
|
|
175
|
-
choices: generators,
|
|
176
|
-
});
|
|
177
|
-
const generator = answerValue(generatorAnswer, 'generator');
|
|
178
|
-
if (!generator)
|
|
179
|
-
return null;
|
|
180
|
-
const actions = (0, help_1.generatorActions)(templates, generator);
|
|
181
|
-
const actionPrompter = config.createPrompter();
|
|
182
|
-
const actionAnswer = await actionPrompter.prompt({
|
|
183
|
-
type: 'select',
|
|
184
|
-
name: 'action',
|
|
185
|
-
message: `Select an action for ${generator}:`,
|
|
186
|
-
choices: actions,
|
|
187
|
-
});
|
|
188
|
-
const action = answerValue(actionAnswer, 'action');
|
|
189
|
-
if (!action)
|
|
190
|
-
return null;
|
|
191
|
-
return { generator, action };
|
|
192
|
-
};
|
|
193
|
-
exports.pickGeneratorAction = pickGeneratorAction;
|
|
194
157
|
exports.default = prompt;
|