@canonical/summon-core 0.20.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/package.json +15 -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,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
|
+
}
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import templateHelpers from "./templateHelpers.js";
|
|
3
|
+
|
|
4
|
+
describe("templateHelpers", () => {
|
|
5
|
+
describe("camelCase", () => {
|
|
6
|
+
it("converts hyphenated strings", () => {
|
|
7
|
+
expect(templateHelpers.camelCase("my-component")).toBe("myComponent");
|
|
8
|
+
});
|
|
9
|
+
it("converts underscored strings", () => {
|
|
10
|
+
expect(templateHelpers.camelCase("my_component")).toBe("myComponent");
|
|
11
|
+
});
|
|
12
|
+
it("converts space-separated strings", () => {
|
|
13
|
+
expect(templateHelpers.camelCase("my component")).toBe("myComponent");
|
|
14
|
+
});
|
|
15
|
+
it("converts PascalCase to camelCase", () => {
|
|
16
|
+
expect(templateHelpers.camelCase("MyComponent")).toBe("myComponent");
|
|
17
|
+
});
|
|
18
|
+
it("handles multiple separators", () => {
|
|
19
|
+
expect(templateHelpers.camelCase("my-component_name")).toBe(
|
|
20
|
+
"myComponentName",
|
|
21
|
+
);
|
|
22
|
+
});
|
|
23
|
+
it("handles single word", () => {
|
|
24
|
+
expect(templateHelpers.camelCase("component")).toBe("component");
|
|
25
|
+
});
|
|
26
|
+
it("handles empty string", () => {
|
|
27
|
+
expect(templateHelpers.camelCase("")).toBe("");
|
|
28
|
+
});
|
|
29
|
+
it("handles consecutive separators", () => {
|
|
30
|
+
expect(templateHelpers.camelCase("my--component")).toBe("myComponent");
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
describe("pascalCase", () => {
|
|
35
|
+
it("converts hyphenated strings", () => {
|
|
36
|
+
expect(templateHelpers.pascalCase("my-component")).toBe("MyComponent");
|
|
37
|
+
});
|
|
38
|
+
it("converts underscored strings", () => {
|
|
39
|
+
expect(templateHelpers.pascalCase("my_component")).toBe("MyComponent");
|
|
40
|
+
});
|
|
41
|
+
it("converts space-separated strings", () => {
|
|
42
|
+
expect(templateHelpers.pascalCase("my component")).toBe("MyComponent");
|
|
43
|
+
});
|
|
44
|
+
it("preserves PascalCase", () => {
|
|
45
|
+
expect(templateHelpers.pascalCase("MyComponent")).toBe("MyComponent");
|
|
46
|
+
});
|
|
47
|
+
it("handles single word", () => {
|
|
48
|
+
expect(templateHelpers.pascalCase("component")).toBe("Component");
|
|
49
|
+
});
|
|
50
|
+
it("handles empty string", () => {
|
|
51
|
+
expect(templateHelpers.pascalCase("")).toBe("");
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
describe("kebabCase", () => {
|
|
56
|
+
it("converts camelCase", () => {
|
|
57
|
+
expect(templateHelpers.kebabCase("myComponent")).toBe("my-component");
|
|
58
|
+
});
|
|
59
|
+
it("converts PascalCase", () => {
|
|
60
|
+
expect(templateHelpers.kebabCase("MyComponent")).toBe("my-component");
|
|
61
|
+
});
|
|
62
|
+
it("converts underscored strings", () => {
|
|
63
|
+
expect(templateHelpers.kebabCase("my_component")).toBe("my-component");
|
|
64
|
+
});
|
|
65
|
+
it("converts space-separated strings", () => {
|
|
66
|
+
expect(templateHelpers.kebabCase("my component")).toBe("my-component");
|
|
67
|
+
});
|
|
68
|
+
it("preserves kebab-case", () => {
|
|
69
|
+
expect(templateHelpers.kebabCase("my-component")).toBe("my-component");
|
|
70
|
+
});
|
|
71
|
+
it("handles single word", () => {
|
|
72
|
+
expect(templateHelpers.kebabCase("Component")).toBe("component");
|
|
73
|
+
});
|
|
74
|
+
it("handles empty string", () => {
|
|
75
|
+
expect(templateHelpers.kebabCase("")).toBe("");
|
|
76
|
+
});
|
|
77
|
+
it("handles consecutive uppercase", () => {
|
|
78
|
+
expect(templateHelpers.kebabCase("XMLParser")).toBe("xml-parser");
|
|
79
|
+
});
|
|
80
|
+
it("handles mixed case with boundaries", () => {
|
|
81
|
+
expect(templateHelpers.kebabCase("parseXMLDocument")).toBe(
|
|
82
|
+
"parse-xml-document",
|
|
83
|
+
);
|
|
84
|
+
});
|
|
85
|
+
it("splits on each lowercase to uppercase transition", () => {
|
|
86
|
+
expect(templateHelpers.kebabCase("parseXmlDocument")).toBe(
|
|
87
|
+
"parse-xml-document",
|
|
88
|
+
);
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
describe("snakeCase", () => {
|
|
93
|
+
it("converts camelCase", () => {
|
|
94
|
+
expect(templateHelpers.snakeCase("myComponent")).toBe("my_component");
|
|
95
|
+
});
|
|
96
|
+
it("converts PascalCase", () => {
|
|
97
|
+
expect(templateHelpers.snakeCase("MyComponent")).toBe("my_component");
|
|
98
|
+
});
|
|
99
|
+
it("converts hyphenated strings", () => {
|
|
100
|
+
expect(templateHelpers.snakeCase("my-component")).toBe("my_component");
|
|
101
|
+
});
|
|
102
|
+
it("converts space-separated strings", () => {
|
|
103
|
+
expect(templateHelpers.snakeCase("my component")).toBe("my_component");
|
|
104
|
+
});
|
|
105
|
+
it("preserves snake_case", () => {
|
|
106
|
+
expect(templateHelpers.snakeCase("my_component")).toBe("my_component");
|
|
107
|
+
});
|
|
108
|
+
it("handles single word", () => {
|
|
109
|
+
expect(templateHelpers.snakeCase("Component")).toBe("component");
|
|
110
|
+
});
|
|
111
|
+
it("handles empty string", () => {
|
|
112
|
+
expect(templateHelpers.snakeCase("")).toBe("");
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
describe("constantCase", () => {
|
|
117
|
+
it("converts camelCase", () => {
|
|
118
|
+
expect(templateHelpers.constantCase("myComponent")).toBe("MY_COMPONENT");
|
|
119
|
+
});
|
|
120
|
+
it("converts PascalCase", () => {
|
|
121
|
+
expect(templateHelpers.constantCase("MyComponent")).toBe("MY_COMPONENT");
|
|
122
|
+
});
|
|
123
|
+
it("converts hyphenated strings", () => {
|
|
124
|
+
expect(templateHelpers.constantCase("my-component")).toBe("MY_COMPONENT");
|
|
125
|
+
});
|
|
126
|
+
it("converts space-separated strings", () => {
|
|
127
|
+
expect(templateHelpers.constantCase("my component")).toBe("MY_COMPONENT");
|
|
128
|
+
});
|
|
129
|
+
it("handles single word", () => {
|
|
130
|
+
expect(templateHelpers.constantCase("component")).toBe("COMPONENT");
|
|
131
|
+
});
|
|
132
|
+
it("handles empty string", () => {
|
|
133
|
+
expect(templateHelpers.constantCase("")).toBe("");
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
describe("capitalize", () => {
|
|
138
|
+
it("capitalizes first letter", () => {
|
|
139
|
+
expect(templateHelpers.capitalize("hello")).toBe("Hello");
|
|
140
|
+
});
|
|
141
|
+
it("preserves rest of string", () => {
|
|
142
|
+
expect(templateHelpers.capitalize("hELLO")).toBe("HELLO");
|
|
143
|
+
});
|
|
144
|
+
it("handles single character", () => {
|
|
145
|
+
expect(templateHelpers.capitalize("h")).toBe("H");
|
|
146
|
+
});
|
|
147
|
+
it("handles empty string", () => {
|
|
148
|
+
expect(templateHelpers.capitalize("")).toBe("");
|
|
149
|
+
});
|
|
150
|
+
it("handles already capitalized", () => {
|
|
151
|
+
expect(templateHelpers.capitalize("Hello")).toBe("Hello");
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
describe("isoDate", () => {
|
|
156
|
+
it("returns ISO date string", () => {
|
|
157
|
+
expect(templateHelpers.isoDate()).toMatch(
|
|
158
|
+
/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/,
|
|
159
|
+
);
|
|
160
|
+
});
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
describe("year", () => {
|
|
164
|
+
it("returns current year", () => {
|
|
165
|
+
expect(templateHelpers.year()).toBe(new Date().getFullYear());
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
describe("indent", () => {
|
|
170
|
+
it("indents single line", () => {
|
|
171
|
+
expect(templateHelpers.indent("hello", 2)).toBe(" hello");
|
|
172
|
+
});
|
|
173
|
+
it("indents multiple lines", () => {
|
|
174
|
+
expect(templateHelpers.indent("line1\nline2\nline3", 2)).toBe(
|
|
175
|
+
" line1\n line2\n line3",
|
|
176
|
+
);
|
|
177
|
+
});
|
|
178
|
+
it("handles zero indent", () => {
|
|
179
|
+
expect(templateHelpers.indent("hello", 0)).toBe("hello");
|
|
180
|
+
});
|
|
181
|
+
it("handles large indent", () => {
|
|
182
|
+
expect(templateHelpers.indent("hello", 10)).toBe(" hello");
|
|
183
|
+
});
|
|
184
|
+
it("handles empty string", () => {
|
|
185
|
+
expect(templateHelpers.indent("", 2)).toBe(" ");
|
|
186
|
+
});
|
|
187
|
+
it("handles empty lines", () => {
|
|
188
|
+
expect(templateHelpers.indent("a\n\nb", 2)).toBe(" a\n \n b");
|
|
189
|
+
});
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
describe("join", () => {
|
|
193
|
+
it("joins array with default separator", () => {
|
|
194
|
+
expect(templateHelpers.join(["a", "b", "c"])).toBe("a, b, c");
|
|
195
|
+
});
|
|
196
|
+
it("joins array with custom separator", () => {
|
|
197
|
+
expect(templateHelpers.join(["a", "b", "c"], " | ")).toBe("a | b | c");
|
|
198
|
+
});
|
|
199
|
+
it("handles empty array", () => {
|
|
200
|
+
expect(templateHelpers.join([])).toBe("");
|
|
201
|
+
});
|
|
202
|
+
it("handles single item", () => {
|
|
203
|
+
expect(templateHelpers.join(["single"])).toBe("single");
|
|
204
|
+
});
|
|
205
|
+
it("converts non-string items", () => {
|
|
206
|
+
expect(templateHelpers.join([1, 2, 3])).toBe("1, 2, 3");
|
|
207
|
+
});
|
|
208
|
+
it("handles mixed types", () => {
|
|
209
|
+
expect(templateHelpers.join([1, "two", true])).toBe("1, two, true");
|
|
210
|
+
});
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
describe("pluralize", () => {
|
|
214
|
+
it("returns singular for count of 1", () => {
|
|
215
|
+
expect(templateHelpers.pluralize("item", 1)).toBe("item");
|
|
216
|
+
});
|
|
217
|
+
it("returns plural for count of 0", () => {
|
|
218
|
+
expect(templateHelpers.pluralize("item", 0)).toBe("items");
|
|
219
|
+
});
|
|
220
|
+
it("returns plural for count > 1", () => {
|
|
221
|
+
expect(templateHelpers.pluralize("item", 5)).toBe("items");
|
|
222
|
+
});
|
|
223
|
+
it("handles negative counts", () => {
|
|
224
|
+
expect(templateHelpers.pluralize("item", -1)).toBe("items");
|
|
225
|
+
});
|
|
226
|
+
});
|
|
227
|
+
});
|