@canonical/summon-core 0.18.0 → 0.21.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 +166 -0
- package/package.json +16 -8
- package/src/discovery/discoverGeneratorTree.ts +1 -1
- package/src/discovery/generatorCache.ts +1 -1
- package/src/index.ts +13 -14
- package/src/stamp/applyStamp.test.ts +199 -0
- package/src/stamp/applyStamp.ts +90 -0
- package/src/stamp/index.ts +3 -0
- package/src/stamp/runTaskWithStamp.ts +31 -0
- package/src/stamp/types.ts +7 -0
- package/src/template/TemplatingEngine.ts +17 -0
- package/src/template/ejsEngine.ts +21 -0
- package/src/template/generateStamp.ts +27 -0
- package/src/template/generatorComment.test.ts +81 -0
- package/src/template/generatorComment.ts +32 -0
- package/src/template/getCommentStyle.ts +59 -0
- package/src/template/index.ts +13 -0
- package/src/template/prependStamp.ts +16 -0
- package/src/template/renderFile.ts +13 -0
- package/src/template/renderString.test.ts +107 -0
- package/src/template/renderString.ts +13 -0
- package/src/template/renderStringAsync.ts +13 -0
- package/src/template/tasks/index.ts +3 -0
- package/src/template/tasks/template.test.ts +66 -0
- package/src/template/tasks/template.ts +22 -0
- package/src/template/tasks/templateDir.ts +60 -0
- package/src/template/tasks/types.ts +29 -0
- package/src/template/templateHelpers.test.ts +227 -0
- package/src/template/templateHelpers.ts +35 -0
- package/src/template/withHelpers.test.ts +41 -0
- package/src/template/withHelpers.ts +10 -0
- package/src/testing/integration/template-rendering.test.ts +69 -0
- package/src/types/AnyGenerator.ts +9 -0
- package/src/types/ForbidReserved.ts +23 -0
- package/src/types/GeneratorDefinition.ts +19 -0
- package/src/types/GeneratorMeta.ts +29 -0
- package/src/types/PromptDefinition.ts +33 -0
- package/src/types/ReservedOption.ts +23 -0
- package/src/types/StampConfig.ts +6 -0
- package/src/types/index.ts +7 -0
- package/src/cli-types.ts +0 -59
- package/src/stamp.ts +0 -165
- package/src/template.test.ts +0 -666
- package/src/template.ts +0 -543
- package/src/types.ts +0 -97
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type StampConfig from "../types/StampConfig.js";
|
|
2
|
+
import getCommentStyle from "./getCommentStyle.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Generate a stamp comment for a generated file.
|
|
6
|
+
* Returns null if the file type doesn't support comments.
|
|
7
|
+
*/
|
|
8
|
+
export default function generateStamp(
|
|
9
|
+
filePath: string,
|
|
10
|
+
options: StampConfig,
|
|
11
|
+
): string | null {
|
|
12
|
+
const style = getCommentStyle(filePath);
|
|
13
|
+
if (!style) return null;
|
|
14
|
+
if (!style.single && !style.blockStart) return null;
|
|
15
|
+
|
|
16
|
+
const stampText = `Generated by ${options.generator} v${options.version}`;
|
|
17
|
+
|
|
18
|
+
if (style.preferBlock || (!style.single && style.blockStart)) {
|
|
19
|
+
return `${style.blockStart} ${stampText} ${style.blockEnd}`;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (style.single) {
|
|
23
|
+
return `${style.single} ${stampText}`;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import generatorComment from "./generatorComment.js";
|
|
3
|
+
|
|
4
|
+
describe("generatorComment", () => {
|
|
5
|
+
it("generates basic comment", () => {
|
|
6
|
+
expect(generatorComment("ComponentGenerator")).toBe(
|
|
7
|
+
"// Generated by ComponentGenerator",
|
|
8
|
+
);
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it("includes version when specified", () => {
|
|
12
|
+
expect(generatorComment("ComponentGenerator", { version: "1.0.0" })).toBe(
|
|
13
|
+
"// Generated by ComponentGenerator v1.0.0",
|
|
14
|
+
);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it("includes timestamp when specified", () => {
|
|
18
|
+
expect(generatorComment("ComponentGenerator", { timestamp: true })).toMatch(
|
|
19
|
+
/^\/\/ Generated by ComponentGenerator on \d{4}-\d{2}-\d{2}/,
|
|
20
|
+
);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it("includes both version and timestamp", () => {
|
|
24
|
+
expect(
|
|
25
|
+
generatorComment("ComponentGenerator", {
|
|
26
|
+
version: "2.0.0",
|
|
27
|
+
timestamp: true,
|
|
28
|
+
}),
|
|
29
|
+
).toMatch(/^\/\/ Generated by ComponentGenerator v2\.0\.0 on \d{4}/);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("handles generator name with spaces", () => {
|
|
33
|
+
expect(generatorComment("My Generator")).toBe(
|
|
34
|
+
"// Generated by My Generator",
|
|
35
|
+
);
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
describe("generatorComment (html format)", () => {
|
|
40
|
+
it("generates basic HTML comment", () => {
|
|
41
|
+
expect(generatorComment("ComponentGenerator", { format: "html" })).toBe(
|
|
42
|
+
"<!-- Generated by ComponentGenerator -->",
|
|
43
|
+
);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("includes version when specified", () => {
|
|
47
|
+
expect(
|
|
48
|
+
generatorComment("ComponentGenerator", {
|
|
49
|
+
version: "1.0.0",
|
|
50
|
+
format: "html",
|
|
51
|
+
}),
|
|
52
|
+
).toBe("<!-- Generated by ComponentGenerator v1.0.0 -->");
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("includes timestamp when specified", () => {
|
|
56
|
+
expect(
|
|
57
|
+
generatorComment("ComponentGenerator", {
|
|
58
|
+
timestamp: true,
|
|
59
|
+
format: "html",
|
|
60
|
+
}),
|
|
61
|
+
).toMatch(/^<!-- Generated by ComponentGenerator on \d{4}-\d{2}-\d{2}/);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("includes both version and timestamp", () => {
|
|
65
|
+
expect(
|
|
66
|
+
generatorComment("ComponentGenerator", {
|
|
67
|
+
version: "2.0.0",
|
|
68
|
+
timestamp: true,
|
|
69
|
+
format: "html",
|
|
70
|
+
}),
|
|
71
|
+
).toMatch(/^<!-- Generated by ComponentGenerator v2\.0\.0 on \d{4}/);
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
describe("generatorComment (jsx format)", () => {
|
|
76
|
+
it("generates JSX comment", () => {
|
|
77
|
+
expect(generatorComment("ComponentGenerator", { format: "jsx" })).toBe(
|
|
78
|
+
"{/* Generated by ComponentGenerator */}",
|
|
79
|
+
);
|
|
80
|
+
});
|
|
81
|
+
});
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
type CommentFormat = "line" | "html" | "jsx";
|
|
2
|
+
|
|
3
|
+
interface GeneratorCommentOptions {
|
|
4
|
+
version?: string;
|
|
5
|
+
timestamp?: boolean;
|
|
6
|
+
/** Comment format. Defaults to "line" (`// ...`). */
|
|
7
|
+
format?: CommentFormat;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const DELIMITERS: Record<CommentFormat, { start: string; end: string }> = {
|
|
11
|
+
line: { start: "// ", end: "" },
|
|
12
|
+
html: { start: "<!-- ", end: " -->" },
|
|
13
|
+
jsx: { start: "{/* ", end: " */}" },
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Generate a metadata comment for generated files.
|
|
18
|
+
*
|
|
19
|
+
* @param format - "line" for `// ...`, "html" for `<!-- ... -->`, "jsx" for `{/* ... * /}`
|
|
20
|
+
*/
|
|
21
|
+
export default function generatorComment(
|
|
22
|
+
generatorName: string,
|
|
23
|
+
options?: GeneratorCommentOptions,
|
|
24
|
+
): string {
|
|
25
|
+
const parts = [`Generated by ${generatorName}`];
|
|
26
|
+
if (options?.version) parts.push(`v${options.version}`);
|
|
27
|
+
if (options?.timestamp) parts.push(`on ${new Date().toISOString()}`);
|
|
28
|
+
|
|
29
|
+
const format = options?.format ?? "line";
|
|
30
|
+
const { start, end } = DELIMITERS[format];
|
|
31
|
+
return `${start}${parts.join(" ")}${end}`;
|
|
32
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import * as path from "node:path";
|
|
2
|
+
|
|
3
|
+
interface CommentStyle {
|
|
4
|
+
single?: string;
|
|
5
|
+
blockStart?: string;
|
|
6
|
+
blockEnd?: string;
|
|
7
|
+
preferBlock?: boolean;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const COMMENT_STYLES: Record<string, CommentStyle> = {
|
|
11
|
+
".ts": { single: "//", blockStart: "/*", blockEnd: "*/" },
|
|
12
|
+
".tsx": { single: "//", blockStart: "/*", blockEnd: "*/" },
|
|
13
|
+
".js": { single: "//", blockStart: "/*", blockEnd: "*/" },
|
|
14
|
+
".jsx": { single: "//", blockStart: "/*", blockEnd: "*/" },
|
|
15
|
+
".mjs": { single: "//", blockStart: "/*", blockEnd: "*/" },
|
|
16
|
+
".cjs": { single: "//", blockStart: "/*", blockEnd: "*/" },
|
|
17
|
+
".css": { blockStart: "/*", blockEnd: "*/", preferBlock: true },
|
|
18
|
+
".scss": { single: "//", blockStart: "/*", blockEnd: "*/" },
|
|
19
|
+
".sass": { single: "//" },
|
|
20
|
+
".less": { single: "//", blockStart: "/*", blockEnd: "*/" },
|
|
21
|
+
".html": { blockStart: "<!--", blockEnd: "-->" },
|
|
22
|
+
".htm": { blockStart: "<!--", blockEnd: "-->" },
|
|
23
|
+
".xml": { blockStart: "<!--", blockEnd: "-->" },
|
|
24
|
+
".svg": { blockStart: "<!--", blockEnd: "-->" },
|
|
25
|
+
".vue": { blockStart: "<!--", blockEnd: "-->" },
|
|
26
|
+
".svelte": { blockStart: "<!--", blockEnd: "-->" },
|
|
27
|
+
".json": {},
|
|
28
|
+
".yaml": { single: "#" },
|
|
29
|
+
".yml": { single: "#" },
|
|
30
|
+
".toml": { single: "#" },
|
|
31
|
+
".sh": { single: "#" },
|
|
32
|
+
".bash": { single: "#" },
|
|
33
|
+
".zsh": { single: "#" },
|
|
34
|
+
".fish": { single: "#" },
|
|
35
|
+
".py": { single: "#" },
|
|
36
|
+
".rb": { single: "#" },
|
|
37
|
+
".pl": { single: "#" },
|
|
38
|
+
".go": { single: "//", blockStart: "/*", blockEnd: "*/" },
|
|
39
|
+
".rs": { single: "//", blockStart: "/*", blockEnd: "*/" },
|
|
40
|
+
".java": { single: "//", blockStart: "/*", blockEnd: "*/" },
|
|
41
|
+
".kt": { single: "//", blockStart: "/*", blockEnd: "*/" },
|
|
42
|
+
".swift": { single: "//", blockStart: "/*", blockEnd: "*/" },
|
|
43
|
+
".c": { single: "//", blockStart: "/*", blockEnd: "*/" },
|
|
44
|
+
".cpp": { single: "//", blockStart: "/*", blockEnd: "*/" },
|
|
45
|
+
".h": { single: "//", blockStart: "/*", blockEnd: "*/" },
|
|
46
|
+
".hpp": { single: "//", blockStart: "/*", blockEnd: "*/" },
|
|
47
|
+
".php": { single: "//", blockStart: "/*", blockEnd: "*/" },
|
|
48
|
+
".md": { blockStart: "<!--", blockEnd: "-->" },
|
|
49
|
+
".mdx": { blockStart: "{/*", blockEnd: "*/}" },
|
|
50
|
+
".sql": { single: "--", blockStart: "/*", blockEnd: "*/" },
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Get the comment style for a given file path.
|
|
55
|
+
*/
|
|
56
|
+
export default function getCommentStyle(filePath: string): CommentStyle | null {
|
|
57
|
+
const ext = path.extname(filePath).toLowerCase();
|
|
58
|
+
return COMMENT_STYLES[ext] ?? null;
|
|
59
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export { default as ejsEngine } from "./ejsEngine.js";
|
|
2
|
+
export { default as generateStamp } from "./generateStamp.js";
|
|
3
|
+
export { default as generatorComment } from "./generatorComment.js";
|
|
4
|
+
export { default as getCommentStyle } from "./getCommentStyle.js";
|
|
5
|
+
export { default as prependStamp } from "./prependStamp.js";
|
|
6
|
+
export { default as renderFile } from "./renderFile.js";
|
|
7
|
+
export { default as renderString } from "./renderString.js";
|
|
8
|
+
export { default as renderStringAsync } from "./renderStringAsync.js";
|
|
9
|
+
export type { default as TemplatingEngine } from "./TemplatingEngine.js";
|
|
10
|
+
export type { TemplateDirOptions, TemplateOptions } from "./tasks/index.js";
|
|
11
|
+
export { template, templateDir } from "./tasks/index.js";
|
|
12
|
+
export { default as templateHelpers } from "./templateHelpers.js";
|
|
13
|
+
export { default as withHelpers } from "./withHelpers.js";
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Prepend a stamp to file content.
|
|
3
|
+
* Handles shebang lines (#!/...) by placing stamp after them.
|
|
4
|
+
*/
|
|
5
|
+
export default function prependStamp(content: string, stamp: string): string {
|
|
6
|
+
if (content.startsWith("#!")) {
|
|
7
|
+
const firstNewline = content.indexOf("\n");
|
|
8
|
+
if (firstNewline !== -1) {
|
|
9
|
+
const shebang = content.slice(0, firstNewline + 1);
|
|
10
|
+
const rest = content.slice(firstNewline + 1);
|
|
11
|
+
return `${shebang}${stamp}\n${rest}`;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return `${stamp}\n${content}`;
|
|
16
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import ejsEngine from "./ejsEngine.js";
|
|
2
|
+
import type TemplatingEngine from "./TemplatingEngine.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Render a template file with variables.
|
|
6
|
+
*/
|
|
7
|
+
export default async function renderFile(
|
|
8
|
+
templatePath: string,
|
|
9
|
+
vars: Record<string, unknown>,
|
|
10
|
+
engine: TemplatingEngine = ejsEngine,
|
|
11
|
+
): Promise<string> {
|
|
12
|
+
return engine.renderFile(templatePath, vars);
|
|
13
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import renderString from "./renderString.js";
|
|
3
|
+
|
|
4
|
+
describe("renderString", () => {
|
|
5
|
+
it("renders simple variables", () => {
|
|
6
|
+
expect(renderString("Hello, <%= name %>!", { name: "World" })).toBe(
|
|
7
|
+
"Hello, World!",
|
|
8
|
+
);
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it("renders multiple variables", () => {
|
|
12
|
+
expect(
|
|
13
|
+
renderString("<%= greeting %>, <%= name %>!", {
|
|
14
|
+
greeting: "Hello",
|
|
15
|
+
name: "World",
|
|
16
|
+
}),
|
|
17
|
+
).toBe("Hello, World!");
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("renders with no variables", () => {
|
|
21
|
+
expect(renderString("No variables here", {})).toBe("No variables here");
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it("renders embedded JavaScript expressions", () => {
|
|
25
|
+
expect(renderString("<%= 2 + 2 %>", {})).toBe("4");
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it("renders conditionals", () => {
|
|
29
|
+
const tpl = "<% if (show) { %>Visible<% } %>";
|
|
30
|
+
expect(renderString(tpl, { show: true })).toBe("Visible");
|
|
31
|
+
expect(renderString(tpl, { show: false })).toBe("");
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("renders loops", () => {
|
|
35
|
+
const tpl = "<% items.forEach(item => { %><%= item %> <% }) %>";
|
|
36
|
+
expect(renderString(tpl, { items: ["a", "b", "c"] })).toBe("a b c ");
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("escapes HTML by default with <%= %>", () => {
|
|
40
|
+
const result = renderString("<%= html %>", {
|
|
41
|
+
html: "<script>alert('xss')</script>",
|
|
42
|
+
});
|
|
43
|
+
expect(result).toContain("<script>");
|
|
44
|
+
expect(result).not.toContain("<script>");
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("renders unescaped HTML with <%- %>", () => {
|
|
48
|
+
expect(renderString("<%- html %>", { html: "<div>content</div>" })).toBe(
|
|
49
|
+
"<div>content</div>",
|
|
50
|
+
);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("handles nested objects", () => {
|
|
54
|
+
expect(
|
|
55
|
+
renderString("<%= user.name %> (<%= user.age %>)", {
|
|
56
|
+
user: { name: "John", age: 30 },
|
|
57
|
+
}),
|
|
58
|
+
).toBe("John (30)");
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("handles array access", () => {
|
|
62
|
+
expect(
|
|
63
|
+
renderString("<%= items[0] %> and <%= items[1] %>", {
|
|
64
|
+
items: ["first", "second"],
|
|
65
|
+
}),
|
|
66
|
+
).toBe("first and second");
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("handles functions in variables", () => {
|
|
70
|
+
expect(
|
|
71
|
+
renderString("<%= format(name) %>", {
|
|
72
|
+
name: "hello",
|
|
73
|
+
format: (s: string) => s.toUpperCase(),
|
|
74
|
+
}),
|
|
75
|
+
).toBe("HELLO");
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("handles multiline templates", () => {
|
|
79
|
+
const result = renderString(`\nline 1\n<%= middle %>\nline 3\n`, {
|
|
80
|
+
middle: "line 2",
|
|
81
|
+
});
|
|
82
|
+
expect(result).toContain("line 1");
|
|
83
|
+
expect(result).toContain("line 2");
|
|
84
|
+
expect(result).toContain("line 3");
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it("handles empty template", () => {
|
|
88
|
+
expect(renderString("", {})).toBe("");
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("handles template with only whitespace", () => {
|
|
92
|
+
expect(renderString(" \n\t ", {})).toBe(" \n\t ");
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it("handles unicode in template", () => {
|
|
96
|
+
expect(renderString("<%= emoji %>", { emoji: "\u{1F600}" })).toBe(
|
|
97
|
+
"\u{1F600}",
|
|
98
|
+
);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("handles special characters in variables", () => {
|
|
102
|
+
const result = renderString("<%= special %>", {
|
|
103
|
+
special: 'Special: @#$%^&*()[]{}|\\;"<>',
|
|
104
|
+
});
|
|
105
|
+
expect(result).toContain("Special:");
|
|
106
|
+
});
|
|
107
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import ejsEngine from "./ejsEngine.js";
|
|
2
|
+
import type TemplatingEngine from "./TemplatingEngine.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Render a template string with variables.
|
|
6
|
+
*/
|
|
7
|
+
export default function renderString(
|
|
8
|
+
template: string,
|
|
9
|
+
vars: Record<string, unknown>,
|
|
10
|
+
engine: TemplatingEngine = ejsEngine,
|
|
11
|
+
): string {
|
|
12
|
+
return engine.render(template, vars);
|
|
13
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import ejsEngine from "./ejsEngine.js";
|
|
2
|
+
import type TemplatingEngine from "./TemplatingEngine.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Render a template string asynchronously.
|
|
6
|
+
*/
|
|
7
|
+
export default async function renderStringAsync(
|
|
8
|
+
template: string,
|
|
9
|
+
vars: Record<string, unknown>,
|
|
10
|
+
engine: TemplatingEngine = ejsEngine,
|
|
11
|
+
): Promise<string> {
|
|
12
|
+
return engine.renderAsync(template, vars);
|
|
13
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { dryRun } from "@canonical/task";
|
|
2
|
+
import { describe, expect, it } from "vitest";
|
|
3
|
+
import template from "./template.js";
|
|
4
|
+
import templateDir from "./templateDir.js";
|
|
5
|
+
|
|
6
|
+
describe("template task", () => {
|
|
7
|
+
it("creates mkdir and writeFile effects", () => {
|
|
8
|
+
const t = template({
|
|
9
|
+
source: "/templates/component.tsx.ejs",
|
|
10
|
+
dest: "/output/Button.tsx",
|
|
11
|
+
vars: { name: "Button" },
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
const { effects } = dryRun(t);
|
|
15
|
+
expect(effects.some((e) => e._tag === "MakeDir")).toBe(true);
|
|
16
|
+
expect(effects.some((e) => e._tag === "ReadFile")).toBe(true);
|
|
17
|
+
expect(effects.some((e) => e._tag === "WriteFile")).toBe(true);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("renders destination path with variables", () => {
|
|
21
|
+
const t = template({
|
|
22
|
+
source: "/templates/component.tsx.ejs",
|
|
23
|
+
dest: "/output/<%= name %>.tsx",
|
|
24
|
+
vars: { name: "Button" },
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
const { effects } = dryRun(t);
|
|
28
|
+
const writeEffect = effects.find((e) => e._tag === "WriteFile");
|
|
29
|
+
expect((writeEffect as { path: string }).path).toBe("/output/Button.tsx");
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("creates parent directory", () => {
|
|
33
|
+
const t = template({
|
|
34
|
+
source: "/templates/test.txt.ejs",
|
|
35
|
+
dest: "/deep/nested/path/file.txt",
|
|
36
|
+
vars: {},
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
const { effects } = dryRun(t);
|
|
40
|
+
const mkdirEffect = effects.find((e) => e._tag === "MakeDir");
|
|
41
|
+
expect((mkdirEffect as { path: string }).path).toBe("/deep/nested/path");
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
describe("templateDir task", () => {
|
|
46
|
+
it("creates effects for directory templating", () => {
|
|
47
|
+
const t = templateDir({
|
|
48
|
+
source: "/templates",
|
|
49
|
+
dest: "/output",
|
|
50
|
+
vars: { name: "MyComponent" },
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
const { effects } = dryRun(t);
|
|
54
|
+
expect(effects.some((e) => e._tag === "Glob")).toBe(true);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("handles empty directory (no files matched)", () => {
|
|
58
|
+
const t = templateDir({
|
|
59
|
+
source: "/empty-templates",
|
|
60
|
+
dest: "/output",
|
|
61
|
+
vars: {},
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
expect(() => dryRun(t)).not.toThrow();
|
|
65
|
+
});
|
|
66
|
+
});
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import * as path from "node:path";
|
|
2
|
+
import { mkdir, readFile, type Task, task, writeFile } from "@canonical/task";
|
|
3
|
+
import ejsEngine from "../ejsEngine.js";
|
|
4
|
+
import renderString from "../renderString.js";
|
|
5
|
+
import type { TemplateOptions } from "./types.js";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Render a single template file to a destination.
|
|
9
|
+
*/
|
|
10
|
+
export default function template(options: TemplateOptions): Task<void> {
|
|
11
|
+
const engine = options.engine ?? ejsEngine;
|
|
12
|
+
|
|
13
|
+
// Render destination path with variables
|
|
14
|
+
const destPath = renderString(options.dest, options.vars, engine);
|
|
15
|
+
const destDir = path.dirname(destPath);
|
|
16
|
+
|
|
17
|
+
return task(mkdir(destDir))
|
|
18
|
+
.chain(() => task(readFile(options.source)))
|
|
19
|
+
.map((content) => renderString(content, options.vars, engine))
|
|
20
|
+
.chain((rendered) => task(writeFile(destPath, rendered)))
|
|
21
|
+
.unwrap();
|
|
22
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import * as path from "node:path";
|
|
2
|
+
import { glob, sequence_, type Task, task } from "@canonical/task";
|
|
3
|
+
import ejsEngine from "../ejsEngine.js";
|
|
4
|
+
import renderString from "../renderString.js";
|
|
5
|
+
import template from "./template.js";
|
|
6
|
+
import type { TemplateDirOptions } from "./types.js";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Simple minimatch implementation for common patterns.
|
|
10
|
+
*/
|
|
11
|
+
const minimatch = (filepath: string, pattern: string): boolean => {
|
|
12
|
+
const regex = pattern
|
|
13
|
+
.replace(/\./g, "\\.")
|
|
14
|
+
.replace(/\*\*/g, "<<GLOBSTAR>>")
|
|
15
|
+
.replace(/\*/g, "[^/]*")
|
|
16
|
+
.replace(/<<GLOBSTAR>>/g, ".*");
|
|
17
|
+
|
|
18
|
+
return new RegExp(`^${regex}$`).test(filepath);
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Render a directory of templates to a destination.
|
|
23
|
+
*/
|
|
24
|
+
export default function templateDir(options: TemplateDirOptions): Task<void> {
|
|
25
|
+
const engine = options.engine ?? ejsEngine;
|
|
26
|
+
|
|
27
|
+
return task(glob("**/*", options.source))
|
|
28
|
+
.chain((files) => {
|
|
29
|
+
const tasks = files
|
|
30
|
+
.filter((file) => {
|
|
31
|
+
if (options.ignore) {
|
|
32
|
+
return !options.ignore.some((pattern) => minimatch(file, pattern));
|
|
33
|
+
}
|
|
34
|
+
return true;
|
|
35
|
+
})
|
|
36
|
+
.map((file) => {
|
|
37
|
+
const sourcePath = path.join(options.source, file);
|
|
38
|
+
|
|
39
|
+
let destFile = file.replace(/\.ejs$/, "");
|
|
40
|
+
|
|
41
|
+
const renamed = options.rename?.[destFile];
|
|
42
|
+
if (renamed) {
|
|
43
|
+
destFile = renamed;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
destFile = renderString(destFile, options.vars, engine);
|
|
47
|
+
const destPath = path.join(options.dest, destFile);
|
|
48
|
+
|
|
49
|
+
return template({
|
|
50
|
+
source: sourcePath,
|
|
51
|
+
dest: destPath,
|
|
52
|
+
vars: options.vars,
|
|
53
|
+
engine,
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
return task(sequence_(tasks));
|
|
58
|
+
})
|
|
59
|
+
.unwrap();
|
|
60
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type TemplatingEngine from "../TemplatingEngine.js";
|
|
2
|
+
|
|
3
|
+
export interface TemplateOptions {
|
|
4
|
+
/** Path to the template file */
|
|
5
|
+
source: string;
|
|
6
|
+
/** Destination path (can contain template variables) */
|
|
7
|
+
dest: string;
|
|
8
|
+
/** Variables to pass to the template */
|
|
9
|
+
vars: Record<string, unknown>;
|
|
10
|
+
/** Templating engine to use (defaults to EJS) */
|
|
11
|
+
engine?: TemplatingEngine;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface TemplateDirOptions {
|
|
15
|
+
/** Source directory containing templates */
|
|
16
|
+
source: string;
|
|
17
|
+
/** Destination directory */
|
|
18
|
+
dest: string;
|
|
19
|
+
/** Variables to pass to all templates */
|
|
20
|
+
vars: Record<string, unknown>;
|
|
21
|
+
/** File rename mappings (original -> new name, supports template vars) */
|
|
22
|
+
rename?: Record<string, string>;
|
|
23
|
+
/** Glob patterns to ignore */
|
|
24
|
+
ignore?: string[];
|
|
25
|
+
/** Content transformers by file extension or name */
|
|
26
|
+
transform?: Record<string, (content: string) => string>;
|
|
27
|
+
/** Templating engine to use (defaults to EJS) */
|
|
28
|
+
engine?: TemplatingEngine;
|
|
29
|
+
}
|