@biorate/hygen 0.1.0 → 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/README.md +1 -0
- package/dist/cjs/bin.js +0 -0
- package/dist/cjs/src/errors.js +9 -1
- package/dist/cjs/src/ops/add.js +8 -5
- package/dist/cjs/src/path-macros.js +30 -0
- package/dist/esm/src/errors.js +7 -0
- package/dist/esm/src/ops/add.js +8 -5
- package/dist/esm/src/path-macros.js +24 -0
- package/dist/types/src/errors.d.ts +3 -0
- package/dist/types/src/ops/add.d.ts +1 -1
- package/dist/types/src/path-macros.d.ts +3 -0
- package/dist/types/src/types.d.ts +1 -1
- package/package.json +22 -20
package/README.md
CHANGED
|
@@ -130,6 +130,7 @@ The complete user guide lives in the package's local `docs/` pages. README is fo
|
|
|
130
130
|
- **Composition** — the action and name positionals accept comma lists, so one command runs several actions and/or names (`hygen mailer new,html a,b`); action lists run sequentially, the name loop is name-outer, `--keep-going` collects failures without halting, and `name` is always a scalar in templates. A template with `mirror: true` (and no `to:`) writes to the path mirroring its location inside the action folder, with the trailing `.t` stripped (or `.ejs.t` behind `HYGEN_MIRROR_EJS_SUFFIX=1`). See [Multiple actions and names](./docs/generators.md#multiple-actions-and-names-in-one-run) and [Mirroring](./docs/templates.md#mirroring-the-template-layout-mirror).
|
|
131
131
|
- **`--name` flag removed (breaking, semver-major)** — the `--name` flag is no longer accepted by default. Invoking it fails with `NameFlagRemovedError`, whose message names the offending flag, shows the positional equivalent (`hygen <generator> <action[,action...]> <name[,name...]>`), and points at the legacy escape. Use the positional name instead, optionally as a comma list: `hygen mailer new fancy` or `hygen mailer new a,b`. For migration, two legacy escapes restore the pre-removal behavior: set `HYGEN_LEGACY_NAME=1`, or pass `legacyNameFlag: true` in `RunnerConfig` (embedding). Under the escape the flag behaves exactly as before this change (including the repeated-flag array). Name precedence is prompt hook > positional name; the `--name` flag participates only via the legacy escape.
|
|
132
132
|
- **Glob batch actions** — a template may declare `glob:` to scaffold a whole directory in one action (plop's `addMany` equivalent): the pattern resolves relative to the template's own directory, `base:`/`stripExtensions:` shape each target path, EJS renders in file names and contents, and the glob template itself writes nothing. `glob` is mutually exclusive with `mirror: true` (`GlobMirrorConflictError`). `skipIfExists: true` skips an existing target softly (no prompt, no failure) on any file-creating action. See [Glob batch actions](./docs/templates.md#glob-batch-actions-glob).
|
|
133
|
+
- **Path macros in mirrored targets** — any segment of a mirrored path may carry `$IDENTIFIER$` macros (e.g. `apps/$CLIENT_NAME$/src/`), substituted from the run locals (prompt answers, `name`, CLI flags, config helpers); a macro is always a scalar (one pass per name under name expansion), unknown identifiers fail fast with `PathMacroResolutionError`, and `$` is a legal filename character on Linux, macOS and Windows. `to:` and glob `destination:` remain fully EJS-rendered. See [Path macros](./docs/templates.md#path-macros-identifier).
|
|
133
134
|
- **Named content transforms** — the config file may declare a `transforms` namespace of post-processors, and a template frontmatter `transform: name` (or a chained `a|b`) applies them to the rendered content before it is written or injected (formatting generated code with prettier is the canonical use). See [Content transforms](./docs/extensibility.md#content-transforms).
|
|
134
135
|
- **Conditional action skipping** — a template frontmatter `skip:` (EJS-rendered with the run locals) skips the action when truthy, so a template can decide at render time whether it runs: `skip: <%= wantTests ? 'false' : 'no tests requested' %>`. Skipped actions execute no operation and are reported with a third `skipped` status in `RunnerResult.statuses` (never in `failures`). See [Conditional action skipping](./docs/templates.md#conditional-action-skipping-skip).
|
|
135
136
|
- **Interactive generator/action picker** — running `hygen` with no positional arguments in a terminal offers a two-level picker (generator → action) and runs the selection exactly as if passed positionally; in non-TTY environments (CI, piped stdio) the invocation fails fast with the usual missing-arguments error plus a hint, and embedders can force/disable it via `RunnerConfig.interactive`. See [Interactive picker](./docs/generators.md#interactive-picker-no-arguments).
|
package/dist/cjs/bin.js
CHANGED
|
File without changes
|
package/dist/cjs/src/errors.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.NameFlagRemovedError = exports.CliArgumentError = exports.SetupRepositoryError = exports.UnknownTransformError = exports.ConfigTsNodeError = exports.GlobMirrorConflictError = exports.ShellCommandError = exports.TemplateConfigError = exports.InjectionAnchorNotFoundError = exports.InjectionTargetNotFoundError = exports.TemplateNotFoundError = exports.TemplateRenderError = exports.KeepGoingError = exports.ShowHelpError = void 0;
|
|
3
|
+
exports.NameFlagRemovedError = exports.CliArgumentError = exports.SetupRepositoryError = exports.UnknownTransformError = exports.ConfigTsNodeError = exports.GlobMirrorConflictError = exports.PathMacroResolutionError = exports.ShellCommandError = exports.TemplateConfigError = exports.InjectionAnchorNotFoundError = exports.InjectionTargetNotFoundError = exports.TemplateNotFoundError = exports.TemplateRenderError = exports.KeepGoingError = exports.ShowHelpError = void 0;
|
|
4
4
|
const errors_1 = require("@biorate/errors");
|
|
5
5
|
class ShowHelpError extends errors_1.BaseError {
|
|
6
6
|
constructor(message) {
|
|
@@ -61,6 +61,14 @@ class ShellCommandError extends errors_1.BaseError {
|
|
|
61
61
|
}
|
|
62
62
|
}
|
|
63
63
|
exports.ShellCommandError = ShellCommandError;
|
|
64
|
+
class PathMacroResolutionError extends errors_1.BaseError {
|
|
65
|
+
constructor(file, segment, identifier) {
|
|
66
|
+
super('Template [%s] has a mirrored path segment [%s] with the macro $%s$, which ' +
|
|
67
|
+
'cannot be resolved from the run locals. Define it in the prompt hook, pass ' +
|
|
68
|
+
'it as a CLI flag, or export it from the config helpers factory.', [file, segment, identifier]);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
exports.PathMacroResolutionError = PathMacroResolutionError;
|
|
64
72
|
class GlobMirrorConflictError extends errors_1.BaseError {
|
|
65
73
|
constructor(file) {
|
|
66
74
|
super('Template [%s] declares both `mirror: true` and `glob:`, which are incompatible: ' +
|
package/dist/cjs/src/ops/add.js
CHANGED
|
@@ -7,21 +7,24 @@ const path_1 = __importDefault(require("path"));
|
|
|
7
7
|
const fs_extra_1 = __importDefault(require("fs-extra"));
|
|
8
8
|
const chalk_1 = require("chalk");
|
|
9
9
|
const result_1 = __importDefault(require("./result"));
|
|
10
|
-
const
|
|
10
|
+
const path_macros_1 = require("../path-macros");
|
|
11
|
+
const targetPath = (action, args, config) => {
|
|
11
12
|
const { attributes } = action;
|
|
12
13
|
if (attributes.to)
|
|
13
14
|
return attributes.to;
|
|
14
15
|
if (attributes.mirror !== true || attributes.sh !== undefined || !action.file) {
|
|
15
16
|
return undefined;
|
|
16
17
|
}
|
|
17
|
-
const rel = path_1.default.relative(actionfolder, action.file);
|
|
18
|
-
|
|
18
|
+
const rel = path_1.default.relative(args.actionfolder, action.file);
|
|
19
|
+
const stripped = process.env.HYGEN_MIRROR_EJS_SUFFIX === '1'
|
|
19
20
|
? rel.replace(/\.ejs\.t$/, '')
|
|
20
21
|
: rel.replace(/\.t$/, '');
|
|
22
|
+
return (0, path_macros_1.resolvePathMacros)(stripped, args, config, action.file);
|
|
21
23
|
};
|
|
22
|
-
const add = async (action, args,
|
|
24
|
+
const add = async (action, args, config) => {
|
|
25
|
+
const { logger, cwd, createPrompter } = config;
|
|
23
26
|
const { attributes: { inject, unless_exists, force, from, skip_if, skipIfExists }, } = action;
|
|
24
|
-
const to = targetPath(action, args
|
|
27
|
+
const to = targetPath(action, args, config);
|
|
25
28
|
const result = (0, result_1.default)('add', to);
|
|
26
29
|
const prompter = createPrompter();
|
|
27
30
|
if (!to || inject) {
|
|
@@ -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;
|
package/dist/esm/src/errors.js
CHANGED
|
@@ -50,6 +50,13 @@ export class ShellCommandError extends BaseError {
|
|
|
50
50
|
]);
|
|
51
51
|
}
|
|
52
52
|
}
|
|
53
|
+
export class PathMacroResolutionError extends BaseError {
|
|
54
|
+
constructor(file, segment, identifier) {
|
|
55
|
+
super('Template [%s] has a mirrored path segment [%s] with the macro $%s$, which ' +
|
|
56
|
+
'cannot be resolved from the run locals. Define it in the prompt hook, pass ' +
|
|
57
|
+
'it as a CLI flag, or export it from the config helpers factory.', [file, segment, identifier]);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
53
60
|
export class GlobMirrorConflictError extends BaseError {
|
|
54
61
|
constructor(file) {
|
|
55
62
|
super('Template [%s] declares both `mirror: true` and `glob:`, which are incompatible: ' +
|
package/dist/esm/src/ops/add.js
CHANGED
|
@@ -2,21 +2,24 @@ import path from 'path';
|
|
|
2
2
|
import fs from 'fs-extra';
|
|
3
3
|
import { red } from 'chalk';
|
|
4
4
|
import createResult from './result.js';
|
|
5
|
-
|
|
5
|
+
import { resolvePathMacros } from '../path-macros.js';
|
|
6
|
+
const targetPath = (action, args, config) => {
|
|
6
7
|
const { attributes } = action;
|
|
7
8
|
if (attributes.to)
|
|
8
9
|
return attributes.to;
|
|
9
10
|
if (attributes.mirror !== true || attributes.sh !== undefined || !action.file) {
|
|
10
11
|
return undefined;
|
|
11
12
|
}
|
|
12
|
-
const rel = path.relative(actionfolder, action.file);
|
|
13
|
-
|
|
13
|
+
const rel = path.relative(args.actionfolder, action.file);
|
|
14
|
+
const stripped = process.env.HYGEN_MIRROR_EJS_SUFFIX === '1'
|
|
14
15
|
? rel.replace(/\.ejs\.t$/, '')
|
|
15
16
|
: rel.replace(/\.t$/, '');
|
|
17
|
+
return resolvePathMacros(stripped, args, config, action.file);
|
|
16
18
|
};
|
|
17
|
-
const add = async (action, args,
|
|
19
|
+
const add = async (action, args, config) => {
|
|
20
|
+
const { logger, cwd, createPrompter } = config;
|
|
18
21
|
const { attributes: { inject, unless_exists, force, from, skip_if, skipIfExists }, } = action;
|
|
19
|
-
const to = targetPath(action, args
|
|
22
|
+
const to = targetPath(action, args, config);
|
|
20
23
|
const result = createResult('add', to);
|
|
21
24
|
const prompter = createPrompter();
|
|
22
25
|
if (!to || inject) {
|
|
@@ -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 };
|
|
@@ -27,6 +27,9 @@ export declare class TemplateConfigError extends BaseError {
|
|
|
27
27
|
export declare class ShellCommandError extends BaseError {
|
|
28
28
|
constructor(command: string, cause: unknown);
|
|
29
29
|
}
|
|
30
|
+
export declare class PathMacroResolutionError extends BaseError {
|
|
31
|
+
constructor(file: string, segment: string, identifier: string);
|
|
32
|
+
}
|
|
30
33
|
export declare class GlobMirrorConflictError extends BaseError {
|
|
31
34
|
constructor(file: string);
|
|
32
35
|
}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import type { ActionResult, RenderedAction, RunnerConfig } from '../types';
|
|
2
|
-
declare const add: (action: RenderedAction, args: Record<string, any>,
|
|
2
|
+
declare const add: (action: RenderedAction, args: Record<string, any>, config: RunnerConfig) => Promise<ActionResult>;
|
|
3
3
|
export default add;
|
|
@@ -28,7 +28,7 @@ export interface RenderedAction {
|
|
|
28
28
|
skip?: true | string;
|
|
29
29
|
}
|
|
30
30
|
export type Helper = (...args: unknown[]) => unknown;
|
|
31
|
-
export type Helpers = Record<string,
|
|
31
|
+
export type Helpers = Record<string, unknown>;
|
|
32
32
|
export type Transform = (content: string, locals: Record<string, unknown>) => string | Promise<string>;
|
|
33
33
|
export type Transforms = Record<string, Transform>;
|
|
34
34
|
export interface RunnerConfig {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@biorate/hygen",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "The scalable code generator that saves you time. Fork of jondot/hygen",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"template",
|
|
@@ -38,7 +38,25 @@
|
|
|
38
38
|
"README.md",
|
|
39
39
|
"LICENSE"
|
|
40
40
|
],
|
|
41
|
+
"scripts": {
|
|
42
|
+
"clean": "cleanup dist",
|
|
43
|
+
"build:cjs": "npx tsc -p ./tsconfig.build.cjs.json",
|
|
44
|
+
"build:esm": "npx tsc -p ./tsconfig.build.esm.json",
|
|
45
|
+
"build:types": "npx tsc -p ./tsconfig.build.types.json",
|
|
46
|
+
"build": "pnpm run clean && pnpm run build:cjs && pnpm run build:esm && pnpm run build:types",
|
|
47
|
+
"docs": "node .scripts/build-hygen-docs.js",
|
|
48
|
+
"demo": "pnpm run docs",
|
|
49
|
+
"postbuild:cjs": "node ../../../.scripts/write-package-type.js dist/cjs commonjs && cp -r src/templates dist/cjs/templates",
|
|
50
|
+
"postbuild:esm": "tsc-esm-fix --tsconfig=./tsconfig.build.esm.json --target=./dist/esm && node ../../../.scripts/write-package-type.js dist/esm module && cp -r src/templates dist/esm/templates",
|
|
51
|
+
"format": "prettier --check src",
|
|
52
|
+
"lint": "eslint src --ext .ts",
|
|
53
|
+
"format:fix": "prettier --write src",
|
|
54
|
+
"lint:fix": "eslint src --ext .ts --fix",
|
|
55
|
+
"test": "npx vitest run --coverage",
|
|
56
|
+
"prepublishOnly": "pnpm run build"
|
|
57
|
+
},
|
|
41
58
|
"dependencies": {
|
|
59
|
+
"@biorate/errors": "3.1.2",
|
|
42
60
|
"chalk": "4.1.2",
|
|
43
61
|
"change-case": "3.1.0",
|
|
44
62
|
"debug": "4.3.3",
|
|
@@ -52,8 +70,7 @@
|
|
|
52
70
|
"ignore-walk": "4.0.1",
|
|
53
71
|
"inflection": "1.12.0",
|
|
54
72
|
"ora": "5.4.1",
|
|
55
|
-
"yargs-parser": "21.0.0"
|
|
56
|
-
"@biorate/errors": "3.1.2"
|
|
73
|
+
"yargs-parser": "21.0.0"
|
|
57
74
|
},
|
|
58
75
|
"devDependencies": {
|
|
59
76
|
"@types/debug": "4.1.7",
|
|
@@ -63,20 +80,5 @@
|
|
|
63
80
|
"@types/inflection": "1.13.0",
|
|
64
81
|
"@types/yargs-parser": "21.0.3"
|
|
65
82
|
},
|
|
66
|
-
"
|
|
67
|
-
|
|
68
|
-
"build:cjs": "npx tsc -p ./tsconfig.build.cjs.json",
|
|
69
|
-
"build:esm": "npx tsc -p ./tsconfig.build.esm.json",
|
|
70
|
-
"build:types": "npx tsc -p ./tsconfig.build.types.json",
|
|
71
|
-
"build": "pnpm run clean && pnpm run build:cjs && pnpm run build:esm && pnpm run build:types",
|
|
72
|
-
"docs": "node .scripts/build-hygen-docs.js",
|
|
73
|
-
"demo": "pnpm run docs",
|
|
74
|
-
"postbuild:cjs": "node ../../../.scripts/write-package-type.js dist/cjs commonjs && cp -r src/templates dist/cjs/templates",
|
|
75
|
-
"postbuild:esm": "tsc-esm-fix --tsconfig=./tsconfig.build.esm.json --target=./dist/esm && node ../../../.scripts/write-package-type.js dist/esm module && cp -r src/templates dist/esm/templates",
|
|
76
|
-
"format": "prettier --check src",
|
|
77
|
-
"lint": "eslint src --ext .ts",
|
|
78
|
-
"format:fix": "prettier --write src",
|
|
79
|
-
"lint:fix": "eslint src --ext .ts --fix",
|
|
80
|
-
"test": "npx vitest run --coverage"
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
+
"gitHead": "0768d737d2355377d0d27e36a716ea27f89b6b1d"
|
|
84
|
+
}
|