@biorate/hygen 0.0.0-stage → 0.2.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.
Files changed (104) hide show
  1. package/LICENSE +25 -0
  2. package/README.md +146 -2
  3. package/dist/cjs/bin.js +26 -0
  4. package/dist/cjs/index.js +17 -0
  5. package/dist/cjs/package.json +3 -0
  6. package/dist/cjs/src/cli-help.js +19 -0
  7. package/dist/cjs/src/config-resolver.js +66 -0
  8. package/dist/cjs/src/config.js +37 -0
  9. package/dist/cjs/src/context.js +37 -0
  10. package/dist/cjs/src/engine.js +149 -0
  11. package/dist/cjs/src/errors.js +116 -0
  12. package/dist/cjs/src/execute.js +48 -0
  13. package/dist/cjs/src/help.js +89 -0
  14. package/dist/cjs/src/helpers.js +24 -0
  15. package/dist/cjs/src/index.js +42 -0
  16. package/dist/cjs/src/logger.js +28 -0
  17. package/dist/cjs/src/newline.js +13 -0
  18. package/dist/cjs/src/ops/add.js +74 -0
  19. package/dist/cjs/src/ops/anchors.js +45 -0
  20. package/dist/cjs/src/ops/echo.js +17 -0
  21. package/dist/cjs/src/ops/index.js +51 -0
  22. package/dist/cjs/src/ops/inject.js +37 -0
  23. package/dist/cjs/src/ops/injector.js +49 -0
  24. package/dist/cjs/src/ops/result.js +9 -0
  25. package/dist/cjs/src/ops/setup.js +52 -0
  26. package/dist/cjs/src/ops/shell.js +38 -0
  27. package/dist/cjs/src/params.js +77 -0
  28. package/dist/cjs/src/path-macros.js +30 -0
  29. package/dist/cjs/src/prompt.js +111 -0
  30. package/dist/cjs/src/render.js +189 -0
  31. package/dist/cjs/src/runner.js +38 -0
  32. package/dist/cjs/src/types.js +2 -0
  33. package/dist/cjs/templates/init/repo/new-repo.ejs.t +4 -0
  34. package/dist/cjs/templates/init/self/new-generator-help.ejs.t +8 -0
  35. package/dist/cjs/templates/init/self/new-generator-with-prompt.ejs.t +21 -0
  36. package/dist/cjs/templates/init/self/new-generator-with-prompt.prompt.ejs.t +17 -0
  37. package/dist/cjs/templates/init/self/new-generator.ejs.t +21 -0
  38. package/dist/cjs/templates/init/self/new-repo.ejs.t +7 -0
  39. package/dist/esm/bin.js +21 -0
  40. package/dist/esm/index.js +1 -0
  41. package/dist/esm/package.json +3 -0
  42. package/dist/esm/src/cli-help.js +14 -0
  43. package/dist/esm/src/config-resolver.js +37 -0
  44. package/dist/esm/src/config.js +29 -0
  45. package/dist/esm/src/context.js +32 -0
  46. package/dist/esm/src/engine.js +118 -0
  47. package/dist/esm/src/errors.js +98 -0
  48. package/dist/esm/src/execute.js +41 -0
  49. package/dist/esm/src/help.js +78 -0
  50. package/dist/esm/src/helpers.js +19 -0
  51. package/dist/esm/src/index.js +6 -0
  52. package/dist/esm/src/logger.js +23 -0
  53. package/dist/esm/src/newline.js +11 -0
  54. package/dist/esm/src/ops/add.js +69 -0
  55. package/dist/esm/src/ops/anchors.js +41 -0
  56. package/dist/esm/src/ops/echo.js +12 -0
  57. package/dist/esm/src/ops/index.js +26 -0
  58. package/dist/esm/src/ops/inject.js +32 -0
  59. package/dist/esm/src/ops/injector.js +44 -0
  60. package/dist/esm/src/ops/result.js +7 -0
  61. package/dist/esm/src/ops/setup.js +47 -0
  62. package/dist/esm/src/ops/shell.js +33 -0
  63. package/dist/esm/src/params.js +71 -0
  64. package/dist/esm/src/path-macros.js +24 -0
  65. package/dist/esm/src/prompt.js +81 -0
  66. package/dist/esm/src/render.js +182 -0
  67. package/dist/esm/src/runner.js +32 -0
  68. package/dist/esm/src/types.js +1 -0
  69. package/dist/esm/templates/init/repo/new-repo.ejs.t +4 -0
  70. package/dist/esm/templates/init/self/new-generator-help.ejs.t +8 -0
  71. package/dist/esm/templates/init/self/new-generator-with-prompt.ejs.t +21 -0
  72. package/dist/esm/templates/init/self/new-generator-with-prompt.prompt.ejs.t +17 -0
  73. package/dist/esm/templates/init/self/new-generator.ejs.t +21 -0
  74. package/dist/esm/templates/init/self/new-repo.ejs.t +7 -0
  75. package/dist/types/bin.d.ts +2 -0
  76. package/dist/types/index.d.ts +1 -0
  77. package/dist/types/src/cli-help.d.ts +3 -0
  78. package/dist/types/src/config-resolver.d.ts +4 -0
  79. package/dist/types/src/config.d.ts +14 -0
  80. package/dist/types/src/context.d.ts +12 -0
  81. package/dist/types/src/engine.d.ts +6 -0
  82. package/dist/types/src/errors.d.ts +50 -0
  83. package/dist/types/src/execute.d.ts +5 -0
  84. package/dist/types/src/help.d.ts +8 -0
  85. package/dist/types/src/helpers.d.ts +22 -0
  86. package/dist/types/src/index.d.ts +6 -0
  87. package/dist/types/src/logger.d.ts +10 -0
  88. package/dist/types/src/newline.d.ts +2 -0
  89. package/dist/types/src/ops/add.d.ts +3 -0
  90. package/dist/types/src/ops/anchors.d.ts +4 -0
  91. package/dist/types/src/ops/echo.d.ts +3 -0
  92. package/dist/types/src/ops/index.d.ts +4 -0
  93. package/dist/types/src/ops/inject.d.ts +3 -0
  94. package/dist/types/src/ops/injector.d.ts +3 -0
  95. package/dist/types/src/ops/result.d.ts +3 -0
  96. package/dist/types/src/ops/setup.d.ts +3 -0
  97. package/dist/types/src/ops/shell.d.ts +3 -0
  98. package/dist/types/src/params.d.ts +4 -0
  99. package/dist/types/src/path-macros.d.ts +3 -0
  100. package/dist/types/src/prompt.d.ts +8 -0
  101. package/dist/types/src/render.d.ts +2 -0
  102. package/dist/types/src/runner.d.ts +3 -0
  103. package/dist/types/src/types.d.ts +94 -0
  104. package/package.json +82 -4
@@ -0,0 +1,47 @@
1
+ import path from 'path';
2
+ import createDebug from 'debug';
3
+ import degit from 'degit';
4
+ import createResult from './result.js';
5
+ import { SetupRepositoryError } from '../errors.js';
6
+ const debug = createDebug('hygen:ops:setup');
7
+ const notEmpty = (x) => x && x.length > 0;
8
+ const setup = async ({ attributes: { setup, mode, verbose, force } }, args, { logger, cwd }) => {
9
+ const result = createResult('setup', setup);
10
+ const setupStr = setup;
11
+ if (notEmpty(setupStr)) {
12
+ if (!setupStr.includes('/')) {
13
+ throw new SetupRepositoryError(setupStr);
14
+ }
15
+ const to = args.to || '.';
16
+ const payload = {
17
+ setup: setupStr,
18
+ mode: mode || 'tar',
19
+ verbose: !!verbose,
20
+ force: !!args.force || !!force,
21
+ to: path.resolve(cwd, to),
22
+ };
23
+ if (!args.dry) {
24
+ try {
25
+ debug('exec %o %o', setup, payload);
26
+ const d = degit(setupStr, {
27
+ mode: payload.mode,
28
+ force: payload.force,
29
+ verbose: payload.verbose,
30
+ });
31
+ await d.clone(payload.to);
32
+ debug('done');
33
+ logger.ok(` setup: ${setup} -> ${to}`);
34
+ }
35
+ catch (error) {
36
+ debug('error %o', error);
37
+ logger.err(` setup: ${setup} ${String(error)}`);
38
+ }
39
+ }
40
+ else {
41
+ logger.ok(` setup: ${setup} -> ${to}`);
42
+ }
43
+ return result('executed', payload);
44
+ }
45
+ return result('ignored');
46
+ };
47
+ export default setup;
@@ -0,0 +1,33 @@
1
+ import createDebug from 'debug';
2
+ import ora from 'ora';
3
+ import createResult from './result.js';
4
+ const debug = createDebug('hygen:ops:shell');
5
+ const notEmpty = (x) => x && x.length > 0;
6
+ const shell = async ({ attributes: { sh, spinner, sh_ignore_exit }, body }, args, { logger, exec }) => {
7
+ const result = createResult('shell', sh);
8
+ const shStr = sh;
9
+ if (notEmpty(shStr)) {
10
+ const spin = ora(` shell: ${spinner === true ? 'running...' : spinner}`);
11
+ if (!args.dry) {
12
+ try {
13
+ debug('exec %o %o', shStr, body);
14
+ spinner && spin.start();
15
+ const res = await exec(shStr, body);
16
+ debug('result %o', res);
17
+ }
18
+ catch (error) {
19
+ if (sh_ignore_exit !== true) {
20
+ logger.err(error.stderr ?? String(error));
21
+ process.exit(1);
22
+ }
23
+ }
24
+ finally {
25
+ spinner && spin.succeed();
26
+ }
27
+ }
28
+ logger.ok(` shell: ${sh}`);
29
+ return result('executed');
30
+ }
31
+ return result('ignored');
32
+ };
33
+ export default shell;
@@ -0,0 +1,71 @@
1
+ import path from 'path';
2
+ import yargs from 'yargs-parser';
3
+ import fs from 'fs-extra';
4
+ import { CliArgumentError, NameFlagRemovedError, TemplateNotFoundError } from './errors.js';
5
+ export const DEFAULT_ACTION = '_default';
6
+ const splitList = (value) => value ? value.split(',') : undefined;
7
+ const resolvePositionals = async (templates, args) => {
8
+ let [generator, action, name] = args;
9
+ const actionList = splitList(action);
10
+ const nameList = splitList(name);
11
+ if (generator &&
12
+ action &&
13
+ (await fs.pathExists(path.join(templates, generator, action)))) {
14
+ return { generator, actions: [action], names: nameList };
15
+ }
16
+ if (generator && action && actionList && actionList.length > 1) {
17
+ const missing = [];
18
+ for (const part of actionList) {
19
+ if (!(await fs.pathExists(path.join(templates, generator, part)))) {
20
+ missing.push(part);
21
+ }
22
+ }
23
+ if (missing.length === 0) {
24
+ return { generator, actions: actionList, names: nameList };
25
+ }
26
+ if (missing.length < actionList.length) {
27
+ throw new TemplateNotFoundError(generator, missing[0]);
28
+ }
29
+ }
30
+ if (generator &&
31
+ (await fs.pathExists(path.join(templates, generator, DEFAULT_ACTION)))) {
32
+ [generator, name] = args;
33
+ return { generator, actions: [DEFAULT_ACTION], names: splitList(name) };
34
+ }
35
+ return { generator, actions: actionList, names: nameList };
36
+ };
37
+ const params = async ({ templates, legacyNameFlag }, externalArgv) => {
38
+ const argv = yargs(externalArgv);
39
+ const { _, ...cleanArgv } = argv;
40
+ const legacyGate = process.env.HYGEN_LEGACY_NAME === '1' || legacyNameFlag === true;
41
+ if (Array.isArray(cleanArgv.name) && !legacyGate) {
42
+ 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').");
43
+ }
44
+ if (cleanArgv.name !== undefined && !legacyGate) {
45
+ throw new NameFlagRemovedError(cleanArgv.name);
46
+ }
47
+ const { generator, actions, names: positionalNames, } = await resolvePositionals(templates, argv._.map(String));
48
+ const names = legacyGate && cleanArgv.name !== undefined && positionalNames === undefined
49
+ ? [cleanArgv.name]
50
+ : positionalNames;
51
+ if (!generator || !actions) {
52
+ return {
53
+ generator,
54
+ actions,
55
+ names,
56
+ templates,
57
+ cleanArgv,
58
+ };
59
+ }
60
+ return {
61
+ templates,
62
+ generator,
63
+ actions,
64
+ names,
65
+ cleanArgv,
66
+ _,
67
+ ts: process.env.HYGEN_TS || new Date().getTime(),
68
+ ...cleanArgv,
69
+ };
70
+ };
71
+ export default params;
@@ -0,0 +1,24 @@
1
+ import path from 'path';
2
+ import { PathMacroResolutionError } from './errors.js';
3
+ import context from './context.js';
4
+ const MACRO_PATTERN = /\$([A-Za-z_][A-Za-z0-9_]*)\$/g;
5
+ const macroDictionary = (args, config) => {
6
+ const ctx = context(args, config);
7
+ return { ...ctx, ...ctx.h };
8
+ };
9
+ const resolvePathMacros = (rel, args, config, file) => {
10
+ if (!rel.includes('$'))
11
+ return rel;
12
+ const dict = macroDictionary(args, config);
13
+ return rel
14
+ .split(path.sep)
15
+ .map((segment) => segment.replace(MACRO_PATTERN, (_match, identifier) => {
16
+ const value = dict[identifier];
17
+ if (value === undefined || value === null) {
18
+ throw new PathMacroResolutionError(file, segment, identifier);
19
+ }
20
+ return String(value);
21
+ }))
22
+ .join(path.sep);
23
+ };
24
+ export { resolvePathMacros };
@@ -0,0 +1,81 @@
1
+ import path from 'path';
2
+ import fs from 'fs';
3
+ import helpers from './helpers.js';
4
+ import { pickableGenerators, generatorActions } from './help.js';
5
+ const hooksfiles = ['index.js', 'index.ts', 'prompt.cjs', 'prompt.js', 'prompt.ts'];
6
+ const prompt = async (createPrompter, actionfolder, args) => {
7
+ const hooksfile = hooksfiles
8
+ .map((f) => path.resolve(path.join(actionfolder, f)))
9
+ .find((f) => fs.existsSync(f));
10
+ if (!hooksfile) {
11
+ return Promise.resolve({});
12
+ }
13
+ const isTypeScriptHook = /\.ts$/.test(hooksfile);
14
+ if (isTypeScriptHook) {
15
+ require('ts-node/register/transpile-only');
16
+ }
17
+ let hooksModule = await import(hooksfile);
18
+ if (hooksModule.default) {
19
+ hooksModule = hooksModule.default;
20
+ }
21
+ if (hooksModule.params) {
22
+ return hooksModule.params({ args, h: helpers });
23
+ }
24
+ const prompter = createPrompter();
25
+ if (hooksModule.prompt) {
26
+ return hooksModule.prompt({
27
+ prompter,
28
+ inquirer: prompter,
29
+ args,
30
+ h: helpers,
31
+ });
32
+ }
33
+ return prompter.prompt(hooksModule.filter((p) => args[p.name] === undefined || args[p.name] === null || args[p.name].length === 0));
34
+ };
35
+ const answerValue = (answer, name) => {
36
+ const value = answer[name];
37
+ return typeof value === 'string' && value.length > 0 ? value : undefined;
38
+ };
39
+ export const pickGeneratorAction = async (config) => {
40
+ const { templates, logger } = config;
41
+ if (!templates || !fs.existsSync(templates)) {
42
+ logger.log(`No generators found.
43
+
44
+ The interactive picker needs a _templates folder here, or anywhere up the folder tree.
45
+ Create one with: $ hygen init self
46
+ `);
47
+ return null;
48
+ }
49
+ const generators = pickableGenerators(templates);
50
+ if (generators.length === 0) {
51
+ logger.log(`No generators found.
52
+
53
+ Every top-level folder in _templates should hold at least one action subfolder.
54
+ Create one with: $ hygen with-prompt new my-generator
55
+ `);
56
+ return null;
57
+ }
58
+ const prompter = config.createPrompter();
59
+ const generatorAnswer = await prompter.prompt({
60
+ type: 'select',
61
+ name: 'generator',
62
+ message: 'Select a generator:',
63
+ choices: generators,
64
+ });
65
+ const generator = answerValue(generatorAnswer, 'generator');
66
+ if (!generator)
67
+ return null;
68
+ const actions = generatorActions(templates, generator);
69
+ const actionPrompter = config.createPrompter();
70
+ const actionAnswer = await actionPrompter.prompt({
71
+ type: 'select',
72
+ name: 'action',
73
+ message: `Select an action for ${generator}:`,
74
+ choices: actions,
75
+ });
76
+ const action = answerValue(actionAnswer, 'action');
77
+ if (!action)
78
+ return null;
79
+ return { generator, action };
80
+ };
81
+ export default prompt;
@@ -0,0 +1,182 @@
1
+ import path from 'path';
2
+ import fs from 'fs-extra';
3
+ import ejs from 'ejs';
4
+ import fg from 'fast-glob';
5
+ import fm from 'front-matter';
6
+ import walk from 'ignore-walk';
7
+ import createDebug from 'debug';
8
+ import context from './context.js';
9
+ import { GlobMirrorConflictError, TemplateConfigError, TemplateRenderError, UnknownTransformError, } from './errors.js';
10
+ const debug = createDebug('hygen:render');
11
+ const evaluateSkip = (rendered) => {
12
+ if (rendered === undefined)
13
+ return undefined;
14
+ if (rendered === '' || rendered === false)
15
+ return undefined;
16
+ if (rendered === true)
17
+ return true;
18
+ const value = String(rendered).trim();
19
+ if (value === '')
20
+ return undefined;
21
+ if (value.toLowerCase() === 'false')
22
+ return undefined;
23
+ if (value.toLowerCase() === 'true')
24
+ return true;
25
+ return value;
26
+ };
27
+ const map = (f) => (arr) => arr.map(f);
28
+ const filter = (f) => (arr) => arr.filter(f);
29
+ const ignores = [
30
+ 'prompt.js',
31
+ 'index.js',
32
+ 'prompt.ts',
33
+ 'index.ts',
34
+ '.hygenignore',
35
+ '.DS_Store',
36
+ '.Spotlight-V100',
37
+ '.Trashes',
38
+ 'ehthumbs.db',
39
+ 'Thumbs.db',
40
+ ];
41
+ const renderTemplate = (tmpl, locals, config, file) => {
42
+ if (typeof tmpl !== 'string')
43
+ return tmpl;
44
+ try {
45
+ return ejs.render(tmpl, context(locals, config), {
46
+ escape: (s) => s,
47
+ });
48
+ }
49
+ catch (error) {
50
+ throw new TemplateRenderError(file, error);
51
+ }
52
+ };
53
+ async function getFiles(dir) {
54
+ const files = walk
55
+ .sync({ path: dir, ignoreFiles: ['.hygenignore'] })
56
+ .map((f) => path.join(dir, f));
57
+ return files;
58
+ }
59
+ const applyTransforms = async (content, attributes, locals, config) => {
60
+ const nameList = attributes.transform;
61
+ if (nameList == null)
62
+ return content;
63
+ const hasFileOutput = Boolean(attributes.to) || (attributes.mirror === true && attributes.sh === undefined);
64
+ if (attributes.sh !== undefined && !hasFileOutput) {
65
+ config.logger?.warn(`transform [${String(nameList)}] is ignored for sh: action (no file output)`);
66
+ return content;
67
+ }
68
+ const transforms = config.transforms ?? {};
69
+ let result = content;
70
+ for (const rawName of String(nameList).split('|')) {
71
+ const name = rawName.trim();
72
+ if (!name)
73
+ continue;
74
+ const transform = transforms[name];
75
+ if (typeof transform !== 'function') {
76
+ throw new UnknownTransformError(name);
77
+ }
78
+ result = await transform(result, locals);
79
+ }
80
+ return result;
81
+ };
82
+ export const render = async (args, config) => getFiles(args.actionfolder)
83
+ .then((things) => things.sort((a, b) => a.localeCompare(b)))
84
+ .then(filter((f) => !ignores.find((ig) => f.endsWith(ig))))
85
+ .then(filter((file) => args.subaction
86
+ ? !!file.replace(args.actionfolder, '').match(args.subaction)
87
+ : true))
88
+ .then(map((file) => fs.readFile(file).then((text) => ({ file, text: text.toString() }))))
89
+ .then((_) => Promise.all(_))
90
+ .then(map(({ file, text }) => {
91
+ debug('Pre-formatting file: %o', file);
92
+ return { file, ...fm(text, { allowUnsafe: true }) };
93
+ }))
94
+ .then(map(({ file, attributes, body, }) => expandItem(file, attributes, body, args, config)))
95
+ .then((_) => Promise.all(_))
96
+ .then((_) => _.flat());
97
+ const expandItem = async (file, attributes, body, args, config) => {
98
+ const renderedAttrs = Object.entries(attributes).reduce((obj, [key, value]) => {
99
+ return {
100
+ ...obj,
101
+ [key]: renderTemplate(value, args, config, file),
102
+ };
103
+ }, {});
104
+ debug('Rendering file: %o', file);
105
+ if (renderedAttrs.glob) {
106
+ if (renderedAttrs.mirror === true) {
107
+ throw new GlobMirrorConflictError(file);
108
+ }
109
+ return expandGlob(file, renderedAttrs, args, config);
110
+ }
111
+ const locals = { ...args, attributes: renderedAttrs };
112
+ const skip = evaluateSkip(renderedAttrs.skip);
113
+ if (skip !== undefined) {
114
+ return [{ file, attributes: renderedAttrs, body: '', skip }];
115
+ }
116
+ return [
117
+ {
118
+ file,
119
+ attributes: renderedAttrs,
120
+ body: await applyTransforms(renderTemplate(body, locals, config, file), renderedAttrs, locals, config),
121
+ },
122
+ ];
123
+ };
124
+ const expandGlob = async (file, attributes, args, config) => {
125
+ const skip = evaluateSkip(attributes.skip);
126
+ if (skip !== undefined) {
127
+ return [{ file, attributes: {}, body: '', skip }];
128
+ }
129
+ const baseDir = path.dirname(file);
130
+ const matches = (await fg(attributes.glob, {
131
+ cwd: baseDir,
132
+ absolute: true,
133
+ onlyFiles: true,
134
+ dot: false,
135
+ })).sort();
136
+ const basePrefix = attributes.base
137
+ ? attributes.base.replace(/[\\/]+$/, '')
138
+ : undefined;
139
+ const stripExts = attributes.stripExtensions == null
140
+ ? []
141
+ : Array.isArray(attributes.stripExtensions)
142
+ ? attributes.stripExtensions
143
+ : [attributes.stripExtensions];
144
+ const destFolder = (attributes.to ?? attributes.destination);
145
+ if (!destFolder) {
146
+ throw new TemplateConfigError(`glob template [${file}] must declare a 'to:' (or 'destination:') destination folder`);
147
+ }
148
+ const renderedItems = [];
149
+ for (const match of matches) {
150
+ let rel = path.relative(baseDir, match);
151
+ if (basePrefix) {
152
+ if (rel === basePrefix) {
153
+ rel = '';
154
+ }
155
+ else if (rel.startsWith(basePrefix + path.sep)) {
156
+ rel = rel.slice(basePrefix.length + 1);
157
+ }
158
+ }
159
+ for (const ext of stripExts) {
160
+ if (ext && rel.endsWith(ext)) {
161
+ rel = rel.slice(0, -ext.length);
162
+ break;
163
+ }
164
+ }
165
+ const renderedRel = renderTemplate(rel, args, config, file);
166
+ const outAttributes = {
167
+ to: path.join(destFolder, renderedRel),
168
+ };
169
+ if (attributes.skipIfExists !== undefined) {
170
+ outAttributes.skipIfExists = attributes.skipIfExists;
171
+ }
172
+ if (attributes.transform !== undefined) {
173
+ outAttributes.transform = attributes.transform;
174
+ }
175
+ renderedItems.push({
176
+ file: match,
177
+ attributes: outAttributes,
178
+ body: await applyTransforms(renderTemplate((await fs.readFile(match)).toString(), args, config, match), outAttributes, args, config),
179
+ });
180
+ }
181
+ return renderedItems;
182
+ };
@@ -0,0 +1,32 @@
1
+ import resolve from './config-resolver.js';
2
+ import { engine, ShowHelpError, KeepGoingError } from './engine.js';
3
+ import { printHelp } from './help.js';
4
+ const runner = async (argv, config) => {
5
+ const resolvedConfig = await resolve(config);
6
+ const { templates, logger } = resolvedConfig;
7
+ try {
8
+ const actions = await engine(argv, resolvedConfig);
9
+ return { success: true, actions, statuses: actions.statuses, time: 0 };
10
+ }
11
+ catch (err) {
12
+ const e = err;
13
+ logger.log(e.toString());
14
+ if (resolvedConfig.debug) {
15
+ logger.log('details -----------');
16
+ logger.log(e.stack ?? '');
17
+ logger.log('-------------------');
18
+ }
19
+ if (err instanceof ShowHelpError) {
20
+ printHelp(templates, logger);
21
+ }
22
+ const keepGoing = err instanceof KeepGoingError ? err : undefined;
23
+ return {
24
+ success: false,
25
+ actions: keepGoing ? keepGoing.results : [],
26
+ statuses: keepGoing ? keepGoing.statuses : [],
27
+ ...(keepGoing ? { failures: keepGoing.failures } : {}),
28
+ time: 0,
29
+ };
30
+ }
31
+ };
32
+ export { runner };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,4 @@
1
+ ---
2
+ setup: <%= name %>
3
+ force: true # this is because mostly, people init into existing folders is safe
4
+ ---
@@ -0,0 +1,8 @@
1
+ ---
2
+ to: _templates/generator/help/index.ejs.t
3
+ ---
4
+ ---
5
+ message: |
6
+ hygen {bold generator new} [NAME] --action [ACTION]
7
+ hygen {bold generator with-prompt} [NAME] --action [ACTION]
8
+ ---
@@ -0,0 +1,21 @@
1
+ ---
2
+ to: _templates/generator/with-prompt/hello.ejs.t
3
+ ---
4
+ ---
5
+ to: _templates/<%%= name %%>/<%%= action || 'new' %%>/hello.ejs.t
6
+ ---
7
+ ---
8
+ to: app/hello.js
9
+ ---
10
+ const hello = ```
11
+ Hello!
12
+ This is your first prompt based hygen template.
13
+
14
+ Learn what it can do here:
15
+
16
+ https://github.com/jondot/hygen
17
+ ```
18
+
19
+ console.log(hello)
20
+
21
+
@@ -0,0 +1,17 @@
1
+ ---
2
+ to: _templates/generator/with-prompt/prompt.ejs.t
3
+ ---
4
+ ---
5
+ to: _templates/<%%= name %%>/<%%= action || 'new' %%>/prompt.js
6
+ ---
7
+
8
+ // see types of prompts:
9
+ // https://github.com/enquirer/enquirer/tree/master/examples
10
+ //
11
+ module.exports = [
12
+ {
13
+ type: 'input',
14
+ name: 'message',
15
+ message: "What's your message?"
16
+ }
17
+ ]
@@ -0,0 +1,21 @@
1
+ ---
2
+ to: _templates/generator/new/hello.ejs.t
3
+ ---
4
+ ---
5
+ to: _templates/<%%= name %%>/<%%= action || 'new' %%>/hello.ejs.t
6
+ ---
7
+ ---
8
+ to: app/hello.js
9
+ ---
10
+ const hello = ```
11
+ Hello!
12
+ This is your first hygen template.
13
+
14
+ Learn what it can do here:
15
+
16
+ https://github.com/jondot/hygen
17
+ ```
18
+
19
+ console.log(hello)
20
+
21
+
@@ -0,0 +1,7 @@
1
+ ---
2
+ to: _templates/init/repo/new-repo.ejs.t
3
+ ---
4
+ ---
5
+ setup: <%%= name %%>
6
+ force: true # this is because mostly, people init into existing folders is safe
7
+ ---
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ export {};
@@ -0,0 +1 @@
1
+ export * from './src';
@@ -0,0 +1,3 @@
1
+ import type { Logger } from './types';
2
+ export declare const isHelpFlag: (argv: string[]) => boolean;
3
+ export declare const printUsage: (logger: Logger) => void;
@@ -0,0 +1,4 @@
1
+ import type { RunnerConfig } from './types';
2
+ export declare const CONFIG_FILES: string[];
3
+ declare const _default: (config: RunnerConfig) => Promise<RunnerConfig>;
4
+ export default _default;
@@ -0,0 +1,14 @@
1
+ import importedPath from 'path';
2
+ import type { ResolverIO } from './types';
3
+ declare const reversePathsToWalk: ({ folder, path, }: {
4
+ folder: string;
5
+ path: typeof importedPath;
6
+ }) => string[];
7
+ declare const configLookup: (file: string, folder: string, path?: typeof importedPath) => string[];
8
+ declare class ConfigResolver {
9
+ configFiles: string[];
10
+ io: ResolverIO;
11
+ constructor(configFiles: string[], io: ResolverIO);
12
+ resolve(from: string): Promise<Record<string, unknown>>;
13
+ }
14
+ export { configLookup, ConfigResolver, reversePathsToWalk };
@@ -0,0 +1,12 @@
1
+ import type { RunnerConfig } from './types';
2
+ declare const context: (locals: Record<string, any>, config?: Partial<RunnerConfig>) => {
3
+ name: string;
4
+ } & Record<string, any> & {
5
+ h: {
6
+ capitalize(str: string): string;
7
+ inflection: import("./helpers").Inflection;
8
+ changeCase: typeof import("change-case");
9
+ path: import("path").PlatformPath;
10
+ };
11
+ };
12
+ export default context;
@@ -0,0 +1,6 @@
1
+ import type { ActionResult, ActionStatusEntry, RunnerConfig } from './types';
2
+ import { ShowHelpError, KeepGoingError } from './errors';
3
+ declare const engine: (argv: string[], config: RunnerConfig) => Promise<ActionResult[] & {
4
+ statuses: ActionStatusEntry[];
5
+ }>;
6
+ export { engine, ShowHelpError, KeepGoingError };
@@ -0,0 +1,50 @@
1
+ import { BaseError } from '@biorate/errors';
2
+ import type { ActionResult, ActionStatusEntry, RunFailure } from './types';
3
+ export declare class ShowHelpError extends BaseError {
4
+ constructor(message: string);
5
+ }
6
+ export declare class KeepGoingError extends ShowHelpError {
7
+ readonly results: ActionResult[];
8
+ readonly failures: RunFailure[];
9
+ readonly statuses: ActionStatusEntry[];
10
+ constructor(failures: RunFailure[], results: ActionResult[], statuses?: ActionStatusEntry[]);
11
+ }
12
+ export declare class TemplateRenderError extends BaseError {
13
+ constructor(file: string, cause: unknown);
14
+ }
15
+ export declare class TemplateNotFoundError extends BaseError {
16
+ constructor(generator: string, action: string);
17
+ }
18
+ export declare class InjectionTargetNotFoundError extends BaseError {
19
+ constructor(to: string);
20
+ }
21
+ export declare class InjectionAnchorNotFoundError extends BaseError {
22
+ constructor(anchor: string, to: string);
23
+ }
24
+ export declare class TemplateConfigError extends BaseError {
25
+ constructor(message: string);
26
+ }
27
+ export declare class ShellCommandError extends BaseError {
28
+ constructor(command: string, cause: unknown);
29
+ }
30
+ export declare class PathMacroResolutionError extends BaseError {
31
+ constructor(file: string, segment: string, identifier: string);
32
+ }
33
+ export declare class GlobMirrorConflictError extends BaseError {
34
+ constructor(file: string);
35
+ }
36
+ export declare class ConfigTsNodeError extends BaseError {
37
+ constructor(file: string, cause: unknown);
38
+ }
39
+ export declare class UnknownTransformError extends BaseError {
40
+ constructor(name: string);
41
+ }
42
+ export declare class SetupRepositoryError extends BaseError {
43
+ constructor(setup: string);
44
+ }
45
+ export declare class CliArgumentError extends BaseError {
46
+ constructor(message: string);
47
+ }
48
+ export declare class NameFlagRemovedError extends BaseError {
49
+ constructor(name: unknown);
50
+ }
@@ -0,0 +1,5 @@
1
+ import type { ActionResult, ActionStatusEntry, RenderedAction, RunnerConfig } from './types';
2
+ export declare const execute: (renderedActions: RenderedAction[], args: Record<string, any>, config: RunnerConfig) => Promise<{
3
+ results: ActionResult[];
4
+ statuses: ActionStatusEntry[];
5
+ }>;