@canonical/summon-core 0.18.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 +459 -0
- package/package.json +59 -0
- package/src/cli-types.ts +59 -0
- package/src/discovery/discoverGeneratorTree.ts +449 -0
- package/src/discovery/generatorCache.ts +16 -0
- package/src/discovery/index.ts +3 -0
- package/src/discovery/types.ts +18 -0
- package/src/index.ts +66 -0
- package/src/stamp.ts +165 -0
- package/src/template.test.ts +666 -0
- package/src/template.ts +543 -0
- package/src/types.ts +97 -0
package/src/stamp.ts
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stamp — Generated File Stamp Logic
|
|
3
|
+
*
|
|
4
|
+
* Stamps are a generation concern: a comment prepended to generated files
|
|
5
|
+
* identifying the generator that produced them. The base interpreter
|
|
6
|
+
* (@canonical/task) is domain-agnostic and does not know about stamps.
|
|
7
|
+
*
|
|
8
|
+
* Stamps are applied via composition: `runTaskWithStamp` wraps `runTask`
|
|
9
|
+
* from @canonical/task, intercepting WriteFile effects to prepend stamps.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import * as path from "node:path";
|
|
13
|
+
import {
|
|
14
|
+
type Effect,
|
|
15
|
+
type RunTaskOptions,
|
|
16
|
+
runTask,
|
|
17
|
+
type Task,
|
|
18
|
+
} from "@canonical/task";
|
|
19
|
+
|
|
20
|
+
// =============================================================================
|
|
21
|
+
// Stamp Config
|
|
22
|
+
// =============================================================================
|
|
23
|
+
|
|
24
|
+
export interface StampConfig {
|
|
25
|
+
/** Generator name (e.g., "@canonical/summon-package") */
|
|
26
|
+
generator: string;
|
|
27
|
+
/** Generator version (e.g., "0.1.0") */
|
|
28
|
+
version: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// =============================================================================
|
|
32
|
+
// Comment Styles
|
|
33
|
+
// =============================================================================
|
|
34
|
+
|
|
35
|
+
const COMMENT_STYLES: Record<
|
|
36
|
+
string,
|
|
37
|
+
{
|
|
38
|
+
single?: string;
|
|
39
|
+
blockStart?: string;
|
|
40
|
+
blockEnd?: string;
|
|
41
|
+
preferBlock?: boolean;
|
|
42
|
+
}
|
|
43
|
+
> = {
|
|
44
|
+
".ts": { single: "//" },
|
|
45
|
+
".tsx": { single: "//" },
|
|
46
|
+
".js": { single: "//" },
|
|
47
|
+
".jsx": { single: "//" },
|
|
48
|
+
".mjs": { single: "//" },
|
|
49
|
+
".cjs": { single: "//" },
|
|
50
|
+
".css": { blockStart: "/*", blockEnd: "*/", preferBlock: true },
|
|
51
|
+
".scss": { single: "//" },
|
|
52
|
+
".sass": { single: "//" },
|
|
53
|
+
".less": { single: "//" },
|
|
54
|
+
".html": { blockStart: "<!--", blockEnd: "-->" },
|
|
55
|
+
".htm": { blockStart: "<!--", blockEnd: "-->" },
|
|
56
|
+
".xml": { blockStart: "<!--", blockEnd: "-->" },
|
|
57
|
+
".svg": { blockStart: "<!--", blockEnd: "-->" },
|
|
58
|
+
".vue": { blockStart: "<!--", blockEnd: "-->" },
|
|
59
|
+
".svelte": { blockStart: "<!--", blockEnd: "-->" },
|
|
60
|
+
".yaml": { single: "#" },
|
|
61
|
+
".yml": { single: "#" },
|
|
62
|
+
".toml": { single: "#" },
|
|
63
|
+
".sh": { single: "#" },
|
|
64
|
+
".bash": { single: "#" },
|
|
65
|
+
".zsh": { single: "#" },
|
|
66
|
+
".py": { single: "#" },
|
|
67
|
+
".rb": { single: "#" },
|
|
68
|
+
".go": { single: "//" },
|
|
69
|
+
".rs": { single: "//" },
|
|
70
|
+
".java": { single: "//" },
|
|
71
|
+
".kt": { single: "//" },
|
|
72
|
+
".swift": { single: "//" },
|
|
73
|
+
".c": { single: "//" },
|
|
74
|
+
".cpp": { single: "//" },
|
|
75
|
+
".h": { single: "//" },
|
|
76
|
+
".php": { single: "//" },
|
|
77
|
+
".md": { blockStart: "<!--", blockEnd: "-->" },
|
|
78
|
+
".mdx": { blockStart: "{/*", blockEnd: "*/}" },
|
|
79
|
+
".sql": { single: "--" },
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
// =============================================================================
|
|
83
|
+
// Stamp Application (Pure Function)
|
|
84
|
+
// =============================================================================
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Apply a stamp to file content based on file extension.
|
|
88
|
+
* Returns the original content if the file type doesn't support comments.
|
|
89
|
+
*/
|
|
90
|
+
export const applyStamp = (
|
|
91
|
+
filePath: string,
|
|
92
|
+
content: string,
|
|
93
|
+
stamp: StampConfig,
|
|
94
|
+
): string => {
|
|
95
|
+
const ext = path.extname(filePath).toLowerCase();
|
|
96
|
+
const style = COMMENT_STYLES[ext];
|
|
97
|
+
|
|
98
|
+
if (!style) {
|
|
99
|
+
return content;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const stampText = `Generated by ${stamp.generator} v${stamp.version}`;
|
|
103
|
+
let stampLine: string;
|
|
104
|
+
|
|
105
|
+
if (style.preferBlock || (!style.single && style.blockStart)) {
|
|
106
|
+
if (!style.blockStart || !style.blockEnd) return content;
|
|
107
|
+
stampLine = `${style.blockStart} ${stampText} ${style.blockEnd}`;
|
|
108
|
+
} else if (style.single) {
|
|
109
|
+
stampLine = `${style.single} ${stampText}`;
|
|
110
|
+
} else {
|
|
111
|
+
return content;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// Handle shebang - place stamp after it
|
|
115
|
+
if (content.startsWith("#!")) {
|
|
116
|
+
const firstNewline = content.indexOf("\n");
|
|
117
|
+
if (firstNewline !== -1) {
|
|
118
|
+
const shebang = content.slice(0, firstNewline + 1);
|
|
119
|
+
const rest = content.slice(firstNewline + 1);
|
|
120
|
+
return `${shebang}${stampLine}\n\n${rest}`;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return `${stampLine}\n\n${content}`;
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
// =============================================================================
|
|
128
|
+
// Stamped Task Runner
|
|
129
|
+
// =============================================================================
|
|
130
|
+
|
|
131
|
+
export interface RunTaskWithStampOptions extends RunTaskOptions {
|
|
132
|
+
/** Stamp configuration for generated files. If provided, all written files get a stamp comment. */
|
|
133
|
+
stamp?: StampConfig;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Run a task with stamp support. Wraps `runTask` from @canonical/task,
|
|
138
|
+
* intercepting WriteFile effects to prepend generated-file stamps.
|
|
139
|
+
*/
|
|
140
|
+
export const runTaskWithStamp = async <A>(
|
|
141
|
+
task: Task<A>,
|
|
142
|
+
options: RunTaskWithStampOptions = {},
|
|
143
|
+
): Promise<A> => {
|
|
144
|
+
const { stamp, onEffectStart: userOnEffectStart, ...restOptions } = options;
|
|
145
|
+
|
|
146
|
+
if (!stamp) {
|
|
147
|
+
return runTask(task, options);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// Wrap onEffectStart to mutate WriteFile content in-place before execution.
|
|
151
|
+
// This is the composition point: stamps are applied as a pre-processing step
|
|
152
|
+
// on the effect data, not baked into the interpreter.
|
|
153
|
+
const onEffectStart = (effect: Effect) => {
|
|
154
|
+
if (effect._tag === "WriteFile") {
|
|
155
|
+
(effect as { content: string }).content = applyStamp(
|
|
156
|
+
effect.path,
|
|
157
|
+
effect.content,
|
|
158
|
+
stamp,
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
userOnEffectStart?.(effect);
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
return runTask(task, { ...restOptions, onEffectStart });
|
|
165
|
+
};
|