@brand-map/generator 0.0.0-dev.14 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.oxfmtrc.json +29 -0
- package/.oxlintrc.json +144 -0
- package/README.md +203 -7
- package/bun.lock +47 -19
- package/package.json +15 -20
- package/src/actions/load-templates.ts +70 -0
- package/src/exports.ts +2 -0
- package/src/generator.ts +138 -0
- package/{lib.ts → src/lib.ts} +4 -4
- package/src/renderer-handlebars.ts +27 -0
- package/src/renderer-vento.ts +21 -0
- package/{types.ts → src/types.ts} +6 -4
- package/tsconfig.json +1 -1
- package/TODO.md +0 -2
- package/actions/ai.ts +0 -7
- package/actions/answer.ts +0 -7
- package/actions/blank.ts +0 -55
- package/actions/combine.ts +0 -14
- package/actions/config.ts +0 -7
- package/actions/context/context.ts +0 -44
- package/actions/context.ts +0 -10
- package/actions/echo.ts +0 -19
- package/actions/exports.ts +0 -9
- package/actions/jargal-context.ts +0 -21
- package/actions/jargal-templates.ts +0 -82
- package/actions/jargal-write.ts +0 -46
- package/actions/load-templates.ts +0 -125
- package/actions/modify.ts +0 -7
- package/actions/parallel.ts +0 -16
- package/actions/pipe/pipe.ts +0 -993
- package/actions/pipe.ts +0 -7
- package/actions/prompt.ts +0 -134
- package/actions/render-template.ts +0 -39
- package/actions/run/run.ts +0 -20
- package/actions/select-generator.ts +0 -28
- package/actions/use.ts +0 -9
- package/actions/validate-answers.ts +0 -13
- package/actions/write/write.ts +0 -69
- package/actions/write.ts +0 -51
- package/biome.json +0 -35
- package/exports.ts +0 -8
- package/jargal.ts +0 -181
- package/renderer.ts +0 -100
- package/runner.test.ts +0 -24
- package/runner.ts +0 -99
package/renderer.ts
DELETED
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
import { get, set } from "es-toolkit/compat";
|
|
2
|
-
import handlebars from "handlebars";
|
|
3
|
-
|
|
4
|
-
import type { GetReturnType, HelperFn, Helpers, MappingScope, Partials, SetOptions, Setter, SetterScope } from "./types.ts";
|
|
5
|
-
import { readonly, textHelpers, utilHelpers } from "./lib.ts";
|
|
6
|
-
|
|
7
|
-
export class Renderer {
|
|
8
|
-
#partials: Partials = {};
|
|
9
|
-
#helpers: Helpers = { ...textHelpers, ...utilHelpers };
|
|
10
|
-
|
|
11
|
-
#mapping: { partial: Partials; helper: Helpers } = {
|
|
12
|
-
partial: this.#partials,
|
|
13
|
-
helper: this.#helpers,
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
#set<T extends SetterScope>(scope: T, config: Setter<T>, options?: SetOptions): Renderer {
|
|
17
|
-
if (!config.name) {
|
|
18
|
-
throw new Error("Name must be non-empty string");
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
const target = get(this.#mapping, scope);
|
|
22
|
-
|
|
23
|
-
if (!target) throw new Error("No mapping");
|
|
24
|
-
|
|
25
|
-
if (config.name in target && !options?.override) {
|
|
26
|
-
throw new Error("Can't override");
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
set(target, config.name, config.target);
|
|
30
|
-
|
|
31
|
-
return this;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
private readonly partials: Partials = readonly(this.#partials);
|
|
35
|
-
private readonly helpers: Helpers = readonly(this.#helpers);
|
|
36
|
-
|
|
37
|
-
private get<T extends SetterScope>(scope: T, name: string): GetReturnType<T> {
|
|
38
|
-
const target = get(this.#mapping, scope);
|
|
39
|
-
|
|
40
|
-
if (!target) {
|
|
41
|
-
throw new Error("!");
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
return get(target, name);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
private list<T extends `${SetterScope}s`, O extends boolean = false>(
|
|
48
|
-
scope: T,
|
|
49
|
-
options?: { full?: O },
|
|
50
|
-
): O extends true ? MappingScope[T extends `${infer S}s` ? S : T][] : string[] {
|
|
51
|
-
const target = get(this.#mapping, scope.slice(0, scope.length - 1));
|
|
52
|
-
|
|
53
|
-
if (!target) {
|
|
54
|
-
throw new Error("No mapping");
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
if (!options?.full) {
|
|
58
|
-
return Object.keys(target) as any;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
switch (scope) {
|
|
62
|
-
case "helpers":
|
|
63
|
-
return Object.entries(target).map(([name, helper]) => ({
|
|
64
|
-
name,
|
|
65
|
-
helper,
|
|
66
|
-
})) as any;
|
|
67
|
-
|
|
68
|
-
case "partials":
|
|
69
|
-
return Object.entries(target).map(([name, partial]) => ({
|
|
70
|
-
name,
|
|
71
|
-
partial,
|
|
72
|
-
})) as any;
|
|
73
|
-
|
|
74
|
-
default:
|
|
75
|
-
throw new Error("can't find the scope");
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
public setPartial(name: string, partial: string, options?: SetOptions): Renderer {
|
|
80
|
-
return this.#set("partial", { target: partial, name }, options);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
public setHelper(name: string, helper: HelperFn, options?: SetOptions): Renderer {
|
|
84
|
-
return this.#set("helper", { target: helper, name }, options);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
public renderString(params: { template: string; data: Record<string, unknown> }): string {
|
|
88
|
-
for (const [name, helper] of Object.entries(this.#helpers)) {
|
|
89
|
-
handlebars.registerHelper(name, helper);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
for (const [name, partial] of Object.entries(this.#partials)) {
|
|
93
|
-
handlebars.registerPartial(name, partial);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
const compiled = handlebars.compile(params.template);
|
|
97
|
-
|
|
98
|
-
return compiled(params.data);
|
|
99
|
-
}
|
|
100
|
-
}
|
package/runner.test.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import test from "node:test";
|
|
2
|
-
import assert from "node:assert";
|
|
3
|
-
import { join } from "node:path";
|
|
4
|
-
|
|
5
|
-
import { run } from "./runner.ts";
|
|
6
|
-
import { blank1, blank2 } from "./actions/blank.ts";
|
|
7
|
-
|
|
8
|
-
import type { Config, GeneratorConfig } from "./types.ts";
|
|
9
|
-
|
|
10
|
-
import { context } from "./actions/context.ts";
|
|
11
|
-
import { loadTemplates } from "./actions/load-templates.ts";
|
|
12
|
-
|
|
13
|
-
test("simple", async () => {
|
|
14
|
-
const simple: GeneratorConfig = {
|
|
15
|
-
name: "simple",
|
|
16
|
-
description: "description",
|
|
17
|
-
actions: [blank1, blank2, context(() => ({ kek: Math.random() })), console.log, loadTemplates(join(process.cwd(), "actions"))],
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
const config: Config = { generators: [simple] };
|
|
21
|
-
const result = await run(config);
|
|
22
|
-
|
|
23
|
-
assert(typeof result === "undefined");
|
|
24
|
-
});
|
package/runner.ts
DELETED
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
import { Renderer } from "./renderer.ts";
|
|
2
|
-
import * as v from "valibot";
|
|
3
|
-
import type { Action, Config, ExecuteActionParams, GeneratorParams } from "./types.ts";
|
|
4
|
-
import { selectGenerator } from "./actions/select-generator.ts";
|
|
5
|
-
import assert from "node:assert/strict";
|
|
6
|
-
|
|
7
|
-
export async function runGenerator({ context, generator, renderer }: GeneratorParams): Promise<void> {
|
|
8
|
-
assert(generator);
|
|
9
|
-
assert(Array.isArray(generator.actions) && generator.actions.length > 0);
|
|
10
|
-
|
|
11
|
-
for (const action of generator.actions) {
|
|
12
|
-
await executeAction({ action, context, renderer });
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export async function executeAction({ action, context, renderer }: ExecuteActionParams): Promise<void | Action | Action[]> {
|
|
17
|
-
if (Array.isArray(action)) {
|
|
18
|
-
for (const action_ of action) {
|
|
19
|
-
const executed = await action_({ context, renderer });
|
|
20
|
-
if (!executed) {
|
|
21
|
-
continue;
|
|
22
|
-
}
|
|
23
|
-
await execRecursive(executed, { context, renderer });
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
if (typeof action !== "function") {
|
|
28
|
-
return undefined;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
const executed = await action({ context, renderer });
|
|
32
|
-
|
|
33
|
-
if (!executed) {
|
|
34
|
-
return undefined;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
return await execRecursive(executed, { context, renderer });
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
async function execRecursive(executed: Action | Action[], { context, renderer }: Omit<ExecuteActionParams, "action">): Promise<Action | Action[] | void> {
|
|
41
|
-
if (Array.isArray(executed)) {
|
|
42
|
-
const executionResults: Action[] = [];
|
|
43
|
-
|
|
44
|
-
for (const action of executed) {
|
|
45
|
-
const result = await executeAction({ action, context, renderer });
|
|
46
|
-
|
|
47
|
-
if (result) {
|
|
48
|
-
if (Array.isArray(result)) {
|
|
49
|
-
executionResults.push(...result.flat());
|
|
50
|
-
} else {
|
|
51
|
-
executionResults.push(result);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
return executionResults;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
if (typeof executed === "function") {
|
|
60
|
-
return await executeAction({ action: executed, context, renderer });
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
return undefined;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
const ConfigSchema = v.object({
|
|
67
|
-
generators: v.pipe(
|
|
68
|
-
v.array(
|
|
69
|
-
v.object({
|
|
70
|
-
name: v.string(),
|
|
71
|
-
description: v.optional(v.string()),
|
|
72
|
-
actions: v.pipe(v.array(v.any()), v.minLength(1)),
|
|
73
|
-
}),
|
|
74
|
-
),
|
|
75
|
-
v.minLength(1),
|
|
76
|
-
),
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
export async function run(runConfig: Config): Promise<void> {
|
|
80
|
-
const config = v.run(ConfigSchema, runConfig);
|
|
81
|
-
|
|
82
|
-
if (config.generators.length === 1) {
|
|
83
|
-
const generator = config.generators[0];
|
|
84
|
-
|
|
85
|
-
assert(generator);
|
|
86
|
-
|
|
87
|
-
return await runGenerator({
|
|
88
|
-
context: { errors: [], answers: {} },
|
|
89
|
-
renderer: new Renderer(),
|
|
90
|
-
generator,
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
return await runGenerator({
|
|
95
|
-
context: { errors: [], answers: {} },
|
|
96
|
-
renderer: new Renderer(),
|
|
97
|
-
generator: { name: "select", actions: [selectGenerator(config)] },
|
|
98
|
-
});
|
|
99
|
-
}
|