@codeandmoney/jargal 0.0.0-dev.11 → 0.0.0-dev.13
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/actions/combine.ts +5 -1
- package/actions/jargal-templates.ts +14 -3
- package/actions/load-templates.ts +24 -6
- package/actions/parallel.ts +9 -1
- package/actions/render-template.ts +12 -2
- package/actions/select-generator.ts +5 -1
- package/actions/write.ts +4 -5
- package/jargal.ts +33 -8
- package/package.json +1 -1
package/actions/combine.ts
CHANGED
|
@@ -4,7 +4,11 @@ import type { Action } from "../types.ts";
|
|
|
4
4
|
export function combine(...actions: Action[]): Action {
|
|
5
5
|
return async function execute(params) {
|
|
6
6
|
for (const action of actions) {
|
|
7
|
-
await executeAction({
|
|
7
|
+
await executeAction({
|
|
8
|
+
action,
|
|
9
|
+
context: params.context,
|
|
10
|
+
renderer: params.renderer,
|
|
11
|
+
});
|
|
8
12
|
}
|
|
9
13
|
};
|
|
10
14
|
}
|
|
@@ -2,7 +2,12 @@ import assert from "node:assert";
|
|
|
2
2
|
import { readdir, readFile } from "node:fs/promises";
|
|
3
3
|
import path, { resolve } from "node:path";
|
|
4
4
|
|
|
5
|
-
export type TemplateData = {
|
|
5
|
+
export type TemplateData = {
|
|
6
|
+
templatePath: string;
|
|
7
|
+
savePath: string;
|
|
8
|
+
templateContent: string;
|
|
9
|
+
metadata?: Record<string, any>;
|
|
10
|
+
};
|
|
6
11
|
|
|
7
12
|
export type TemplatesMap = Record<string, TemplateData>;
|
|
8
13
|
|
|
@@ -12,7 +17,9 @@ export async function templates<Scope extends string | undefined = undefined>(pa
|
|
|
12
17
|
path: string;
|
|
13
18
|
scope?: Scope;
|
|
14
19
|
engine?: "handlebars";
|
|
15
|
-
hooks?: {
|
|
20
|
+
hooks?: {
|
|
21
|
+
onDataReady?: ((path: string, data: TemplateData) => [path: string, data: TemplateData])[];
|
|
22
|
+
};
|
|
16
23
|
}): Promise<(ctx: Record<string, any>) => Templates<Scope extends undefined ? "default" : Scope>> {
|
|
17
24
|
const engine = params?.engine ?? "handlebars";
|
|
18
25
|
const removeExtension = engine === "handlebars" ? ".hbs" : "";
|
|
@@ -30,7 +37,11 @@ export async function templates<Scope extends string | undefined = undefined>(pa
|
|
|
30
37
|
const savePath = path.relative(resolvedPath, templatePath).replace(removeExtension, "");
|
|
31
38
|
|
|
32
39
|
const contentRaw = await readFile(path.resolve(resolvedPath, templatePath));
|
|
33
|
-
const data: TemplateData = {
|
|
40
|
+
const data: TemplateData = {
|
|
41
|
+
templateContent: new TextDecoder().decode(contentRaw),
|
|
42
|
+
templatePath,
|
|
43
|
+
savePath,
|
|
44
|
+
};
|
|
34
45
|
|
|
35
46
|
if (!params.hooks) {
|
|
36
47
|
assert(record[scopeKey]);
|
|
@@ -2,13 +2,19 @@ import type { ContextAction } from "../types.ts";
|
|
|
2
2
|
import { readdir, readFile } from "node:fs/promises";
|
|
3
3
|
import path, { resolve } from "node:path";
|
|
4
4
|
|
|
5
|
-
type TemplateData = {
|
|
5
|
+
type TemplateData = {
|
|
6
|
+
templatePath: string;
|
|
7
|
+
realativePath: string;
|
|
8
|
+
templateContent: string;
|
|
9
|
+
};
|
|
6
10
|
|
|
7
11
|
export function loadTemplates(
|
|
8
12
|
templatesPath: string,
|
|
9
13
|
options: {
|
|
10
14
|
scope?: string;
|
|
11
|
-
hooks?: {
|
|
15
|
+
hooks?: {
|
|
16
|
+
onDataReady?: ((path: string, data: TemplateData) => [path: string, data: TemplateData])[];
|
|
17
|
+
};
|
|
12
18
|
},
|
|
13
19
|
): ContextAction<{ templates: Record<string, Map<string, TemplateData>> }> {
|
|
14
20
|
return async function execute(params) {
|
|
@@ -22,7 +28,11 @@ export function loadTemplates(
|
|
|
22
28
|
const realativePath = path.relative(templatesPath, templatePath);
|
|
23
29
|
|
|
24
30
|
const contentRaw = await readFile(path.resolve(templatesPath, templatePath));
|
|
25
|
-
const data: TemplateData = {
|
|
31
|
+
const data: TemplateData = {
|
|
32
|
+
templateContent: new TextDecoder().decode(contentRaw),
|
|
33
|
+
templatePath,
|
|
34
|
+
realativePath,
|
|
35
|
+
};
|
|
26
36
|
|
|
27
37
|
if (!options.hooks) {
|
|
28
38
|
record[options.scope ? options.scope : "templates"]?.set(realativePath, data);
|
|
@@ -48,7 +58,9 @@ export function loadTemplates(
|
|
|
48
58
|
export async function templates<Scope extends string | undefined = undefined>(params: {
|
|
49
59
|
path: string;
|
|
50
60
|
scope?: Scope;
|
|
51
|
-
hooks?: {
|
|
61
|
+
hooks?: {
|
|
62
|
+
onDataReady?: ((path: string, data: TemplateData) => [path: string, data: TemplateData])[];
|
|
63
|
+
};
|
|
52
64
|
}): Promise<
|
|
53
65
|
(ctx: Record<string, any>) => {
|
|
54
66
|
templates: Record<Scope extends undefined ? "default" : Scope, Map<string, TemplateData>>;
|
|
@@ -68,7 +80,11 @@ export async function templates<Scope extends string | undefined = undefined>(pa
|
|
|
68
80
|
const realativePath = path.relative(resolvedPath, templatePath);
|
|
69
81
|
|
|
70
82
|
const contentRaw = await readFile(path.resolve(resolvedPath, templatePath));
|
|
71
|
-
const data: TemplateData = {
|
|
83
|
+
const data: TemplateData = {
|
|
84
|
+
templateContent: new TextDecoder().decode(contentRaw),
|
|
85
|
+
templatePath,
|
|
86
|
+
realativePath,
|
|
87
|
+
};
|
|
72
88
|
|
|
73
89
|
if (!params.hooks) {
|
|
74
90
|
record[params.scope ? params.scope : "default"]?.set(realativePath, data);
|
|
@@ -87,7 +103,9 @@ export async function templates<Scope extends string | undefined = undefined>(pa
|
|
|
87
103
|
}
|
|
88
104
|
}
|
|
89
105
|
|
|
90
|
-
return function setter(_ctx: Record<string, any>): {
|
|
106
|
+
return function setter(_ctx: Record<string, any>): {
|
|
107
|
+
templates: Record<string, Map<string, TemplateData>>;
|
|
108
|
+
} {
|
|
91
109
|
return { templates: record };
|
|
92
110
|
};
|
|
93
111
|
}
|
package/actions/parallel.ts
CHANGED
|
@@ -3,6 +3,14 @@ import type { Action } from "../types.ts";
|
|
|
3
3
|
|
|
4
4
|
export function parallel(actions: Action[]): Action {
|
|
5
5
|
return async function execute(params) {
|
|
6
|
-
await Promise.all(
|
|
6
|
+
await Promise.all(
|
|
7
|
+
actions.map((action) =>
|
|
8
|
+
executeAction({
|
|
9
|
+
action,
|
|
10
|
+
context: params.context,
|
|
11
|
+
renderer: params.renderer,
|
|
12
|
+
}),
|
|
13
|
+
),
|
|
14
|
+
);
|
|
7
15
|
};
|
|
8
16
|
}
|
|
@@ -20,10 +20,20 @@ export function renderTemplate({
|
|
|
20
20
|
|
|
21
21
|
const renderedTemplate = params.renderer.renderString({ template, data });
|
|
22
22
|
|
|
23
|
-
const renderedPath = params.renderer.renderString({
|
|
23
|
+
const renderedPath = params.renderer.renderString({
|
|
24
|
+
template: templatePath,
|
|
25
|
+
data,
|
|
26
|
+
});
|
|
24
27
|
|
|
25
28
|
if (save) {
|
|
26
|
-
return save({
|
|
29
|
+
return save({
|
|
30
|
+
...params,
|
|
31
|
+
context: {
|
|
32
|
+
...params.context,
|
|
33
|
+
templateContent: renderedTemplate,
|
|
34
|
+
destination: renderedPath,
|
|
35
|
+
},
|
|
36
|
+
});
|
|
27
37
|
}
|
|
28
38
|
};
|
|
29
39
|
}
|
|
@@ -18,7 +18,11 @@ export function selectGenerator(config: Config): Action {
|
|
|
18
18
|
|
|
19
19
|
assert(generator);
|
|
20
20
|
|
|
21
|
-
return runGenerator({
|
|
21
|
+
return runGenerator({
|
|
22
|
+
context: { errors: [], answers: {} },
|
|
23
|
+
renderer: new Renderer(),
|
|
24
|
+
generator,
|
|
25
|
+
});
|
|
22
26
|
},
|
|
23
27
|
];
|
|
24
28
|
}
|
package/actions/write.ts
CHANGED
|
@@ -10,11 +10,10 @@ export type WriteActionConfig = {
|
|
|
10
10
|
mode?: "force" | "skip-if-exists";
|
|
11
11
|
};
|
|
12
12
|
|
|
13
|
-
export function write({
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}: WriteActionConfig): Action<{ templateContent?: string; destination?: string }> {
|
|
13
|
+
export function write({ destination: writeDestination, templateContent: writeContent, mode }: WriteActionConfig): Action<{
|
|
14
|
+
templateContent?: string;
|
|
15
|
+
destination?: string;
|
|
16
|
+
}> {
|
|
18
17
|
return async function execute({ context: { templateContent: executeContent, destination: executeDestination } }) {
|
|
19
18
|
const destination = executeDestination || writeDestination;
|
|
20
19
|
const templateContent = executeContent || writeContent;
|
package/jargal.ts
CHANGED
|
@@ -10,7 +10,14 @@ import { context } from "./actions/jargal-context";
|
|
|
10
10
|
|
|
11
11
|
export { templates, context };
|
|
12
12
|
|
|
13
|
-
export type RenderEntry =
|
|
13
|
+
export type RenderEntry =
|
|
14
|
+
| {
|
|
15
|
+
control: "user";
|
|
16
|
+
data: any;
|
|
17
|
+
pathTemplate: string;
|
|
18
|
+
contentTemplate: string;
|
|
19
|
+
}
|
|
20
|
+
| { control?: "auto"; data: any; destination: string };
|
|
14
21
|
|
|
15
22
|
export class Jargal<const in out Context> {
|
|
16
23
|
#context = {
|
|
@@ -65,8 +72,14 @@ export class Jargal<const in out Context> {
|
|
|
65
72
|
for (const [filename, template] of Object.entries(templatesToRender)) {
|
|
66
73
|
// @ts-expect-error
|
|
67
74
|
this.#context.renderedContent.push({
|
|
68
|
-
savePath: this.#renderer.renderString({
|
|
69
|
-
|
|
75
|
+
savePath: this.#renderer.renderString({
|
|
76
|
+
template: join(renderEntry.destination, template.savePath),
|
|
77
|
+
data: renderEntry.data,
|
|
78
|
+
}),
|
|
79
|
+
content: this.#renderer.renderString({
|
|
80
|
+
template: template.templateContent,
|
|
81
|
+
data: renderEntry.data,
|
|
82
|
+
}),
|
|
70
83
|
});
|
|
71
84
|
}
|
|
72
85
|
|
|
@@ -76,8 +89,14 @@ export class Jargal<const in out Context> {
|
|
|
76
89
|
if (renderEntry.control === "user") {
|
|
77
90
|
// @ts-expect-error
|
|
78
91
|
this.#context.renderedContent.push({
|
|
79
|
-
savePath: this.#renderer.renderString({
|
|
80
|
-
|
|
92
|
+
savePath: this.#renderer.renderString({
|
|
93
|
+
template: renderEntry.pathTemplate,
|
|
94
|
+
data: renderEntry.data,
|
|
95
|
+
}),
|
|
96
|
+
content: this.#renderer.renderString({
|
|
97
|
+
template: renderEntry.contentTemplate,
|
|
98
|
+
data: renderEntry.data,
|
|
99
|
+
}),
|
|
81
100
|
});
|
|
82
101
|
|
|
83
102
|
continue;
|
|
@@ -131,7 +150,10 @@ export class Jargal<const in out Context> {
|
|
|
131
150
|
): Promise<void> {
|
|
132
151
|
if (typeof write === "function") {
|
|
133
152
|
// @ts-expect-error
|
|
134
|
-
for (const renderConfig of this.#context.renderedContent as {
|
|
153
|
+
for (const renderConfig of this.#context.renderedContent as {
|
|
154
|
+
savePath: string;
|
|
155
|
+
content: string;
|
|
156
|
+
}[]) {
|
|
135
157
|
await write({ ...renderConfig, defultWrite });
|
|
136
158
|
}
|
|
137
159
|
|
|
@@ -140,8 +162,11 @@ export class Jargal<const in out Context> {
|
|
|
140
162
|
|
|
141
163
|
// console.log({ renderedContent: this.#context.renderedContent})
|
|
142
164
|
// @ts-expect-error
|
|
143
|
-
for (const renderConfig of this.#context.renderedContent as {
|
|
144
|
-
|
|
165
|
+
for (const renderConfig of this.#context.renderedContent as {
|
|
166
|
+
savePath: string;
|
|
167
|
+
content: string;
|
|
168
|
+
}[]) {
|
|
169
|
+
await defultWrite(renderConfig);
|
|
145
170
|
}
|
|
146
171
|
}
|
|
147
172
|
}
|