@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,48 @@
|
|
|
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.execute = void 0;
|
|
7
|
+
const debug_1 = __importDefault(require("debug"));
|
|
8
|
+
const ops_1 = __importDefault(require("./ops"));
|
|
9
|
+
const debug = (0, debug_1.default)('hygen:execute');
|
|
10
|
+
const execute = async (renderedActions, args, config) => {
|
|
11
|
+
const { logger } = config;
|
|
12
|
+
const messages = [];
|
|
13
|
+
const results = [];
|
|
14
|
+
const statuses = [];
|
|
15
|
+
for (const action of renderedActions) {
|
|
16
|
+
const pair = {
|
|
17
|
+
action: args.action,
|
|
18
|
+
...(args.name !== undefined ? { name: args.name } : {}),
|
|
19
|
+
};
|
|
20
|
+
if (action.skip !== undefined) {
|
|
21
|
+
logger.warn(action.skip === true ? ' skipped' : ` skipped: ${action.skip}`);
|
|
22
|
+
statuses.push({
|
|
23
|
+
...pair,
|
|
24
|
+
status: 'skipped',
|
|
25
|
+
...(typeof action.skip === 'string' ? { reason: action.skip } : {}),
|
|
26
|
+
});
|
|
27
|
+
debug('skipping action: %o', action.file);
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
const { message } = action.attributes;
|
|
31
|
+
if (message) {
|
|
32
|
+
messages.push(message);
|
|
33
|
+
}
|
|
34
|
+
const ops = await (0, ops_1.default)(action.attributes);
|
|
35
|
+
debug('executing %o ops', ops.length);
|
|
36
|
+
for (const op of ops) {
|
|
37
|
+
debug('executing: %o', op);
|
|
38
|
+
results.push(await op(action, args, config));
|
|
39
|
+
}
|
|
40
|
+
statuses.push({ ...pair, status: 'ok' });
|
|
41
|
+
debug('executing ops: done');
|
|
42
|
+
}
|
|
43
|
+
if (messages.length > 0) {
|
|
44
|
+
logger.colorful(`${args.action}:\n${messages.join('\n')}`);
|
|
45
|
+
}
|
|
46
|
+
return { results, statuses };
|
|
47
|
+
};
|
|
48
|
+
exports.execute = execute;
|
|
@@ -0,0 +1,89 @@
|
|
|
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.isVersionFlag = exports.VERSION = exports.printHelp = exports.generatorActions = exports.pickableGenerators = exports.availableActions = void 0;
|
|
7
|
+
const fs_1 = __importDefault(require("fs"));
|
|
8
|
+
const path_1 = __importDefault(require("path"));
|
|
9
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
10
|
+
const params_1 = require("./params");
|
|
11
|
+
const readVersion = () => {
|
|
12
|
+
let dir = __dirname;
|
|
13
|
+
while (true) {
|
|
14
|
+
const candidate = path_1.default.join(dir, 'package.json');
|
|
15
|
+
if (fs_1.default.existsSync(candidate)) {
|
|
16
|
+
try {
|
|
17
|
+
const pkg = JSON.parse(fs_1.default.readFileSync(candidate, 'utf8'));
|
|
18
|
+
if (typeof pkg.version === 'string' && pkg.version.length > 0)
|
|
19
|
+
return pkg.version;
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
const parent = path_1.default.dirname(dir);
|
|
25
|
+
if (parent === dir)
|
|
26
|
+
break;
|
|
27
|
+
dir = parent;
|
|
28
|
+
}
|
|
29
|
+
return 'unknown';
|
|
30
|
+
};
|
|
31
|
+
const VERSION = readVersion();
|
|
32
|
+
exports.VERSION = VERSION;
|
|
33
|
+
const isVersionFlag = (args) => args.includes('--version') || args.includes('-v');
|
|
34
|
+
exports.isVersionFlag = isVersionFlag;
|
|
35
|
+
const availableActions = (templates) => {
|
|
36
|
+
const generators = fs_1.default
|
|
37
|
+
.readdirSync(templates)
|
|
38
|
+
.filter((_) => fs_1.default.lstatSync(path_1.default.join(templates, _)).isDirectory());
|
|
39
|
+
return generators.reduce((acc, generator) => {
|
|
40
|
+
const actions = fs_1.default.readdirSync(path_1.default.join(templates, generator));
|
|
41
|
+
acc[generator] = actions;
|
|
42
|
+
return acc;
|
|
43
|
+
}, {});
|
|
44
|
+
};
|
|
45
|
+
exports.availableActions = availableActions;
|
|
46
|
+
const pickableGenerators = (templates) => fs_1.default.readdirSync(templates).filter((entry) => {
|
|
47
|
+
const full = path_1.default.join(templates, entry);
|
|
48
|
+
if (!fs_1.default.lstatSync(full).isDirectory())
|
|
49
|
+
return false;
|
|
50
|
+
return fs_1.default
|
|
51
|
+
.readdirSync(full)
|
|
52
|
+
.some((sub) => fs_1.default.lstatSync(path_1.default.join(full, sub)).isDirectory());
|
|
53
|
+
});
|
|
54
|
+
exports.pickableGenerators = pickableGenerators;
|
|
55
|
+
const generatorActions = (templates, generator) => fs_1.default
|
|
56
|
+
.readdirSync(path_1.default.join(templates, generator))
|
|
57
|
+
.filter((entry) => fs_1.default.lstatSync(path_1.default.join(templates, generator, entry)).isDirectory());
|
|
58
|
+
exports.generatorActions = generatorActions;
|
|
59
|
+
const printHelp = (templates, logger) => {
|
|
60
|
+
logger.log(`Hygen v${VERSION}`);
|
|
61
|
+
logger.log('\nAvailable actions:');
|
|
62
|
+
if (!templates) {
|
|
63
|
+
logger.log(`No generators or actions found.
|
|
64
|
+
|
|
65
|
+
This means I didn't find a _templates folder right here,
|
|
66
|
+
or anywhere up the folder tree starting here.
|
|
67
|
+
|
|
68
|
+
Here's how to start using Hygen:
|
|
69
|
+
|
|
70
|
+
$ hygen init self
|
|
71
|
+
$ hygen with-prompt new my-generator
|
|
72
|
+
|
|
73
|
+
(edit your generator in _templates/my-generator)
|
|
74
|
+
|
|
75
|
+
$ hygen my-generator
|
|
76
|
+
|
|
77
|
+
See http://hygen.io for more.
|
|
78
|
+
|
|
79
|
+
`);
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
Object.entries(availableActions(templates)).forEach(([k, v]) => {
|
|
83
|
+
logger.log(`${chalk_1.default.bold(k)}: ${v.find((a) => a === params_1.DEFAULT_ACTION) ? `${k}${v.length > 1 ? ',' : ''} ` : ''}${v
|
|
84
|
+
.filter((a) => a !== params_1.DEFAULT_ACTION)
|
|
85
|
+
.map((a) => `${k} ${a}`)
|
|
86
|
+
.join(', ')}`);
|
|
87
|
+
});
|
|
88
|
+
};
|
|
89
|
+
exports.printHelp = printHelp;
|
|
@@ -0,0 +1,24 @@
|
|
|
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
|
+
const path_1 = __importDefault(require("path"));
|
|
7
|
+
const inflection_1 = __importDefault(require("inflection"));
|
|
8
|
+
const change_case_1 = __importDefault(require("change-case"));
|
|
9
|
+
const undasherize = (str) => str
|
|
10
|
+
.split(/[-_]/)
|
|
11
|
+
.map((w) => w[0].toUpperCase() + w.slice(1).toLowerCase())
|
|
12
|
+
.join('');
|
|
13
|
+
inflection_1.default.undasherize = undasherize;
|
|
14
|
+
const inflected = inflection_1.default;
|
|
15
|
+
const helpers = {
|
|
16
|
+
capitalize(str) {
|
|
17
|
+
const toBeCapitalized = String(str);
|
|
18
|
+
return toBeCapitalized.charAt(0).toUpperCase() + toBeCapitalized.slice(1);
|
|
19
|
+
},
|
|
20
|
+
inflection: inflected,
|
|
21
|
+
changeCase: change_case_1.default,
|
|
22
|
+
path: path_1.default,
|
|
23
|
+
};
|
|
24
|
+
exports.default = helpers;
|
|
@@ -0,0 +1,42 @@
|
|
|
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.errors = exports.Logger = exports.VERSION = exports.availableActions = exports.printHelp = exports.resolve = exports.engine = exports.runner = void 0;
|
|
30
|
+
var runner_1 = require("./runner");
|
|
31
|
+
Object.defineProperty(exports, "runner", { enumerable: true, get: function () { return runner_1.runner; } });
|
|
32
|
+
var engine_1 = require("./engine");
|
|
33
|
+
Object.defineProperty(exports, "engine", { enumerable: true, get: function () { return engine_1.engine; } });
|
|
34
|
+
var config_resolver_1 = require("./config-resolver");
|
|
35
|
+
Object.defineProperty(exports, "resolve", { enumerable: true, get: function () { return __importDefault(config_resolver_1).default; } });
|
|
36
|
+
var help_1 = require("./help");
|
|
37
|
+
Object.defineProperty(exports, "printHelp", { enumerable: true, get: function () { return help_1.printHelp; } });
|
|
38
|
+
Object.defineProperty(exports, "availableActions", { enumerable: true, get: function () { return help_1.availableActions; } });
|
|
39
|
+
Object.defineProperty(exports, "VERSION", { enumerable: true, get: function () { return help_1.VERSION; } });
|
|
40
|
+
var logger_1 = require("./logger");
|
|
41
|
+
Object.defineProperty(exports, "Logger", { enumerable: true, get: function () { return __importDefault(logger_1).default; } });
|
|
42
|
+
exports.errors = __importStar(require("./errors"));
|
|
@@ -0,0 +1,28 @@
|
|
|
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
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
7
|
+
const { yellow, red, green, magenta, template } = chalk_1.default;
|
|
8
|
+
class Logger {
|
|
9
|
+
constructor(log) {
|
|
10
|
+
this.log = log;
|
|
11
|
+
}
|
|
12
|
+
colorful(msg) {
|
|
13
|
+
this.log(template(chalk_1.default, msg));
|
|
14
|
+
}
|
|
15
|
+
notice(msg) {
|
|
16
|
+
this.log(magenta(msg));
|
|
17
|
+
}
|
|
18
|
+
warn(msg) {
|
|
19
|
+
this.log(yellow(msg));
|
|
20
|
+
}
|
|
21
|
+
err(msg) {
|
|
22
|
+
this.log(red(msg));
|
|
23
|
+
}
|
|
24
|
+
ok(msg) {
|
|
25
|
+
this.log(green(msg));
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
exports.default = Logger;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const os_1 = require("os");
|
|
4
|
+
const newline = (string) => {
|
|
5
|
+
const newlines = string.match(/(?:\r?\n)/g) || [];
|
|
6
|
+
if (newlines.length === 0) {
|
|
7
|
+
return os_1.EOL;
|
|
8
|
+
}
|
|
9
|
+
const crlf = newlines.filter((newline) => newline === '\r\n').length;
|
|
10
|
+
const lf = newlines.length - crlf;
|
|
11
|
+
return crlf > lf ? '\r\n' : '\n';
|
|
12
|
+
};
|
|
13
|
+
exports.default = newline;
|
|
@@ -0,0 +1,74 @@
|
|
|
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
|
+
const path_1 = __importDefault(require("path"));
|
|
7
|
+
const fs_extra_1 = __importDefault(require("fs-extra"));
|
|
8
|
+
const chalk_1 = require("chalk");
|
|
9
|
+
const result_1 = __importDefault(require("./result"));
|
|
10
|
+
const path_macros_1 = require("../path-macros");
|
|
11
|
+
const targetPath = (action, args, config) => {
|
|
12
|
+
const { attributes } = action;
|
|
13
|
+
if (attributes.to)
|
|
14
|
+
return attributes.to;
|
|
15
|
+
if (attributes.mirror !== true || attributes.sh !== undefined || !action.file) {
|
|
16
|
+
return undefined;
|
|
17
|
+
}
|
|
18
|
+
const rel = path_1.default.relative(args.actionfolder, action.file);
|
|
19
|
+
const stripped = process.env.HYGEN_MIRROR_EJS_SUFFIX === '1'
|
|
20
|
+
? rel.replace(/\.ejs\.t$/, '')
|
|
21
|
+
: rel.replace(/\.t$/, '');
|
|
22
|
+
return (0, path_macros_1.resolvePathMacros)(stripped, args, config, action.file);
|
|
23
|
+
};
|
|
24
|
+
const add = async (action, args, config) => {
|
|
25
|
+
const { logger, cwd, createPrompter } = config;
|
|
26
|
+
const { attributes: { inject, unless_exists, force, from, skip_if, skipIfExists }, } = action;
|
|
27
|
+
const to = targetPath(action, args, config);
|
|
28
|
+
const result = (0, result_1.default)('add', to);
|
|
29
|
+
const prompter = createPrompter();
|
|
30
|
+
if (!to || inject) {
|
|
31
|
+
return result('ignored');
|
|
32
|
+
}
|
|
33
|
+
const absTo = path_1.default.resolve(cwd, to);
|
|
34
|
+
const shouldNotOverwrite = !force && unless_exists !== undefined && unless_exists === true;
|
|
35
|
+
const fileExists = await fs_extra_1.default.pathExists(absTo);
|
|
36
|
+
if (skipIfExists === true && fileExists) {
|
|
37
|
+
logger.warn(` skipped: ${to}`);
|
|
38
|
+
return result('skipped');
|
|
39
|
+
}
|
|
40
|
+
if (shouldNotOverwrite && fileExists) {
|
|
41
|
+
logger.warn(` skipped: ${to}`);
|
|
42
|
+
return result('skipped');
|
|
43
|
+
}
|
|
44
|
+
if (!process.env.HYGEN_OVERWRITE && fileExists && !force) {
|
|
45
|
+
if (!(await prompter
|
|
46
|
+
.prompt({
|
|
47
|
+
prefix: '',
|
|
48
|
+
type: 'confirm',
|
|
49
|
+
name: 'overwrite',
|
|
50
|
+
message: (0, chalk_1.red)(` exists: ${to}. Overwrite? (y/N): `),
|
|
51
|
+
})
|
|
52
|
+
.then(({ overwrite }) => overwrite))) {
|
|
53
|
+
logger.warn(` skipped: ${to}`);
|
|
54
|
+
return result('skipped');
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
const shouldSkip = skip_if === 'true';
|
|
58
|
+
if (shouldSkip) {
|
|
59
|
+
return result('skipped');
|
|
60
|
+
}
|
|
61
|
+
if (from) {
|
|
62
|
+
const from_path = path_1.default.join(args.templates, from);
|
|
63
|
+
const file = fs_extra_1.default.readFileSync(from_path).toString();
|
|
64
|
+
action.body = file;
|
|
65
|
+
}
|
|
66
|
+
if (!args.dry) {
|
|
67
|
+
await fs_extra_1.default.ensureDir(path_1.default.dirname(absTo));
|
|
68
|
+
await fs_extra_1.default.writeFile(absTo, action.body);
|
|
69
|
+
}
|
|
70
|
+
const pathToLog = process.env.HYGEN_OUTPUT_ABS_PATH ? absTo : to;
|
|
71
|
+
logger.ok(` ${force ? 'FORCED' : 'added'}: ${pathToLog}`);
|
|
72
|
+
return result('added');
|
|
73
|
+
};
|
|
74
|
+
exports.default = add;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.indexByLocation = void 0;
|
|
4
|
+
const errors_1 = require("../errors");
|
|
5
|
+
const EOLRegex = /\r?\n/;
|
|
6
|
+
const pragmaticIndex = (pattern, lines, isBefore, anchor, to) => {
|
|
7
|
+
const oneLineMatchIndex = lines.findIndex((l) => l.match(pattern));
|
|
8
|
+
if (oneLineMatchIndex < 0) {
|
|
9
|
+
const fullText = lines.join('\n');
|
|
10
|
+
const fullMatch = fullText.match(new RegExp(pattern, 'm'));
|
|
11
|
+
if (fullMatch && fullMatch.length) {
|
|
12
|
+
if (isBefore) {
|
|
13
|
+
const fullTextUntilMatchStart = fullText.substring(0, fullMatch.index);
|
|
14
|
+
return fullTextUntilMatchStart.split(EOLRegex).length - 1;
|
|
15
|
+
}
|
|
16
|
+
const matchEndIndex = (fullMatch.index ?? 0) + fullMatch.toString().length;
|
|
17
|
+
const fullTextUntilMatchEnd = fullText.substring(0, matchEndIndex);
|
|
18
|
+
return fullTextUntilMatchEnd.split(EOLRegex).length;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
if (oneLineMatchIndex < 0) {
|
|
22
|
+
throw new errors_1.InjectionAnchorNotFoundError(anchor, to);
|
|
23
|
+
}
|
|
24
|
+
return oneLineMatchIndex + (isBefore ? 0 : 1);
|
|
25
|
+
};
|
|
26
|
+
const locatorByKind = {
|
|
27
|
+
at_line: (l) => ({ locate: () => l }),
|
|
28
|
+
prepend: () => ({ locate: () => 0 }),
|
|
29
|
+
append: () => ({ locate: (lines) => lines.length - 1 }),
|
|
30
|
+
before: (v, to) => ({
|
|
31
|
+
locate: (lines) => pragmaticIndex(String(v), lines, true, 'before', to),
|
|
32
|
+
}),
|
|
33
|
+
after: (v, to) => ({
|
|
34
|
+
locate: (lines) => pragmaticIndex(String(v), lines, false, 'after', to),
|
|
35
|
+
}),
|
|
36
|
+
};
|
|
37
|
+
const indexByLocation = (attributes, lines, to) => {
|
|
38
|
+
const pair = Object.entries(attributes).find(([k]) => locatorByKind[k]);
|
|
39
|
+
if (pair) {
|
|
40
|
+
const [k, v] = pair;
|
|
41
|
+
return locatorByKind[k](v, to).locate(lines);
|
|
42
|
+
}
|
|
43
|
+
return -1;
|
|
44
|
+
};
|
|
45
|
+
exports.indexByLocation = indexByLocation;
|
|
@@ -0,0 +1,17 @@
|
|
|
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
|
+
const result_1 = __importDefault(require("./result"));
|
|
7
|
+
const notEmpty = (x) => x && x.length > 0;
|
|
8
|
+
const echo = async ({ attributes: { echo } }, _args, { logger }) => {
|
|
9
|
+
const result = (0, result_1.default)('shell', echo);
|
|
10
|
+
const echoStr = echo;
|
|
11
|
+
if (notEmpty(echoStr)) {
|
|
12
|
+
logger.colorful(echoStr);
|
|
13
|
+
return result('executed');
|
|
14
|
+
}
|
|
15
|
+
return result('ignored');
|
|
16
|
+
};
|
|
17
|
+
exports.default = echo;
|
|
@@ -0,0 +1,51 @@
|
|
|
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 resolve = async (attributes) => {
|
|
27
|
+
const ops = [];
|
|
28
|
+
const isAddTarget = Boolean(attributes.to) || (attributes.mirror === true && !attributes.sh);
|
|
29
|
+
if (isAddTarget && !attributes.inject && !attributes.replace) {
|
|
30
|
+
const add = (await Promise.resolve().then(() => __importStar(require('./add')))).default;
|
|
31
|
+
ops.push(add);
|
|
32
|
+
}
|
|
33
|
+
if (attributes.to && (attributes.inject || attributes.replace)) {
|
|
34
|
+
const inject = (await Promise.resolve().then(() => __importStar(require('./inject')))).default;
|
|
35
|
+
ops.push(inject);
|
|
36
|
+
}
|
|
37
|
+
if (attributes.echo) {
|
|
38
|
+
const echo = (await Promise.resolve().then(() => __importStar(require('./echo')))).default;
|
|
39
|
+
ops.push(echo);
|
|
40
|
+
}
|
|
41
|
+
if (attributes.sh) {
|
|
42
|
+
const shell = (await Promise.resolve().then(() => __importStar(require('./shell')))).default;
|
|
43
|
+
ops.push(shell);
|
|
44
|
+
}
|
|
45
|
+
if (attributes.setup) {
|
|
46
|
+
const setup = (await Promise.resolve().then(() => __importStar(require('./setup')))).default;
|
|
47
|
+
ops.push(setup);
|
|
48
|
+
}
|
|
49
|
+
return ops;
|
|
50
|
+
};
|
|
51
|
+
exports.default = resolve;
|
|
@@ -0,0 +1,37 @@
|
|
|
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
|
+
const path_1 = __importDefault(require("path"));
|
|
7
|
+
const fs_extra_1 = __importDefault(require("fs-extra"));
|
|
8
|
+
const result_1 = __importDefault(require("./result"));
|
|
9
|
+
const injector_1 = __importDefault(require("./injector"));
|
|
10
|
+
const injectOp = async (action, args, { logger, cwd }) => {
|
|
11
|
+
const { attributes: { to, inject, replace, create_if_missing }, } = action;
|
|
12
|
+
const result = (0, result_1.default)('inject', to);
|
|
13
|
+
if (!((inject || replace) && to)) {
|
|
14
|
+
return result('ignored');
|
|
15
|
+
}
|
|
16
|
+
const absTo = path_1.default.resolve(cwd, to);
|
|
17
|
+
if (!(await fs_extra_1.default.pathExists(absTo))) {
|
|
18
|
+
if (create_if_missing) {
|
|
19
|
+
await fs_extra_1.default.ensureFile(absTo);
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
logger.err(`Cannot inject to ${to}: doesn't exist.`);
|
|
23
|
+
return result('error', {
|
|
24
|
+
error: `Cannot inject to ${to}: doesn't exist.`,
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
const content = (await fs_extra_1.default.readFile(absTo)).toString();
|
|
29
|
+
const injectResult = (0, injector_1.default)(action, content, to);
|
|
30
|
+
if (!args.dry) {
|
|
31
|
+
await fs_extra_1.default.writeFile(absTo, injectResult);
|
|
32
|
+
}
|
|
33
|
+
const pathToLog = process.env.HYGEN_OUTPUT_ABS_PATH ? absTo : to;
|
|
34
|
+
logger.notice(` inject: ${pathToLog}`);
|
|
35
|
+
return result('inject');
|
|
36
|
+
};
|
|
37
|
+
exports.default = injectOp;
|
|
@@ -0,0 +1,49 @@
|
|
|
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
|
+
const newline_1 = __importDefault(require("../newline"));
|
|
7
|
+
const errors_1 = require("../errors");
|
|
8
|
+
const anchors_1 = require("./anchors");
|
|
9
|
+
const REPLACE_ANCHORS = ['before', 'after'];
|
|
10
|
+
const injector = (action, content, to = '') => {
|
|
11
|
+
const { attributes: { skip_if, eof_last, replace }, attributes, body, } = action;
|
|
12
|
+
const skipIf = typeof skip_if === 'string' ? skip_if : '';
|
|
13
|
+
const skipIfNot = attributes.skip_if_not === true;
|
|
14
|
+
const shouldSkip = skipIf ? !!content.match(skipIf) !== skipIfNot : false;
|
|
15
|
+
if (shouldSkip) {
|
|
16
|
+
return content;
|
|
17
|
+
}
|
|
18
|
+
if (replace) {
|
|
19
|
+
if (attributes.inject) {
|
|
20
|
+
throw new errors_1.TemplateConfigError('`inject: true` and `replace: true` are mutually exclusive: use exactly one');
|
|
21
|
+
}
|
|
22
|
+
const activeAnchor = REPLACE_ANCHORS.find((anchor) => attributes[anchor] !== undefined);
|
|
23
|
+
if (!activeAnchor) {
|
|
24
|
+
throw new errors_1.TemplateConfigError('`replace: true` requires a `before` or `after` anchor');
|
|
25
|
+
}
|
|
26
|
+
const pattern = String(attributes[activeAnchor]);
|
|
27
|
+
const regex = new RegExp(pattern, 'm');
|
|
28
|
+
if (!regex.test(content)) {
|
|
29
|
+
throw new errors_1.InjectionAnchorNotFoundError(pattern, to);
|
|
30
|
+
}
|
|
31
|
+
return content.replace(regex, () => body);
|
|
32
|
+
}
|
|
33
|
+
const NL = (0, newline_1.default)(content);
|
|
34
|
+
const lines = content.split(NL);
|
|
35
|
+
const idx = (0, anchors_1.indexByLocation)(attributes, lines, to);
|
|
36
|
+
const trimEOF = idx >= 0 && eof_last === false && /\r?\n$/.test(body);
|
|
37
|
+
const insertEOF = idx >= 0 && eof_last === true && !/\r?\n$/.test(body);
|
|
38
|
+
if (trimEOF) {
|
|
39
|
+
lines.splice(idx, 0, body.replace(/\r?\n$/, ''));
|
|
40
|
+
}
|
|
41
|
+
else if (insertEOF) {
|
|
42
|
+
lines.splice(idx, 0, `${body}${NL}`);
|
|
43
|
+
}
|
|
44
|
+
else if (idx >= 0) {
|
|
45
|
+
lines.splice(idx, 0, body);
|
|
46
|
+
}
|
|
47
|
+
return lines.join(NL);
|
|
48
|
+
};
|
|
49
|
+
exports.default = injector;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.default = (type, subject, start = new Date()) => (status, payload = null, end = new Date()) => ({
|
|
4
|
+
type,
|
|
5
|
+
subject,
|
|
6
|
+
status,
|
|
7
|
+
timing: end.getTime() - start.getTime(),
|
|
8
|
+
...(payload ? { payload } : {}),
|
|
9
|
+
});
|
|
@@ -0,0 +1,52 @@
|
|
|
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
|
+
const path_1 = __importDefault(require("path"));
|
|
7
|
+
const debug_1 = __importDefault(require("debug"));
|
|
8
|
+
const degit_1 = __importDefault(require("degit"));
|
|
9
|
+
const result_1 = __importDefault(require("./result"));
|
|
10
|
+
const errors_1 = require("../errors");
|
|
11
|
+
const debug = (0, debug_1.default)('hygen:ops:setup');
|
|
12
|
+
const notEmpty = (x) => x && x.length > 0;
|
|
13
|
+
const setup = async ({ attributes: { setup, mode, verbose, force } }, args, { logger, cwd }) => {
|
|
14
|
+
const result = (0, result_1.default)('setup', setup);
|
|
15
|
+
const setupStr = setup;
|
|
16
|
+
if (notEmpty(setupStr)) {
|
|
17
|
+
if (!setupStr.includes('/')) {
|
|
18
|
+
throw new errors_1.SetupRepositoryError(setupStr);
|
|
19
|
+
}
|
|
20
|
+
const to = args.to || '.';
|
|
21
|
+
const payload = {
|
|
22
|
+
setup: setupStr,
|
|
23
|
+
mode: mode || 'tar',
|
|
24
|
+
verbose: !!verbose,
|
|
25
|
+
force: !!args.force || !!force,
|
|
26
|
+
to: path_1.default.resolve(cwd, to),
|
|
27
|
+
};
|
|
28
|
+
if (!args.dry) {
|
|
29
|
+
try {
|
|
30
|
+
debug('exec %o %o', setup, payload);
|
|
31
|
+
const d = (0, degit_1.default)(setupStr, {
|
|
32
|
+
mode: payload.mode,
|
|
33
|
+
force: payload.force,
|
|
34
|
+
verbose: payload.verbose,
|
|
35
|
+
});
|
|
36
|
+
await d.clone(payload.to);
|
|
37
|
+
debug('done');
|
|
38
|
+
logger.ok(` setup: ${setup} -> ${to}`);
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
debug('error %o', error);
|
|
42
|
+
logger.err(` setup: ${setup} ${String(error)}`);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
logger.ok(` setup: ${setup} -> ${to}`);
|
|
47
|
+
}
|
|
48
|
+
return result('executed', payload);
|
|
49
|
+
}
|
|
50
|
+
return result('ignored');
|
|
51
|
+
};
|
|
52
|
+
exports.default = setup;
|
|
@@ -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
|
+
const debug_1 = __importDefault(require("debug"));
|
|
7
|
+
const ora_1 = __importDefault(require("ora"));
|
|
8
|
+
const result_1 = __importDefault(require("./result"));
|
|
9
|
+
const debug = (0, debug_1.default)('hygen:ops:shell');
|
|
10
|
+
const notEmpty = (x) => x && x.length > 0;
|
|
11
|
+
const shell = async ({ attributes: { sh, spinner, sh_ignore_exit }, body }, args, { logger, exec }) => {
|
|
12
|
+
const result = (0, result_1.default)('shell', sh);
|
|
13
|
+
const shStr = sh;
|
|
14
|
+
if (notEmpty(shStr)) {
|
|
15
|
+
const spin = (0, ora_1.default)(` shell: ${spinner === true ? 'running...' : spinner}`);
|
|
16
|
+
if (!args.dry) {
|
|
17
|
+
try {
|
|
18
|
+
debug('exec %o %o', shStr, body);
|
|
19
|
+
spinner && spin.start();
|
|
20
|
+
const res = await exec(shStr, body);
|
|
21
|
+
debug('result %o', res);
|
|
22
|
+
}
|
|
23
|
+
catch (error) {
|
|
24
|
+
if (sh_ignore_exit !== true) {
|
|
25
|
+
logger.err(error.stderr ?? String(error));
|
|
26
|
+
process.exit(1);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
finally {
|
|
30
|
+
spinner && spin.succeed();
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
logger.ok(` shell: ${sh}`);
|
|
34
|
+
return result('executed');
|
|
35
|
+
}
|
|
36
|
+
return result('ignored');
|
|
37
|
+
};
|
|
38
|
+
exports.default = shell;
|