@biorate/hygen 0.4.0 → 0.5.1

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.
Files changed (90) hide show
  1. package/README.md +14 -14
  2. package/dist/cjs/src/config-resolver.js +4 -11
  3. package/dist/cjs/src/constants/config.js +5 -0
  4. package/dist/cjs/src/constants/engine.js +4 -0
  5. package/dist/cjs/src/constants/hooks.js +18 -0
  6. package/dist/cjs/src/constants/index.js +19 -0
  7. package/dist/cjs/src/engine.js +8 -11
  8. package/dist/cjs/src/errors.js +19 -1
  9. package/dist/cjs/src/help.js +3 -3
  10. package/dist/cjs/src/params-schema/index.js +11 -0
  11. package/dist/cjs/src/params-schema/prompts.js +34 -0
  12. package/dist/cjs/src/params-schema/registry.js +92 -0
  13. package/dist/cjs/src/params.js +21 -5
  14. package/dist/cjs/src/picker.js +55 -0
  15. package/dist/cjs/src/prompt.js +31 -68
  16. package/dist/cjs/src/ts-node-register.js +36 -0
  17. package/dist/cjs/src/types/config.js +2 -0
  18. package/dist/cjs/src/types/helpers-ns.js +2 -0
  19. package/dist/cjs/src/types/hooks.js +2 -0
  20. package/dist/cjs/src/types/index.js +28 -0
  21. package/dist/cjs/src/types/inflection.js +2 -0
  22. package/dist/cjs/src/types/logger.js +2 -0
  23. package/dist/cjs/src/types/ops.js +2 -0
  24. package/dist/cjs/src/types/params-schema.js +2 -0
  25. package/dist/cjs/src/types/params.js +2 -0
  26. package/dist/cjs/src/types/prompter-ns.js +2 -0
  27. package/dist/cjs/src/types/prompter.js +2 -0
  28. package/dist/cjs/src/types/results.js +2 -0
  29. package/dist/esm/src/config-resolver.js +3 -9
  30. package/dist/esm/src/constants/config.js +2 -0
  31. package/dist/esm/src/constants/engine.js +1 -0
  32. package/dist/esm/src/constants/hooks.js +15 -0
  33. package/dist/esm/src/constants/index.js +3 -0
  34. package/dist/esm/src/engine.js +4 -7
  35. package/dist/esm/src/errors.js +15 -0
  36. package/dist/esm/src/help.js +1 -1
  37. package/dist/esm/src/params-schema/index.js +2 -0
  38. package/dist/esm/src/params-schema/prompts.js +30 -0
  39. package/dist/esm/src/params-schema/registry.js +84 -0
  40. package/dist/esm/src/params.js +19 -2
  41. package/dist/esm/src/picker.js +48 -0
  42. package/dist/esm/src/prompt.js +26 -63
  43. package/dist/esm/src/ts-node-register.js +11 -0
  44. package/dist/esm/src/types/config.js +1 -0
  45. package/dist/esm/src/types/helpers-ns.js +1 -0
  46. package/dist/esm/src/types/hooks.js +1 -0
  47. package/dist/esm/src/types/index.js +12 -0
  48. package/dist/esm/src/types/inflection.js +1 -0
  49. package/dist/esm/src/types/logger.js +1 -0
  50. package/dist/esm/src/types/ops.js +1 -0
  51. package/dist/esm/src/types/params-schema.js +1 -0
  52. package/dist/esm/src/types/params.js +1 -0
  53. package/dist/esm/src/types/prompter-ns.js +1 -0
  54. package/dist/esm/src/types/prompter.js +1 -0
  55. package/dist/esm/src/types/results.js +1 -0
  56. package/dist/types/src/config-resolver.d.ts +0 -1
  57. package/dist/types/src/constants/config.d.ts +2 -0
  58. package/dist/types/src/constants/engine.d.ts +1 -0
  59. package/dist/types/src/constants/hooks.d.ts +4 -0
  60. package/dist/types/src/constants/index.d.ts +3 -0
  61. package/dist/types/src/context.d.ts +1 -1
  62. package/dist/types/src/errors.d.ts +9 -0
  63. package/dist/types/src/helpers.d.ts +1 -13
  64. package/dist/types/src/index.d.ts +1 -0
  65. package/dist/types/src/ops/anchors.d.ts +0 -3
  66. package/dist/types/src/ops/index.d.ts +1 -2
  67. package/dist/types/src/params-schema/index.d.ts +2 -0
  68. package/dist/types/src/params-schema/prompts.d.ts +2 -0
  69. package/dist/types/src/params-schema/registry.d.ts +6 -0
  70. package/dist/types/src/params.d.ts +1 -2
  71. package/dist/types/src/picker.d.ts +2 -0
  72. package/dist/types/src/prompt.d.ts +3 -17
  73. package/dist/types/src/ts-node-register.d.ts +2 -0
  74. package/dist/types/src/types/attributes.d.ts +18 -0
  75. package/dist/types/src/types/config.d.ts +26 -0
  76. package/dist/types/src/types/helpers-ns.d.ts +4 -0
  77. package/dist/types/src/types/hooks.d.ts +39 -0
  78. package/dist/types/src/types/index.d.ts +12 -0
  79. package/dist/types/src/types/inflection.d.ts +12 -0
  80. package/dist/types/src/types/logger.d.ts +8 -0
  81. package/dist/types/src/types/ops.d.ts +7 -0
  82. package/dist/types/src/types/params-schema.d.ts +9 -0
  83. package/dist/types/src/types/params.d.ts +14 -0
  84. package/dist/types/src/types/prompter-ns.d.ts +4 -0
  85. package/dist/types/src/types/prompter.d.ts +3 -0
  86. package/dist/types/src/types/results.d.ts +27 -0
  87. package/package.json +2 -2
  88. package/dist/types/src/types.d.ts +0 -121
  89. /package/dist/cjs/src/{types.js → types/attributes.js} +0 -0
  90. /package/dist/esm/src/{types.js → types/attributes.js} +0 -0
@@ -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,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -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);
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -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
- export const CONFIG_FILES = ['.hygen.js', '.hygen.cjs', '.hygen.mjs', '.hygen.ts'];
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
- if (/\.ts$/.test(f)) {
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,2 @@
1
+ export const CONFIG_FILES = ['.hygen.js', '.hygen.cjs', '.hygen.mjs', '.hygen.ts'];
2
+ export const DEFAULT_ACTION = '_default';
@@ -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
+ ];
@@ -0,0 +1,3 @@
1
+ export * from './hooks.js';
2
+ export * from './config.js';
3
+ export * from './engine.js';
@@ -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, pickGeneratorAction, resolveActionHooks, } from './prompt.js';
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 });
@@ -98,10 +99,6 @@ const engine = async (argv, config) => {
98
99
  action,
99
100
  ...(name !== undefined ? { name } : {}),
100
101
  };
101
- const pairStatus = {
102
- action,
103
- ...(name !== undefined ? { name } : {}),
104
- };
105
102
  if (!(await fs.pathExists(actionfolder))) {
106
103
  const err = new ShowHelpError(`I can't find action '${action}' for generator '${generator}'.
107
104
 
@@ -116,7 +113,7 @@ const engine = async (argv, config) => {
116
113
  if (!keepGoing)
117
114
  throw err;
118
115
  failures.push({ ...pair, error: err.message });
119
- statuses.push({ ...pairStatus, status: 'failed' });
116
+ statuses.push({ ...pair, status: 'failed' });
120
117
  continue;
121
118
  }
122
119
  if (projectBeforeAllError) {
@@ -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
+ }
@@ -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 './params.js';
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,2 @@
1
+ export { coerceParam, checkParam, promptTypeOf, resolveType, validateParams, } from './registry.js';
2
+ export { promptFromParams } from './prompts.js';
@@ -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
+ };
@@ -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
- export const DEFAULT_ACTION = '_default';
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
+ };
@@ -1,27 +1,32 @@
1
1
  import path from 'path';
2
2
  import fs from 'fs';
3
3
  import helpers from './helpers.js';
4
- import { pickableGenerators, generatorActions } from './help.js';
5
4
  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');
5
+ import registerTsNode from './ts-node-register.js';
6
+ import { promptFromParams } from './params-schema/index.js';
7
+ import { ParamUnknownTypeError } from './errors.js';
8
+ export const validateParamsSchema = (schema) => {
9
+ if (!schema)
10
+ return undefined;
11
+ if (!Array.isArray(schema)) {
12
+ throw new Error('the params export must be an array of parameter descriptors');
13
+ }
14
+ for (const descriptor of schema) {
15
+ if (typeof descriptor?.name !== 'string' || descriptor.name === '') {
16
+ throw new Error('every parameter descriptor must declare a non-empty string name');
20
17
  }
21
- catch (cause) {
22
- throw new HookTsNodeError(file, cause);
18
+ const { type } = descriptor;
19
+ if (typeof type === 'function')
20
+ continue;
21
+ if (type !== 'string' && type !== 'number' && type !== 'boolean') {
22
+ throw new ParamUnknownTypeError(descriptor.name, String(type));
23
23
  }
24
24
  }
25
+ return schema;
26
+ };
27
+ import { ACTION_HOOK_FILES, HOOK_FILES, LIFECYCLE_METHODS } from './constants/index.js';
28
+ const loadHookModule = async (file) => {
29
+ await registerTsNode(file, HookTsNodeError);
25
30
  return (await import(file));
26
31
  };
27
32
  const legacyAdapter = (raw) => ({
@@ -57,6 +62,7 @@ const loadResolvedHooks = async (actionfolder) => {
57
62
  instance: new exported(),
58
63
  kind: 'class',
59
64
  file,
65
+ params: validateParamsSchema(exported.params),
60
66
  };
61
67
  }
62
68
  catch (cause) {
@@ -97,6 +103,9 @@ const prompt = async (createPrompter, actionfolder, args, options = {}) => {
97
103
  if (resolved.kind === 'legacy' && legacy?.params) {
98
104
  return legacy.params({ args, h: helpers });
99
105
  }
106
+ if (resolved.kind === 'class' && resolved.params?.length) {
107
+ return promptFromParams(resolved.params, createPrompter, args);
108
+ }
100
109
  if (!resolved.instance.prompt)
101
110
  return {};
102
111
  const prompter = createPrompter();
@@ -113,50 +122,4 @@ const prompt = async (createPrompter, actionfolder, args, options = {}) => {
113
122
  },
114
123
  });
115
124
  };
116
- const answerValue = (answer, name) => {
117
- const value = answer[name];
118
- return typeof value === 'string' && value.length > 0 ? value : undefined;
119
- };
120
- export const pickGeneratorAction = async (config) => {
121
- const { templates, logger } = config;
122
- if (!templates || !fs.existsSync(templates)) {
123
- logger.log(`No generators found.
124
-
125
- The interactive picker needs a _templates folder here, or anywhere up the folder tree.
126
- Create one with: $ hygen init self
127
- `);
128
- return null;
129
- }
130
- const generators = pickableGenerators(templates);
131
- if (generators.length === 0) {
132
- logger.log(`No generators found.
133
-
134
- Every top-level folder in _templates should hold at least one action subfolder.
135
- Create one with: $ hygen with-prompt new my-generator
136
- `);
137
- return null;
138
- }
139
- const prompter = config.createPrompter();
140
- const generatorAnswer = await prompter.prompt({
141
- type: 'select',
142
- name: 'generator',
143
- message: 'Select a generator:',
144
- choices: generators,
145
- });
146
- const generator = answerValue(generatorAnswer, 'generator');
147
- if (!generator)
148
- return null;
149
- const actions = generatorActions(templates, generator);
150
- const actionPrompter = config.createPrompter();
151
- const actionAnswer = await actionPrompter.prompt({
152
- type: 'select',
153
- name: 'action',
154
- message: `Select an action for ${generator}:`,
155
- choices: actions,
156
- });
157
- const action = answerValue(actionAnswer, 'action');
158
- if (!action)
159
- return null;
160
- return { generator, action };
161
- };
162
125
  export default prompt;
@@ -0,0 +1,11 @@
1
+ const registerTsNode = async (file, error) => {
2
+ if (!/\.ts$/.test(file))
3
+ return;
4
+ try {
5
+ await import('ts-node/register/transpile-only');
6
+ }
7
+ catch (cause) {
8
+ throw new error(file, cause);
9
+ }
10
+ };
11
+ export default registerTsNode;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,12 @@
1
+ export * from './logger.js';
2
+ export * from './prompter.js';
3
+ export * from './prompter-ns.js';
4
+ export * from './attributes.js';
5
+ export * from './helpers-ns.js';
6
+ export * from './hooks.js';
7
+ export * from './config.js';
8
+ export * from './results.js';
9
+ export * from './params.js';
10
+ export * from './params-schema.js';
11
+ export * from './ops.js';
12
+ export * from './inflection.js';
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -1,4 +1,3 @@
1
1
  import type { RunnerConfig } from './types';
2
- export declare const CONFIG_FILES: string[];
3
2
  declare const _default: (config: RunnerConfig) => Promise<RunnerConfig>;
4
3
  export default _default;
@@ -0,0 +1,2 @@
1
+ export declare const CONFIG_FILES: string[];
2
+ export declare const DEFAULT_ACTION = "_default";
@@ -0,0 +1 @@
1
+ export declare const RUN_SCOPE = "*";
@@ -0,0 +1,4 @@
1
+ export declare const ACTION_HOOK_FILES: string[];
2
+ export declare const LEGACY_HOOK_FILES: string[];
3
+ export declare const HOOK_FILES: string[];
4
+ export declare const LIFECYCLE_METHODS: readonly ["beforeAll", "beforeEach", "afterEach", "afterAll"];
@@ -0,0 +1,3 @@
1
+ export * from './hooks';
2
+ export * from './config';
3
+ export * from './engine';