@canonical/summon-core 0.27.0 → 0.28.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
CHANGED
|
@@ -13,12 +13,14 @@ generate: (answers) => writeFile(`src/${answers.name}.ts`, code)
|
|
|
13
13
|
## Installation
|
|
14
14
|
|
|
15
15
|
```bash
|
|
16
|
-
bun add @canonical/summon
|
|
16
|
+
bun add -g @canonical/summon
|
|
17
17
|
```
|
|
18
18
|
|
|
19
19
|
## Using Generators
|
|
20
20
|
|
|
21
21
|
Summon discovers generators from installed packages automatically.
|
|
22
|
+
See [installing generator packages](#installing-generator-packages) to install an existing generator package,
|
|
23
|
+
or [creating generators](#creating-generators) to create a new one.
|
|
22
24
|
|
|
23
25
|
```bash
|
|
24
26
|
# See what's available
|
|
@@ -139,7 +141,7 @@ Generator packages follow the naming convention `summon-*` or `@scope/summon-*`:
|
|
|
139
141
|
|
|
140
142
|
```bash
|
|
141
143
|
# Install a generator package
|
|
142
|
-
bun add @canonical/summon-component
|
|
144
|
+
bun add -g @canonical/summon-component
|
|
143
145
|
|
|
144
146
|
# Now available (completions work immediately!)
|
|
145
147
|
summon component react
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@canonical/summon-core",
|
|
3
3
|
"description": "Code generation framework: generator definitions, templates, stamps, discovery. Built on @canonical/task.",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.28.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "src/index.ts",
|
|
7
7
|
"types": "src/index.ts",
|
|
@@ -33,6 +33,9 @@
|
|
|
33
33
|
"url": "https://github.com/canonical/pragma/issues"
|
|
34
34
|
},
|
|
35
35
|
"homepage": "https://github.com/canonical/pragma#readme",
|
|
36
|
+
"publishConfig": {
|
|
37
|
+
"access": "public"
|
|
38
|
+
},
|
|
36
39
|
"scripts": {
|
|
37
40
|
"build": "echo 'No build needed - runs directly from TypeScript'",
|
|
38
41
|
"build:all": "bun run build",
|
|
@@ -48,9 +51,9 @@
|
|
|
48
51
|
},
|
|
49
52
|
"devDependencies": {
|
|
50
53
|
"@biomejs/biome": "2.4.9",
|
|
51
|
-
"@canonical/biome-config": "^0.
|
|
52
|
-
"@canonical/typescript-config": "^0.
|
|
53
|
-
"@canonical/webarchitect": "^0.
|
|
54
|
+
"@canonical/biome-config": "^0.28.0",
|
|
55
|
+
"@canonical/typescript-config": "^0.28.0",
|
|
56
|
+
"@canonical/webarchitect": "^0.28.0",
|
|
54
57
|
"@types/bun": "^1.3.10",
|
|
55
58
|
"@types/ejs": "^3.1.5",
|
|
56
59
|
"@types/node": "^24.12.0",
|
|
@@ -58,10 +61,10 @@
|
|
|
58
61
|
"vitest": "^4.0.18"
|
|
59
62
|
},
|
|
60
63
|
"dependencies": {
|
|
61
|
-
"@canonical/task": "^0.
|
|
62
|
-
"@canonical/utils": "^0.
|
|
64
|
+
"@canonical/task": "^0.28.0",
|
|
65
|
+
"@canonical/utils": "^0.28.0",
|
|
63
66
|
"chalk": "^5.6.2",
|
|
64
67
|
"ejs": "^5.0.0"
|
|
65
68
|
},
|
|
66
|
-
"gitHead": "
|
|
69
|
+
"gitHead": "8297d72122bcba903f7e8833cb23ccbaa164ed56"
|
|
67
70
|
}
|
|
@@ -40,6 +40,28 @@ describe("template task", () => {
|
|
|
40
40
|
const mkdirEffect = effects.find((e) => e._tag === "MakeDir");
|
|
41
41
|
expect((mkdirEffect as { path: string }).path).toBe("/deep/nested/path");
|
|
42
42
|
});
|
|
43
|
+
|
|
44
|
+
it("uses provided content instead of reading from source", () => {
|
|
45
|
+
// The compiled-binary path: `content` is pre-loaded (e.g. from
|
|
46
|
+
// Bun.embeddedFiles), so no ReadFile effect should be emitted and the
|
|
47
|
+
// inline content is rendered directly.
|
|
48
|
+
const t = template({
|
|
49
|
+
source: "/templates/component.tsx.ejs",
|
|
50
|
+
content: "export const <%= name %> = () => null;",
|
|
51
|
+
dest: "/output/<%= name %>.tsx",
|
|
52
|
+
vars: { name: "Button" },
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
const { effects } = dryRun(t);
|
|
56
|
+
expect(effects.some((e) => e._tag === "ReadFile")).toBe(false);
|
|
57
|
+
expect(effects.some((e) => e._tag === "MakeDir")).toBe(true);
|
|
58
|
+
const writeEffect = effects.find((e) => e._tag === "WriteFile") as {
|
|
59
|
+
path: string;
|
|
60
|
+
content: string;
|
|
61
|
+
};
|
|
62
|
+
expect(writeEffect.path).toBe("/output/Button.tsx");
|
|
63
|
+
expect(writeEffect.content).toBe("export const Button = () => null;");
|
|
64
|
+
});
|
|
43
65
|
});
|
|
44
66
|
|
|
45
67
|
describe("templateDir task", () => {
|
|
@@ -1,11 +1,22 @@
|
|
|
1
1
|
import * as path from "node:path";
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
mkdir,
|
|
4
|
+
pure,
|
|
5
|
+
readFile,
|
|
6
|
+
type Task,
|
|
7
|
+
task,
|
|
8
|
+
writeFile,
|
|
9
|
+
} from "@canonical/task";
|
|
3
10
|
import ejsEngine from "../ejsEngine.js";
|
|
4
11
|
import renderString from "../renderString.js";
|
|
5
12
|
import type { TemplateOptions } from "./types.js";
|
|
6
13
|
|
|
7
14
|
/**
|
|
8
15
|
* Render a single template file to a destination.
|
|
16
|
+
*
|
|
17
|
+
* When `options.content` is provided, uses it directly instead of
|
|
18
|
+
* reading from `options.source`. This supports compiled binaries
|
|
19
|
+
* where template files are embedded and pre-loaded.
|
|
9
20
|
*/
|
|
10
21
|
export default function template(options: TemplateOptions): Task<void> {
|
|
11
22
|
const engine = options.engine ?? ejsEngine;
|
|
@@ -14,8 +25,16 @@ export default function template(options: TemplateOptions): Task<void> {
|
|
|
14
25
|
const destPath = renderString(options.dest, options.vars, engine);
|
|
15
26
|
const destDir = path.dirname(destPath);
|
|
16
27
|
|
|
28
|
+
// Key off `undefined`, not truthiness: a legitimately empty embedded
|
|
29
|
+
// template ("") must still use the provided content rather than falling
|
|
30
|
+
// back to reading from disk (which fails in compiled-binary mode).
|
|
31
|
+
const readSource =
|
|
32
|
+
options.content !== undefined
|
|
33
|
+
? task(pure(options.content))
|
|
34
|
+
: task(readFile(options.source));
|
|
35
|
+
|
|
17
36
|
return task(mkdir(destDir))
|
|
18
|
-
.chain(() =>
|
|
37
|
+
.chain(() => readSource)
|
|
19
38
|
.map((content) => renderString(content, options.vars, engine))
|
|
20
39
|
.chain((rendered) => task(writeFile(destPath, rendered)))
|
|
21
40
|
.unwrap();
|
|
@@ -1,14 +1,19 @@
|
|
|
1
1
|
import type TemplatingEngine from "../TemplatingEngine.js";
|
|
2
2
|
|
|
3
3
|
export interface TemplateOptions {
|
|
4
|
-
/** Path to the template file */
|
|
4
|
+
/** Path to the template file. */
|
|
5
5
|
source: string;
|
|
6
|
-
/** Destination path (can contain template variables) */
|
|
6
|
+
/** Destination path (can contain template variables). */
|
|
7
7
|
dest: string;
|
|
8
|
-
/** Variables to pass to the template */
|
|
8
|
+
/** Variables to pass to the template. */
|
|
9
9
|
vars: Record<string, unknown>;
|
|
10
|
-
/** Templating engine to use (defaults to EJS) */
|
|
10
|
+
/** Templating engine to use (defaults to EJS). */
|
|
11
11
|
engine?: TemplatingEngine;
|
|
12
|
+
/**
|
|
13
|
+
* Inline template content. When provided, skips reading from `source`.
|
|
14
|
+
* Use this when the template is embedded or pre-loaded (e.g. compiled binaries).
|
|
15
|
+
*/
|
|
16
|
+
content?: string;
|
|
12
17
|
}
|
|
13
18
|
|
|
14
19
|
export interface TemplateDirOptions {
|