@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.
- package/LICENSE +25 -0
- package/README.md +146 -2
- package/dist/cjs/bin.js +26 -0
- package/dist/cjs/index.js +17 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/src/cli-help.js +19 -0
- package/dist/cjs/src/config-resolver.js +66 -0
- package/dist/cjs/src/config.js +37 -0
- package/dist/cjs/src/context.js +37 -0
- package/dist/cjs/src/engine.js +149 -0
- package/dist/cjs/src/errors.js +116 -0
- package/dist/cjs/src/execute.js +48 -0
- package/dist/cjs/src/help.js +89 -0
- package/dist/cjs/src/helpers.js +24 -0
- package/dist/cjs/src/index.js +42 -0
- package/dist/cjs/src/logger.js +28 -0
- package/dist/cjs/src/newline.js +13 -0
- package/dist/cjs/src/ops/add.js +74 -0
- package/dist/cjs/src/ops/anchors.js +45 -0
- package/dist/cjs/src/ops/echo.js +17 -0
- package/dist/cjs/src/ops/index.js +51 -0
- package/dist/cjs/src/ops/inject.js +37 -0
- package/dist/cjs/src/ops/injector.js +49 -0
- package/dist/cjs/src/ops/result.js +9 -0
- package/dist/cjs/src/ops/setup.js +52 -0
- package/dist/cjs/src/ops/shell.js +38 -0
- package/dist/cjs/src/params.js +77 -0
- package/dist/cjs/src/path-macros.js +30 -0
- package/dist/cjs/src/prompt.js +111 -0
- package/dist/cjs/src/render.js +189 -0
- package/dist/cjs/src/runner.js +38 -0
- package/dist/cjs/src/types.js +2 -0
- package/dist/cjs/templates/init/repo/new-repo.ejs.t +4 -0
- package/dist/cjs/templates/init/self/new-generator-help.ejs.t +8 -0
- package/dist/cjs/templates/init/self/new-generator-with-prompt.ejs.t +21 -0
- package/dist/cjs/templates/init/self/new-generator-with-prompt.prompt.ejs.t +17 -0
- package/dist/cjs/templates/init/self/new-generator.ejs.t +21 -0
- package/dist/cjs/templates/init/self/new-repo.ejs.t +7 -0
- package/dist/esm/bin.js +21 -0
- package/dist/esm/index.js +1 -0
- package/dist/esm/package.json +3 -0
- package/dist/esm/src/cli-help.js +14 -0
- package/dist/esm/src/config-resolver.js +37 -0
- package/dist/esm/src/config.js +29 -0
- package/dist/esm/src/context.js +32 -0
- package/dist/esm/src/engine.js +118 -0
- package/dist/esm/src/errors.js +98 -0
- package/dist/esm/src/execute.js +41 -0
- package/dist/esm/src/help.js +78 -0
- package/dist/esm/src/helpers.js +19 -0
- package/dist/esm/src/index.js +6 -0
- package/dist/esm/src/logger.js +23 -0
- package/dist/esm/src/newline.js +11 -0
- package/dist/esm/src/ops/add.js +69 -0
- package/dist/esm/src/ops/anchors.js +41 -0
- package/dist/esm/src/ops/echo.js +12 -0
- package/dist/esm/src/ops/index.js +26 -0
- package/dist/esm/src/ops/inject.js +32 -0
- package/dist/esm/src/ops/injector.js +44 -0
- package/dist/esm/src/ops/result.js +7 -0
- package/dist/esm/src/ops/setup.js +47 -0
- package/dist/esm/src/ops/shell.js +33 -0
- package/dist/esm/src/params.js +71 -0
- package/dist/esm/src/path-macros.js +24 -0
- package/dist/esm/src/prompt.js +81 -0
- package/dist/esm/src/render.js +182 -0
- package/dist/esm/src/runner.js +32 -0
- package/dist/esm/src/types.js +1 -0
- package/dist/esm/templates/init/repo/new-repo.ejs.t +4 -0
- package/dist/esm/templates/init/self/new-generator-help.ejs.t +8 -0
- package/dist/esm/templates/init/self/new-generator-with-prompt.ejs.t +21 -0
- package/dist/esm/templates/init/self/new-generator-with-prompt.prompt.ejs.t +17 -0
- package/dist/esm/templates/init/self/new-generator.ejs.t +21 -0
- package/dist/esm/templates/init/self/new-repo.ejs.t +7 -0
- package/dist/types/bin.d.ts +2 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/src/cli-help.d.ts +3 -0
- package/dist/types/src/config-resolver.d.ts +4 -0
- package/dist/types/src/config.d.ts +14 -0
- package/dist/types/src/context.d.ts +12 -0
- package/dist/types/src/engine.d.ts +6 -0
- package/dist/types/src/errors.d.ts +50 -0
- package/dist/types/src/execute.d.ts +5 -0
- package/dist/types/src/help.d.ts +8 -0
- package/dist/types/src/helpers.d.ts +22 -0
- package/dist/types/src/index.d.ts +6 -0
- package/dist/types/src/logger.d.ts +10 -0
- package/dist/types/src/newline.d.ts +2 -0
- package/dist/types/src/ops/add.d.ts +3 -0
- package/dist/types/src/ops/anchors.d.ts +4 -0
- package/dist/types/src/ops/echo.d.ts +3 -0
- package/dist/types/src/ops/index.d.ts +4 -0
- package/dist/types/src/ops/inject.d.ts +3 -0
- package/dist/types/src/ops/injector.d.ts +3 -0
- package/dist/types/src/ops/result.d.ts +3 -0
- package/dist/types/src/ops/setup.d.ts +3 -0
- package/dist/types/src/ops/shell.d.ts +3 -0
- package/dist/types/src/params.d.ts +4 -0
- package/dist/types/src/path-macros.d.ts +3 -0
- package/dist/types/src/prompt.d.ts +8 -0
- package/dist/types/src/render.d.ts +2 -0
- package/dist/types/src/runner.d.ts +3 -0
- package/dist/types/src/types.d.ts +94 -0
- package/package.json +82 -4
|
@@ -0,0 +1,77 @@
|
|
|
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.DEFAULT_ACTION = void 0;
|
|
7
|
+
const path_1 = __importDefault(require("path"));
|
|
8
|
+
const yargs_parser_1 = __importDefault(require("yargs-parser"));
|
|
9
|
+
const fs_extra_1 = __importDefault(require("fs-extra"));
|
|
10
|
+
const errors_1 = require("./errors");
|
|
11
|
+
exports.DEFAULT_ACTION = '_default';
|
|
12
|
+
const splitList = (value) => value ? value.split(',') : undefined;
|
|
13
|
+
const resolvePositionals = async (templates, args) => {
|
|
14
|
+
let [generator, action, name] = args;
|
|
15
|
+
const actionList = splitList(action);
|
|
16
|
+
const nameList = splitList(name);
|
|
17
|
+
if (generator &&
|
|
18
|
+
action &&
|
|
19
|
+
(await fs_extra_1.default.pathExists(path_1.default.join(templates, generator, action)))) {
|
|
20
|
+
return { generator, actions: [action], names: nameList };
|
|
21
|
+
}
|
|
22
|
+
if (generator && action && actionList && actionList.length > 1) {
|
|
23
|
+
const missing = [];
|
|
24
|
+
for (const part of actionList) {
|
|
25
|
+
if (!(await fs_extra_1.default.pathExists(path_1.default.join(templates, generator, part)))) {
|
|
26
|
+
missing.push(part);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
if (missing.length === 0) {
|
|
30
|
+
return { generator, actions: actionList, names: nameList };
|
|
31
|
+
}
|
|
32
|
+
if (missing.length < actionList.length) {
|
|
33
|
+
throw new errors_1.TemplateNotFoundError(generator, missing[0]);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
if (generator &&
|
|
37
|
+
(await fs_extra_1.default.pathExists(path_1.default.join(templates, generator, exports.DEFAULT_ACTION)))) {
|
|
38
|
+
[generator, name] = args;
|
|
39
|
+
return { generator, actions: [exports.DEFAULT_ACTION], names: splitList(name) };
|
|
40
|
+
}
|
|
41
|
+
return { generator, actions: actionList, names: nameList };
|
|
42
|
+
};
|
|
43
|
+
const params = async ({ templates, legacyNameFlag }, externalArgv) => {
|
|
44
|
+
const argv = (0, yargs_parser_1.default)(externalArgv);
|
|
45
|
+
const { _, ...cleanArgv } = argv;
|
|
46
|
+
const legacyGate = process.env.HYGEN_LEGACY_NAME === '1' || legacyNameFlag === true;
|
|
47
|
+
if (Array.isArray(cleanArgv.name) && !legacyGate) {
|
|
48
|
+
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').");
|
|
49
|
+
}
|
|
50
|
+
if (cleanArgv.name !== undefined && !legacyGate) {
|
|
51
|
+
throw new errors_1.NameFlagRemovedError(cleanArgv.name);
|
|
52
|
+
}
|
|
53
|
+
const { generator, actions, names: positionalNames, } = await resolvePositionals(templates, argv._.map(String));
|
|
54
|
+
const names = legacyGate && cleanArgv.name !== undefined && positionalNames === undefined
|
|
55
|
+
? [cleanArgv.name]
|
|
56
|
+
: positionalNames;
|
|
57
|
+
if (!generator || !actions) {
|
|
58
|
+
return {
|
|
59
|
+
generator,
|
|
60
|
+
actions,
|
|
61
|
+
names,
|
|
62
|
+
templates,
|
|
63
|
+
cleanArgv,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
return {
|
|
67
|
+
templates,
|
|
68
|
+
generator,
|
|
69
|
+
actions,
|
|
70
|
+
names,
|
|
71
|
+
cleanArgv,
|
|
72
|
+
_,
|
|
73
|
+
ts: process.env.HYGEN_TS || new Date().getTime(),
|
|
74
|
+
...cleanArgv,
|
|
75
|
+
};
|
|
76
|
+
};
|
|
77
|
+
exports.default = params;
|
|
@@ -0,0 +1,30 @@
|
|
|
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.resolvePathMacros = void 0;
|
|
7
|
+
const path_1 = __importDefault(require("path"));
|
|
8
|
+
const errors_1 = require("./errors");
|
|
9
|
+
const context_1 = __importDefault(require("./context"));
|
|
10
|
+
const MACRO_PATTERN = /\$([A-Za-z_][A-Za-z0-9_]*)\$/g;
|
|
11
|
+
const macroDictionary = (args, config) => {
|
|
12
|
+
const ctx = (0, context_1.default)(args, config);
|
|
13
|
+
return { ...ctx, ...ctx.h };
|
|
14
|
+
};
|
|
15
|
+
const resolvePathMacros = (rel, args, config, file) => {
|
|
16
|
+
if (!rel.includes('$'))
|
|
17
|
+
return rel;
|
|
18
|
+
const dict = macroDictionary(args, config);
|
|
19
|
+
return rel
|
|
20
|
+
.split(path_1.default.sep)
|
|
21
|
+
.map((segment) => segment.replace(MACRO_PATTERN, (_match, identifier) => {
|
|
22
|
+
const value = dict[identifier];
|
|
23
|
+
if (value === undefined || value === null) {
|
|
24
|
+
throw new errors_1.PathMacroResolutionError(file, segment, identifier);
|
|
25
|
+
}
|
|
26
|
+
return String(value);
|
|
27
|
+
}))
|
|
28
|
+
.join(path_1.default.sep);
|
|
29
|
+
};
|
|
30
|
+
exports.resolvePathMacros = resolvePathMacros;
|
|
@@ -0,0 +1,111 @@
|
|
|
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
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.pickGeneratorAction = void 0;
|
|
30
|
+
const path_1 = __importDefault(require("path"));
|
|
31
|
+
const fs_1 = __importDefault(require("fs"));
|
|
32
|
+
const helpers_1 = __importDefault(require("./helpers"));
|
|
33
|
+
const help_1 = require("./help");
|
|
34
|
+
const hooksfiles = ['index.js', 'index.ts', 'prompt.cjs', 'prompt.js', 'prompt.ts'];
|
|
35
|
+
const prompt = async (createPrompter, actionfolder, args) => {
|
|
36
|
+
const hooksfile = hooksfiles
|
|
37
|
+
.map((f) => path_1.default.resolve(path_1.default.join(actionfolder, f)))
|
|
38
|
+
.find((f) => fs_1.default.existsSync(f));
|
|
39
|
+
if (!hooksfile) {
|
|
40
|
+
return Promise.resolve({});
|
|
41
|
+
}
|
|
42
|
+
const isTypeScriptHook = /\.ts$/.test(hooksfile);
|
|
43
|
+
if (isTypeScriptHook) {
|
|
44
|
+
require('ts-node/register/transpile-only');
|
|
45
|
+
}
|
|
46
|
+
let hooksModule = await Promise.resolve(`${hooksfile}`).then(s => __importStar(require(s)));
|
|
47
|
+
if (hooksModule.default) {
|
|
48
|
+
hooksModule = hooksModule.default;
|
|
49
|
+
}
|
|
50
|
+
if (hooksModule.params) {
|
|
51
|
+
return hooksModule.params({ args, h: helpers_1.default });
|
|
52
|
+
}
|
|
53
|
+
const prompter = createPrompter();
|
|
54
|
+
if (hooksModule.prompt) {
|
|
55
|
+
return hooksModule.prompt({
|
|
56
|
+
prompter,
|
|
57
|
+
inquirer: prompter,
|
|
58
|
+
args,
|
|
59
|
+
h: helpers_1.default,
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
return prompter.prompt(hooksModule.filter((p) => args[p.name] === undefined || args[p.name] === null || args[p.name].length === 0));
|
|
63
|
+
};
|
|
64
|
+
const answerValue = (answer, name) => {
|
|
65
|
+
const value = answer[name];
|
|
66
|
+
return typeof value === 'string' && value.length > 0 ? value : undefined;
|
|
67
|
+
};
|
|
68
|
+
const pickGeneratorAction = async (config) => {
|
|
69
|
+
const { templates, logger } = config;
|
|
70
|
+
if (!templates || !fs_1.default.existsSync(templates)) {
|
|
71
|
+
logger.log(`No generators found.
|
|
72
|
+
|
|
73
|
+
The interactive picker needs a _templates folder here, or anywhere up the folder tree.
|
|
74
|
+
Create one with: $ hygen init self
|
|
75
|
+
`);
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
const generators = (0, help_1.pickableGenerators)(templates);
|
|
79
|
+
if (generators.length === 0) {
|
|
80
|
+
logger.log(`No generators found.
|
|
81
|
+
|
|
82
|
+
Every top-level folder in _templates should hold at least one action subfolder.
|
|
83
|
+
Create one with: $ hygen with-prompt new my-generator
|
|
84
|
+
`);
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
const prompter = config.createPrompter();
|
|
88
|
+
const generatorAnswer = await prompter.prompt({
|
|
89
|
+
type: 'select',
|
|
90
|
+
name: 'generator',
|
|
91
|
+
message: 'Select a generator:',
|
|
92
|
+
choices: generators,
|
|
93
|
+
});
|
|
94
|
+
const generator = answerValue(generatorAnswer, 'generator');
|
|
95
|
+
if (!generator)
|
|
96
|
+
return null;
|
|
97
|
+
const actions = (0, help_1.generatorActions)(templates, generator);
|
|
98
|
+
const actionPrompter = config.createPrompter();
|
|
99
|
+
const actionAnswer = await actionPrompter.prompt({
|
|
100
|
+
type: 'select',
|
|
101
|
+
name: 'action',
|
|
102
|
+
message: `Select an action for ${generator}:`,
|
|
103
|
+
choices: actions,
|
|
104
|
+
});
|
|
105
|
+
const action = answerValue(actionAnswer, 'action');
|
|
106
|
+
if (!action)
|
|
107
|
+
return null;
|
|
108
|
+
return { generator, action };
|
|
109
|
+
};
|
|
110
|
+
exports.pickGeneratorAction = pickGeneratorAction;
|
|
111
|
+
exports.default = prompt;
|
|
@@ -0,0 +1,189 @@
|
|
|
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.render = void 0;
|
|
7
|
+
const path_1 = __importDefault(require("path"));
|
|
8
|
+
const fs_extra_1 = __importDefault(require("fs-extra"));
|
|
9
|
+
const ejs_1 = __importDefault(require("ejs"));
|
|
10
|
+
const fast_glob_1 = __importDefault(require("fast-glob"));
|
|
11
|
+
const front_matter_1 = __importDefault(require("front-matter"));
|
|
12
|
+
const ignore_walk_1 = __importDefault(require("ignore-walk"));
|
|
13
|
+
const debug_1 = __importDefault(require("debug"));
|
|
14
|
+
const context_1 = __importDefault(require("./context"));
|
|
15
|
+
const errors_1 = require("./errors");
|
|
16
|
+
const debug = (0, debug_1.default)('hygen:render');
|
|
17
|
+
const evaluateSkip = (rendered) => {
|
|
18
|
+
if (rendered === undefined)
|
|
19
|
+
return undefined;
|
|
20
|
+
if (rendered === '' || rendered === false)
|
|
21
|
+
return undefined;
|
|
22
|
+
if (rendered === true)
|
|
23
|
+
return true;
|
|
24
|
+
const value = String(rendered).trim();
|
|
25
|
+
if (value === '')
|
|
26
|
+
return undefined;
|
|
27
|
+
if (value.toLowerCase() === 'false')
|
|
28
|
+
return undefined;
|
|
29
|
+
if (value.toLowerCase() === 'true')
|
|
30
|
+
return true;
|
|
31
|
+
return value;
|
|
32
|
+
};
|
|
33
|
+
const map = (f) => (arr) => arr.map(f);
|
|
34
|
+
const filter = (f) => (arr) => arr.filter(f);
|
|
35
|
+
const ignores = [
|
|
36
|
+
'prompt.js',
|
|
37
|
+
'index.js',
|
|
38
|
+
'prompt.ts',
|
|
39
|
+
'index.ts',
|
|
40
|
+
'.hygenignore',
|
|
41
|
+
'.DS_Store',
|
|
42
|
+
'.Spotlight-V100',
|
|
43
|
+
'.Trashes',
|
|
44
|
+
'ehthumbs.db',
|
|
45
|
+
'Thumbs.db',
|
|
46
|
+
];
|
|
47
|
+
const renderTemplate = (tmpl, locals, config, file) => {
|
|
48
|
+
if (typeof tmpl !== 'string')
|
|
49
|
+
return tmpl;
|
|
50
|
+
try {
|
|
51
|
+
return ejs_1.default.render(tmpl, (0, context_1.default)(locals, config), {
|
|
52
|
+
escape: (s) => s,
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
throw new errors_1.TemplateRenderError(file, error);
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
async function getFiles(dir) {
|
|
60
|
+
const files = ignore_walk_1.default
|
|
61
|
+
.sync({ path: dir, ignoreFiles: ['.hygenignore'] })
|
|
62
|
+
.map((f) => path_1.default.join(dir, f));
|
|
63
|
+
return files;
|
|
64
|
+
}
|
|
65
|
+
const applyTransforms = async (content, attributes, locals, config) => {
|
|
66
|
+
const nameList = attributes.transform;
|
|
67
|
+
if (nameList == null)
|
|
68
|
+
return content;
|
|
69
|
+
const hasFileOutput = Boolean(attributes.to) || (attributes.mirror === true && attributes.sh === undefined);
|
|
70
|
+
if (attributes.sh !== undefined && !hasFileOutput) {
|
|
71
|
+
config.logger?.warn(`transform [${String(nameList)}] is ignored for sh: action (no file output)`);
|
|
72
|
+
return content;
|
|
73
|
+
}
|
|
74
|
+
const transforms = config.transforms ?? {};
|
|
75
|
+
let result = content;
|
|
76
|
+
for (const rawName of String(nameList).split('|')) {
|
|
77
|
+
const name = rawName.trim();
|
|
78
|
+
if (!name)
|
|
79
|
+
continue;
|
|
80
|
+
const transform = transforms[name];
|
|
81
|
+
if (typeof transform !== 'function') {
|
|
82
|
+
throw new errors_1.UnknownTransformError(name);
|
|
83
|
+
}
|
|
84
|
+
result = await transform(result, locals);
|
|
85
|
+
}
|
|
86
|
+
return result;
|
|
87
|
+
};
|
|
88
|
+
const render = async (args, config) => getFiles(args.actionfolder)
|
|
89
|
+
.then((things) => things.sort((a, b) => a.localeCompare(b)))
|
|
90
|
+
.then(filter((f) => !ignores.find((ig) => f.endsWith(ig))))
|
|
91
|
+
.then(filter((file) => args.subaction
|
|
92
|
+
? !!file.replace(args.actionfolder, '').match(args.subaction)
|
|
93
|
+
: true))
|
|
94
|
+
.then(map((file) => fs_extra_1.default.readFile(file).then((text) => ({ file, text: text.toString() }))))
|
|
95
|
+
.then((_) => Promise.all(_))
|
|
96
|
+
.then(map(({ file, text }) => {
|
|
97
|
+
debug('Pre-formatting file: %o', file);
|
|
98
|
+
return { file, ...(0, front_matter_1.default)(text, { allowUnsafe: true }) };
|
|
99
|
+
}))
|
|
100
|
+
.then(map(({ file, attributes, body, }) => expandItem(file, attributes, body, args, config)))
|
|
101
|
+
.then((_) => Promise.all(_))
|
|
102
|
+
.then((_) => _.flat());
|
|
103
|
+
exports.render = render;
|
|
104
|
+
const expandItem = async (file, attributes, body, args, config) => {
|
|
105
|
+
const renderedAttrs = Object.entries(attributes).reduce((obj, [key, value]) => {
|
|
106
|
+
return {
|
|
107
|
+
...obj,
|
|
108
|
+
[key]: renderTemplate(value, args, config, file),
|
|
109
|
+
};
|
|
110
|
+
}, {});
|
|
111
|
+
debug('Rendering file: %o', file);
|
|
112
|
+
if (renderedAttrs.glob) {
|
|
113
|
+
if (renderedAttrs.mirror === true) {
|
|
114
|
+
throw new errors_1.GlobMirrorConflictError(file);
|
|
115
|
+
}
|
|
116
|
+
return expandGlob(file, renderedAttrs, args, config);
|
|
117
|
+
}
|
|
118
|
+
const locals = { ...args, attributes: renderedAttrs };
|
|
119
|
+
const skip = evaluateSkip(renderedAttrs.skip);
|
|
120
|
+
if (skip !== undefined) {
|
|
121
|
+
return [{ file, attributes: renderedAttrs, body: '', skip }];
|
|
122
|
+
}
|
|
123
|
+
return [
|
|
124
|
+
{
|
|
125
|
+
file,
|
|
126
|
+
attributes: renderedAttrs,
|
|
127
|
+
body: await applyTransforms(renderTemplate(body, locals, config, file), renderedAttrs, locals, config),
|
|
128
|
+
},
|
|
129
|
+
];
|
|
130
|
+
};
|
|
131
|
+
const expandGlob = async (file, attributes, args, config) => {
|
|
132
|
+
const skip = evaluateSkip(attributes.skip);
|
|
133
|
+
if (skip !== undefined) {
|
|
134
|
+
return [{ file, attributes: {}, body: '', skip }];
|
|
135
|
+
}
|
|
136
|
+
const baseDir = path_1.default.dirname(file);
|
|
137
|
+
const matches = (await (0, fast_glob_1.default)(attributes.glob, {
|
|
138
|
+
cwd: baseDir,
|
|
139
|
+
absolute: true,
|
|
140
|
+
onlyFiles: true,
|
|
141
|
+
dot: false,
|
|
142
|
+
})).sort();
|
|
143
|
+
const basePrefix = attributes.base
|
|
144
|
+
? attributes.base.replace(/[\\/]+$/, '')
|
|
145
|
+
: undefined;
|
|
146
|
+
const stripExts = attributes.stripExtensions == null
|
|
147
|
+
? []
|
|
148
|
+
: Array.isArray(attributes.stripExtensions)
|
|
149
|
+
? attributes.stripExtensions
|
|
150
|
+
: [attributes.stripExtensions];
|
|
151
|
+
const destFolder = (attributes.to ?? attributes.destination);
|
|
152
|
+
if (!destFolder) {
|
|
153
|
+
throw new errors_1.TemplateConfigError(`glob template [${file}] must declare a 'to:' (or 'destination:') destination folder`);
|
|
154
|
+
}
|
|
155
|
+
const renderedItems = [];
|
|
156
|
+
for (const match of matches) {
|
|
157
|
+
let rel = path_1.default.relative(baseDir, match);
|
|
158
|
+
if (basePrefix) {
|
|
159
|
+
if (rel === basePrefix) {
|
|
160
|
+
rel = '';
|
|
161
|
+
}
|
|
162
|
+
else if (rel.startsWith(basePrefix + path_1.default.sep)) {
|
|
163
|
+
rel = rel.slice(basePrefix.length + 1);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
for (const ext of stripExts) {
|
|
167
|
+
if (ext && rel.endsWith(ext)) {
|
|
168
|
+
rel = rel.slice(0, -ext.length);
|
|
169
|
+
break;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
const renderedRel = renderTemplate(rel, args, config, file);
|
|
173
|
+
const outAttributes = {
|
|
174
|
+
to: path_1.default.join(destFolder, renderedRel),
|
|
175
|
+
};
|
|
176
|
+
if (attributes.skipIfExists !== undefined) {
|
|
177
|
+
outAttributes.skipIfExists = attributes.skipIfExists;
|
|
178
|
+
}
|
|
179
|
+
if (attributes.transform !== undefined) {
|
|
180
|
+
outAttributes.transform = attributes.transform;
|
|
181
|
+
}
|
|
182
|
+
renderedItems.push({
|
|
183
|
+
file: match,
|
|
184
|
+
attributes: outAttributes,
|
|
185
|
+
body: await applyTransforms(renderTemplate((await fs_extra_1.default.readFile(match)).toString(), args, config, match), outAttributes, args, config),
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
return renderedItems;
|
|
189
|
+
};
|
|
@@ -0,0 +1,38 @@
|
|
|
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.runner = void 0;
|
|
7
|
+
const config_resolver_1 = __importDefault(require("./config-resolver"));
|
|
8
|
+
const engine_1 = require("./engine");
|
|
9
|
+
const help_1 = require("./help");
|
|
10
|
+
const runner = async (argv, config) => {
|
|
11
|
+
const resolvedConfig = await (0, config_resolver_1.default)(config);
|
|
12
|
+
const { templates, logger } = resolvedConfig;
|
|
13
|
+
try {
|
|
14
|
+
const actions = await (0, engine_1.engine)(argv, resolvedConfig);
|
|
15
|
+
return { success: true, actions, statuses: actions.statuses, time: 0 };
|
|
16
|
+
}
|
|
17
|
+
catch (err) {
|
|
18
|
+
const e = err;
|
|
19
|
+
logger.log(e.toString());
|
|
20
|
+
if (resolvedConfig.debug) {
|
|
21
|
+
logger.log('details -----------');
|
|
22
|
+
logger.log(e.stack ?? '');
|
|
23
|
+
logger.log('-------------------');
|
|
24
|
+
}
|
|
25
|
+
if (err instanceof engine_1.ShowHelpError) {
|
|
26
|
+
(0, help_1.printHelp)(templates, logger);
|
|
27
|
+
}
|
|
28
|
+
const keepGoing = err instanceof engine_1.KeepGoingError ? err : undefined;
|
|
29
|
+
return {
|
|
30
|
+
success: false,
|
|
31
|
+
actions: keepGoing ? keepGoing.results : [],
|
|
32
|
+
statuses: keepGoing ? keepGoing.statuses : [],
|
|
33
|
+
...(keepGoing ? { failures: keepGoing.failures } : {}),
|
|
34
|
+
time: 0,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
exports.runner = runner;
|
|
@@ -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
|
+
|
package/dist/esm/bin.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import Logger from './src/logger.js';
|
|
4
|
+
import { isVersionFlag, VERSION } from './src/help.js';
|
|
5
|
+
import { runner } from './index.js';
|
|
6
|
+
const defaultTemplates = path.join(`${process.platform === 'win32' ? '' : '/'}${/file:\/{2,3}(.+)\/[^/]/.exec(import.meta.url)[1]}`, 'templates');
|
|
7
|
+
if (isVersionFlag(process.argv.slice(2))) {
|
|
8
|
+
console.log(`Hygen v${VERSION}`);
|
|
9
|
+
process.exit(0);
|
|
10
|
+
}
|
|
11
|
+
runner(process.argv.slice(2), {
|
|
12
|
+
templates: defaultTemplates,
|
|
13
|
+
cwd: process.cwd(),
|
|
14
|
+
logger: new Logger(console.log.bind(console)),
|
|
15
|
+
debug: !!process.env.DEBUG,
|
|
16
|
+
exec: (action, body) => {
|
|
17
|
+
const opts = body && body.length > 0 ? { input: body } : {};
|
|
18
|
+
return require('execa').command(action, { ...opts, shell: true });
|
|
19
|
+
},
|
|
20
|
+
createPrompter: () => require('enquirer'),
|
|
21
|
+
}).then(({ success }) => process.exit(success ? 0 : 1));
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './src/index.js';
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
const USAGE = `
|
|
2
|
+
Usage:
|
|
3
|
+
hygen [option] GENERATOR ACTION[,ACTION...] [NAME[,NAME...]] [data-options]
|
|
4
|
+
|
|
5
|
+
Options:
|
|
6
|
+
-h, --help # Show this message and quit
|
|
7
|
+
--dry # Perform a dry run. Files will be generated but not saved.
|
|
8
|
+
--keep-going # Continue after a failed action, reporting every failure.
|
|
9
|
+
HYGEN_LEGACY_NAME=1 # Re-enable the removed --name flag (legacy escape).`;
|
|
10
|
+
export const isHelpFlag = (argv) => ['-h', '--help'].includes(argv[0]);
|
|
11
|
+
export const printUsage = (logger) => {
|
|
12
|
+
logger.log(USAGE);
|
|
13
|
+
process.exit(0);
|
|
14
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import fs from 'fs-extra';
|
|
3
|
+
import { ConfigResolver } from './config.js';
|
|
4
|
+
import { ConfigTsNodeError } from './errors.js';
|
|
5
|
+
export const CONFIG_FILES = ['.hygen.js', '.hygen.cjs', '.hygen.mjs', '.hygen.ts'];
|
|
6
|
+
const configResolver = new ConfigResolver(CONFIG_FILES, {
|
|
7
|
+
exists: (f) => fs.pathExists(f),
|
|
8
|
+
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
|
+
}
|
|
17
|
+
return await import(f);
|
|
18
|
+
},
|
|
19
|
+
none: (_) => ({}),
|
|
20
|
+
});
|
|
21
|
+
const resolve = (cwd, defaultTemplates, templatesOverride) => {
|
|
22
|
+
return [
|
|
23
|
+
templatesOverride && path.resolve(cwd, templatesOverride),
|
|
24
|
+
process.env.HYGEN_TMPLS,
|
|
25
|
+
path.resolve(cwd, '_templates'),
|
|
26
|
+
defaultTemplates && path.resolve(cwd, defaultTemplates),
|
|
27
|
+
].find((_) => _ && fs.existsSync(_));
|
|
28
|
+
};
|
|
29
|
+
export default async (config) => {
|
|
30
|
+
const { cwd, templates: defaultTemplates, templatesOverride } = config;
|
|
31
|
+
const resolvedTemplates = resolve(cwd, defaultTemplates, templatesOverride);
|
|
32
|
+
return {
|
|
33
|
+
...config,
|
|
34
|
+
templates: resolvedTemplates,
|
|
35
|
+
...(await configResolver.resolve(cwd)),
|
|
36
|
+
};
|
|
37
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import importedPath from 'path';
|
|
2
|
+
const uniq = (arr) => arr.filter((elem, pos, a) => a.indexOf(elem) === pos);
|
|
3
|
+
const reversePathsToWalk = ({ folder, path, }) => {
|
|
4
|
+
const resolved = path.resolve(folder);
|
|
5
|
+
const parts = resolved.split(path.sep);
|
|
6
|
+
const results = parts.map((_, idx, arr) => arr.slice(0, idx + 1).join(path.sep));
|
|
7
|
+
results[0] = results[0] || '/';
|
|
8
|
+
return results.reverse();
|
|
9
|
+
};
|
|
10
|
+
const configLookup = (file, folder, path = importedPath) => uniq(reversePathsToWalk({ folder, path }).map((p) => path.join(p, file)));
|
|
11
|
+
class ConfigResolver {
|
|
12
|
+
constructor(configFiles, io) {
|
|
13
|
+
this.configFiles = configFiles;
|
|
14
|
+
this.io = io;
|
|
15
|
+
}
|
|
16
|
+
async resolve(from) {
|
|
17
|
+
const { exists, load, none } = this.io;
|
|
18
|
+
for (const dir of reversePathsToWalk({ folder: from, path: importedPath })) {
|
|
19
|
+
for (const file of this.configFiles) {
|
|
20
|
+
const candidate = importedPath.join(dir, file);
|
|
21
|
+
if (await exists(candidate)) {
|
|
22
|
+
return load(candidate);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return none(from);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
export { configLookup, ConfigResolver, reversePathsToWalk };
|