@seasb/metafab 1.0.0 → 2.0.0-next.1
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 +1 -1
- package/dist/cli.js +118 -16
- package/dist/cli.js.map +1 -1
- package/dist/commands/build.d.ts +29 -0
- package/dist/commands/build.js +111 -25
- package/dist/commands/build.js.map +1 -1
- package/dist/commands/check.js +13 -2
- package/dist/commands/check.js.map +1 -1
- package/dist/commands/create.js +1 -0
- package/dist/commands/create.js.map +1 -1
- package/dist/commands/env.d.ts +4 -0
- package/dist/commands/env.js +362 -0
- package/dist/commands/env.js.map +1 -0
- package/dist/commands/map.js +1 -2
- package/dist/commands/map.js.map +1 -1
- package/dist/commands/pipeline-add.js +1 -0
- package/dist/commands/pipeline-add.js.map +1 -1
- package/dist/commands/pipeline-remove.js +1 -0
- package/dist/commands/pipeline-remove.js.map +1 -1
- package/dist/commands/publish.js +8 -2
- package/dist/commands/publish.js.map +1 -1
- package/dist/core/build/agents.d.ts +7 -4
- package/dist/core/build/agents.js +16 -9
- package/dist/core/build/agents.js.map +1 -1
- package/dist/core/build/index.d.ts +1 -0
- package/dist/core/build/index.js +1 -0
- package/dist/core/build/index.js.map +1 -1
- package/dist/core/build/plan.d.ts +35 -1
- package/dist/core/build/plan.js +156 -36
- package/dist/core/build/plan.js.map +1 -1
- package/dist/core/build/preview.d.ts +39 -0
- package/dist/core/build/preview.js +344 -0
- package/dist/core/build/preview.js.map +1 -0
- package/dist/core/build/skill.d.ts +39 -0
- package/dist/core/build/skill.js +120 -41
- package/dist/core/build/skill.js.map +1 -1
- package/dist/core/check/index.d.ts +5 -1
- package/dist/core/check/index.js +8 -2
- package/dist/core/check/index.js.map +1 -1
- package/dist/core/config/parse.js +3 -0
- package/dist/core/config/parse.js.map +1 -1
- package/dist/core/config/source.d.ts +11 -0
- package/dist/core/config/source.js +23 -0
- package/dist/core/config/source.js.map +1 -1
- package/dist/core/config/validate.js +75 -37
- package/dist/core/config/validate.js.map +1 -1
- package/dist/core/config/writers.js +9 -1
- package/dist/core/config/writers.js.map +1 -1
- package/dist/core/edit/create.js +1 -1
- package/dist/core/edit/create.js.map +1 -1
- package/dist/core/edit/factory-yaml.js +4 -2
- package/dist/core/edit/factory-yaml.js.map +1 -1
- package/dist/core/edit/paths.js +8 -3
- package/dist/core/edit/paths.js.map +1 -1
- package/dist/core/edit/pipeline.js +12 -6
- package/dist/core/edit/pipeline.js.map +1 -1
- package/dist/core/env/condition.d.ts +40 -0
- package/dist/core/env/condition.js +283 -0
- package/dist/core/env/condition.js.map +1 -0
- package/dist/core/env/declarations.d.ts +16 -0
- package/dist/core/env/declarations.js +328 -0
- package/dist/core/env/declarations.js.map +1 -0
- package/dist/core/env/directives.d.ts +75 -0
- package/dist/core/env/directives.js +429 -0
- package/dist/core/env/directives.js.map +1 -0
- package/dist/core/env/git-config.d.ts +15 -0
- package/dist/core/env/git-config.js +110 -0
- package/dist/core/env/git-config.js.map +1 -0
- package/dist/core/env/imports.d.ts +60 -0
- package/dist/core/env/imports.js +217 -0
- package/dist/core/env/imports.js.map +1 -0
- package/dist/core/env/index.d.ts +6 -0
- package/dist/core/env/index.js +7 -0
- package/dist/core/env/index.js.map +1 -0
- package/dist/core/env/markdown.d.ts +22 -0
- package/dist/core/env/markdown.js +33 -0
- package/dist/core/env/markdown.js.map +1 -0
- package/dist/core/env/render.d.ts +13 -0
- package/dist/core/env/render.js +65 -0
- package/dist/core/env/render.js.map +1 -0
- package/dist/core/env/report.d.ts +29 -0
- package/dist/core/env/report.js +39 -0
- package/dist/core/env/report.js.map +1 -0
- package/dist/core/env/scenarios.d.ts +40 -0
- package/dist/core/env/scenarios.js +124 -0
- package/dist/core/env/scenarios.js.map +1 -0
- package/dist/core/env/select.d.ts +30 -0
- package/dist/core/env/select.js +191 -0
- package/dist/core/env/select.js.map +1 -0
- package/dist/core/env/types.d.ts +38 -0
- package/dist/core/env/types.js +2 -0
- package/dist/core/env/types.js.map +1 -0
- package/dist/core/env/validate.d.ts +25 -0
- package/dist/core/env/validate.js +212 -0
- package/dist/core/env/validate.js.map +1 -0
- package/dist/core/factory/factory-yaml.d.ts +14 -1
- package/dist/core/factory/factory-yaml.js +166 -3
- package/dist/core/factory/factory-yaml.js.map +1 -1
- package/dist/core/factory/load.js +2 -0
- package/dist/core/factory/load.js.map +1 -1
- package/dist/core/factory/types.d.ts +14 -1
- package/dist/core/factory/validate.d.ts +5 -2
- package/dist/core/factory/validate.js +30 -11
- package/dist/core/factory/validate.js.map +1 -1
- package/dist/core/import/run.js +17 -0
- package/dist/core/import/run.js.map +1 -1
- package/dist/core/publish/git-target.js +35 -22
- package/dist/core/publish/git-target.js.map +1 -1
- package/dist/core/publish/npm-target.js +11 -4
- package/dist/core/publish/npm-target.js.map +1 -1
- package/dist/core/publish/package-json.d.ts +1 -1
- package/dist/core/publish/prepare.d.ts +11 -0
- package/dist/core/publish/prepare.js +11 -2
- package/dist/core/publish/prepare.js.map +1 -1
- package/dist/core/publish/publish.js +15 -2
- package/dist/core/publish/publish.js.map +1 -1
- package/dist/core/publish/readme.js +1 -4
- package/dist/core/publish/readme.js.map +1 -1
- package/dist/core/sources/cache.js +108 -44
- package/dist/core/sources/cache.js.map +1 -1
- package/dist/core/sources/extract.js +35 -72
- package/dist/core/sources/extract.js.map +1 -1
- package/dist/core/sources/git.js +48 -34
- package/dist/core/sources/git.js.map +1 -1
- package/dist/core/sources/https.js +23 -13
- package/dist/core/sources/https.js.map +1 -1
- package/dist/core/sources/npm-cli.js +8 -0
- package/dist/core/sources/npm-cli.js.map +1 -1
- package/dist/core/sources/npm.js +59 -34
- package/dist/core/sources/npm.js.map +1 -1
- package/dist/core/sources/parse.d.ts +20 -6
- package/dist/core/sources/parse.js +75 -18
- package/dist/core/sources/parse.js.map +1 -1
- package/dist/core/sources/path.js +24 -9
- package/dist/core/sources/path.js.map +1 -1
- package/dist/core/sources/problems.d.ts +29 -0
- package/dist/core/sources/problems.js +36 -0
- package/dist/core/sources/problems.js.map +1 -0
- package/dist/core/sources/types.d.ts +1 -1
- package/dist/core/sources/validate.js +18 -13
- package/dist/core/sources/validate.js.map +1 -1
- package/dist/core/summary.d.ts +5 -3
- package/dist/core/summary.js +8 -2
- package/dist/core/summary.js.map +1 -1
- package/dist/core/update/run.js +15 -12
- package/dist/core/update/run.js.map +1 -1
- package/dist/errors.d.ts +14 -4
- package/dist/errors.js +1 -1
- package/dist/errors.js.map +1 -1
- package/dist/help.d.ts +7 -2
- package/dist/help.js +16 -2
- package/dist/help.js.map +1 -1
- package/dist/input.js +1 -0
- package/dist/input.js.map +1 -1
- package/dist/output.d.ts +1 -1
- package/dist/util/fs.d.ts +6 -0
- package/dist/util/fs.js +9 -0
- package/dist/util/fs.js.map +1 -1
- package/dist/util/yaml.d.ts +1 -1
- package/package.json +2 -1
- package/resources/metafab-skill/SKILL.md +76 -2
- package/dist/commands/not-implemented.d.ts +0 -2
- package/dist/commands/not-implemented.js +0 -19
- package/dist/commands/not-implemented.js.map +0 -1
|
@@ -0,0 +1,344 @@
|
|
|
1
|
+
import { chmod, lstat, mkdir, open, readdir, realpath, rm, rmdir, stat } from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { ExitCode, MetafabError } from "../../errors.js";
|
|
4
|
+
import { AGENTS_SKILLS_DIRECTORY, CLAUDE_SKILLS_DIRECTORY } from "../config/destinations.js";
|
|
5
|
+
import { resolveSelection } from "../env/select.js";
|
|
6
|
+
import { desiredOutput, skillWrites } from "./plan.js";
|
|
7
|
+
import { isInside } from "./skill.js";
|
|
8
|
+
/**
|
|
9
|
+
* Directories a preview may not write into. The next `build` would find the
|
|
10
|
+
* preview's output there and treat it as unowned.
|
|
11
|
+
*/
|
|
12
|
+
const FORBIDDEN = [
|
|
13
|
+
{ directory: ".factory", label: ".factory" },
|
|
14
|
+
{ directory: CLAUDE_SKILLS_DIRECTORY, label: ".claude/skills" },
|
|
15
|
+
{ directory: AGENTS_SKILLS_DIRECTORY, label: ".agents/skills" },
|
|
16
|
+
];
|
|
17
|
+
function notADirectory(given) {
|
|
18
|
+
return {
|
|
19
|
+
level: "error",
|
|
20
|
+
file: given,
|
|
21
|
+
message: `--out ${given} is not a directory`,
|
|
22
|
+
fix: "Pass a directory that does not exist or is empty",
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
function notEmpty(given) {
|
|
26
|
+
return {
|
|
27
|
+
level: "error",
|
|
28
|
+
file: given,
|
|
29
|
+
message: `--out ${given} is not empty`,
|
|
30
|
+
fix: "Choose a new directory or empty it",
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
function insideDestination(given, label) {
|
|
34
|
+
return {
|
|
35
|
+
level: "error",
|
|
36
|
+
file: given,
|
|
37
|
+
message: `--out ${given} is inside ${label}, where build would find it`,
|
|
38
|
+
fix: "Choose a directory outside .factory/ and the skill destinations",
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
async function isDirectory(target) {
|
|
42
|
+
try {
|
|
43
|
+
return (await stat(target)).isDirectory();
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
/** What is at `target` now, following a link to a directory. */
|
|
50
|
+
async function entryKind(target) {
|
|
51
|
+
let info;
|
|
52
|
+
try {
|
|
53
|
+
info = await lstat(target);
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
const code = error.code;
|
|
57
|
+
if (code === "ENOENT")
|
|
58
|
+
return "absent";
|
|
59
|
+
// A component above the path is a file, so the path cannot be a directory.
|
|
60
|
+
if (code === "ENOTDIR")
|
|
61
|
+
return "other";
|
|
62
|
+
throw error;
|
|
63
|
+
}
|
|
64
|
+
if (info.isDirectory())
|
|
65
|
+
return "directory";
|
|
66
|
+
if (info.isSymbolicLink() && (await isDirectory(target)))
|
|
67
|
+
return "directory";
|
|
68
|
+
return "other";
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* A path with every existing component resolved through links and the missing
|
|
72
|
+
* tail joined on unchanged, so a destination that does not exist yet, or a
|
|
73
|
+
* `.claude` that is a link, is compared on the same footing as `out`.
|
|
74
|
+
*/
|
|
75
|
+
async function canonical(target) {
|
|
76
|
+
const missing = [];
|
|
77
|
+
let current = target;
|
|
78
|
+
for (;;) {
|
|
79
|
+
try {
|
|
80
|
+
const real = await realpath(current);
|
|
81
|
+
return path.join(real, ...missing.reverse());
|
|
82
|
+
}
|
|
83
|
+
catch (error) {
|
|
84
|
+
const code = error.code;
|
|
85
|
+
if (code !== "ENOENT" && code !== "ENOTDIR")
|
|
86
|
+
throw error;
|
|
87
|
+
missing.push(path.basename(current));
|
|
88
|
+
const parent = path.dirname(current);
|
|
89
|
+
if (parent === current)
|
|
90
|
+
return path.join(target);
|
|
91
|
+
current = parent;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* What `metafab build --out <dir>` writes: the compiled skills for the
|
|
97
|
+
* selection, and the root block beside them. Nothing in the repository is read
|
|
98
|
+
* for ownership or written.
|
|
99
|
+
*/
|
|
100
|
+
export async function planPreview(factory, config, cliVersion, out, options = {}) {
|
|
101
|
+
const plan = {
|
|
102
|
+
out: out === "" ? "" : path.resolve(out),
|
|
103
|
+
created: false,
|
|
104
|
+
writes: [],
|
|
105
|
+
errors: [],
|
|
106
|
+
conflicts: [],
|
|
107
|
+
warnings: [],
|
|
108
|
+
};
|
|
109
|
+
if (out === "") {
|
|
110
|
+
plan.errors.push({
|
|
111
|
+
level: "error",
|
|
112
|
+
file: "--out",
|
|
113
|
+
message: "--out needs a directory",
|
|
114
|
+
fix: "Pass a directory, such as --out /tmp/preview-amp",
|
|
115
|
+
});
|
|
116
|
+
return plan;
|
|
117
|
+
}
|
|
118
|
+
const kind = await entryKind(plan.out);
|
|
119
|
+
if (kind === "absent") {
|
|
120
|
+
plan.created = true;
|
|
121
|
+
}
|
|
122
|
+
else if (kind === "other") {
|
|
123
|
+
plan.errors.push(notADirectory(out));
|
|
124
|
+
return plan;
|
|
125
|
+
}
|
|
126
|
+
else if ((await readdir(plan.out)).length > 0) {
|
|
127
|
+
plan.conflicts.push(notEmpty(out));
|
|
128
|
+
return plan;
|
|
129
|
+
}
|
|
130
|
+
const target = await canonical(plan.out);
|
|
131
|
+
for (const forbidden of FORBIDDEN) {
|
|
132
|
+
const location = await canonical(path.join(factory.root, ...forbidden.directory.split("/")));
|
|
133
|
+
if (isInside(location, target)) {
|
|
134
|
+
plan.errors.push(insideDestination(out, forbidden.label));
|
|
135
|
+
return plan;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
// A caller that read `--env` has already resolved the selection; otherwise the
|
|
139
|
+
// developer's process environment and Git config decide it.
|
|
140
|
+
const selection = options.selection ??
|
|
141
|
+
(await resolveSelection(factory.factoryYaml?.env ?? [], { root: factory.root }));
|
|
142
|
+
plan.warnings.push(...selection.warnings);
|
|
143
|
+
const desired = await desiredOutput(factory, config, cliVersion, selection);
|
|
144
|
+
plan.warnings.push(...desired.warnings);
|
|
145
|
+
if (desired.errors.length > 0) {
|
|
146
|
+
plan.errors.push(...desired.errors);
|
|
147
|
+
return plan;
|
|
148
|
+
}
|
|
149
|
+
for (const [directory, skills] of desired.skills) {
|
|
150
|
+
for (const skill of skills) {
|
|
151
|
+
plan.writes.push(...skillWrites(directory, skill));
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
if (desired.agentsBlock !== undefined) {
|
|
155
|
+
plan.writes.push({
|
|
156
|
+
path: "AGENTS.md",
|
|
157
|
+
content: Buffer.from(`${desired.agentsBlock}\n`, "utf8"),
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
return plan;
|
|
161
|
+
}
|
|
162
|
+
/** A failure the preview handles: it rolls back, then throws a MetafabError. */
|
|
163
|
+
class PreviewStop extends Error {
|
|
164
|
+
problem;
|
|
165
|
+
exitCode;
|
|
166
|
+
constructor(problem, exitCode) {
|
|
167
|
+
super(problem.message);
|
|
168
|
+
this.problem = problem;
|
|
169
|
+
this.exitCode = exitCode;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
function conflictProblem(target) {
|
|
173
|
+
return {
|
|
174
|
+
level: "error",
|
|
175
|
+
file: target,
|
|
176
|
+
message: `${target} appeared while the preview was being written`,
|
|
177
|
+
fix: "Choose a new directory or empty it, then run metafab build --out again",
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
function writeProblem(target, error) {
|
|
181
|
+
const code = error?.code ?? "error";
|
|
182
|
+
return {
|
|
183
|
+
level: "error",
|
|
184
|
+
file: target,
|
|
185
|
+
message: `Cannot write ${target}`,
|
|
186
|
+
fix: `Check that it is writable and has space left (${code}), then run metafab build --out again`,
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
function removeWarning(target) {
|
|
190
|
+
return {
|
|
191
|
+
level: "warning",
|
|
192
|
+
file: target,
|
|
193
|
+
message: `Could not remove ${target}; remove it by hand`,
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
async function removeEntry(target) {
|
|
197
|
+
const info = await lstat(target);
|
|
198
|
+
if (info.isDirectory())
|
|
199
|
+
await rmdir(target);
|
|
200
|
+
else
|
|
201
|
+
await rm(target);
|
|
202
|
+
}
|
|
203
|
+
/** A directory that stays because an entry inside it stayed, or because it is gone. */
|
|
204
|
+
function leftInPlace(error) {
|
|
205
|
+
const code = error?.code;
|
|
206
|
+
return code === "ENOTEMPTY" || code === "EEXIST" || code === "ENOENT";
|
|
207
|
+
}
|
|
208
|
+
/** Missing directories from the highest down to `target`, `target` last. */
|
|
209
|
+
async function missingChain(target) {
|
|
210
|
+
const missing = [];
|
|
211
|
+
let current = target;
|
|
212
|
+
for (;;) {
|
|
213
|
+
try {
|
|
214
|
+
await lstat(current);
|
|
215
|
+
return missing.reverse();
|
|
216
|
+
}
|
|
217
|
+
catch (error) {
|
|
218
|
+
const code = error.code;
|
|
219
|
+
// ENOTDIR means a file above `current` is in the way; keep walking so the
|
|
220
|
+
// walk finds it and the caller can report it as an entry that appeared.
|
|
221
|
+
if (code !== "ENOENT" && code !== "ENOTDIR") {
|
|
222
|
+
throw new PreviewStop(writeProblem(current, error), ExitCode.Failed);
|
|
223
|
+
}
|
|
224
|
+
missing.push(current);
|
|
225
|
+
const parent = path.dirname(current);
|
|
226
|
+
if (parent === current)
|
|
227
|
+
return missing.reverse();
|
|
228
|
+
current = parent;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Creates the preview's directories and files exclusively, so it never writes
|
|
234
|
+
* into an entry it did not make. Each file is recorded as soon as its exclusive
|
|
235
|
+
* create succeeds, so a write that fails part way through still removes it. On a
|
|
236
|
+
* failure it removes every entry it created, deepest first, and leaves a
|
|
237
|
+
* directory that existed before the run in place.
|
|
238
|
+
*/
|
|
239
|
+
export async function writePreview(plan, io = {}) {
|
|
240
|
+
if (plan.writes.length === 0)
|
|
241
|
+
return [];
|
|
242
|
+
const write = io.write ??
|
|
243
|
+
(async (file, content) => {
|
|
244
|
+
await file.writeFile(content);
|
|
245
|
+
});
|
|
246
|
+
const remove = io.remove ?? removeEntry;
|
|
247
|
+
const created = [];
|
|
248
|
+
const made = new Set();
|
|
249
|
+
const changes = [];
|
|
250
|
+
const record = (target, directory) => {
|
|
251
|
+
created.push({ path: target, directory });
|
|
252
|
+
made.add(target);
|
|
253
|
+
};
|
|
254
|
+
const create = async (directory) => {
|
|
255
|
+
try {
|
|
256
|
+
await mkdir(directory);
|
|
257
|
+
record(directory, true);
|
|
258
|
+
}
|
|
259
|
+
catch (error) {
|
|
260
|
+
if (error.code !== "EEXIST") {
|
|
261
|
+
throw new PreviewStop(writeProblem(directory, error), ExitCode.Failed);
|
|
262
|
+
}
|
|
263
|
+
// Above `out` another process may make a directory; it is not ours, and
|
|
264
|
+
// not a conflict. At or below `out` the preflight saw nothing, so anything
|
|
265
|
+
// there appeared while the preview was being written.
|
|
266
|
+
if (isInside(plan.out, directory)) {
|
|
267
|
+
throw new PreviewStop(conflictProblem(directory), ExitCode.NotOwned);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
const rollback = async () => {
|
|
272
|
+
const warnings = [];
|
|
273
|
+
const deepestFirst = [...created].sort((left, right) => right.path.length - left.path.length);
|
|
274
|
+
for (const entry of deepestFirst) {
|
|
275
|
+
try {
|
|
276
|
+
await remove(entry.path);
|
|
277
|
+
}
|
|
278
|
+
catch (error) {
|
|
279
|
+
if (entry.directory && leftInPlace(error))
|
|
280
|
+
continue;
|
|
281
|
+
warnings.push(removeWarning(entry.path));
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
return warnings;
|
|
285
|
+
};
|
|
286
|
+
try {
|
|
287
|
+
for (const directory of await missingChain(plan.out)) {
|
|
288
|
+
await create(directory);
|
|
289
|
+
}
|
|
290
|
+
for (const entry of plan.writes) {
|
|
291
|
+
const target = path.join(plan.out, ...entry.path.split("/"));
|
|
292
|
+
const chain = await missingChain(path.dirname(target));
|
|
293
|
+
// The nearest directory that already exists is either `out`, one this run
|
|
294
|
+
// made, or something that appeared after the preflight.
|
|
295
|
+
const existing = chain.length > 0 ? path.dirname(chain[0]) : path.dirname(target);
|
|
296
|
+
if (isInside(plan.out, existing) && existing !== plan.out && !made.has(existing)) {
|
|
297
|
+
throw new PreviewStop(conflictProblem(existing), ExitCode.NotOwned);
|
|
298
|
+
}
|
|
299
|
+
for (const directory of chain) {
|
|
300
|
+
await create(directory);
|
|
301
|
+
}
|
|
302
|
+
let file;
|
|
303
|
+
try {
|
|
304
|
+
// Exclusive, so the file is either this run's or one that appeared after
|
|
305
|
+
// the preflight. Recorded the moment it exists, so a failure part way
|
|
306
|
+
// through the content still removes it.
|
|
307
|
+
file = await open(target, "wx");
|
|
308
|
+
}
|
|
309
|
+
catch (error) {
|
|
310
|
+
if (error.code === "EEXIST") {
|
|
311
|
+
throw new PreviewStop(conflictProblem(target), ExitCode.NotOwned);
|
|
312
|
+
}
|
|
313
|
+
throw new PreviewStop(writeProblem(target, error), ExitCode.Failed);
|
|
314
|
+
}
|
|
315
|
+
record(target, false);
|
|
316
|
+
try {
|
|
317
|
+
await write(file, entry.content);
|
|
318
|
+
if (entry.mode !== undefined)
|
|
319
|
+
await chmod(target, entry.mode);
|
|
320
|
+
await file.close();
|
|
321
|
+
}
|
|
322
|
+
catch (error) {
|
|
323
|
+
// Best effort: the file is recorded, so rollback removes it either way.
|
|
324
|
+
await file.close().catch(() => undefined);
|
|
325
|
+
throw new PreviewStop(writeProblem(target, error), ExitCode.Failed);
|
|
326
|
+
}
|
|
327
|
+
changes.push({ path: entry.path, action: "created" });
|
|
328
|
+
}
|
|
329
|
+
return changes;
|
|
330
|
+
}
|
|
331
|
+
catch (error) {
|
|
332
|
+
const warnings = await rollback();
|
|
333
|
+
if (!(error instanceof PreviewStop))
|
|
334
|
+
throw error;
|
|
335
|
+
const conflict = error.exitCode === ExitCode.NotOwned;
|
|
336
|
+
throw new MetafabError(conflict ? "The preview conflicts with what is on disk." : "The preview could not be written.", {
|
|
337
|
+
exitCode: error.exitCode,
|
|
338
|
+
code: conflict ? "preview-conflict" : "preview-write-failed",
|
|
339
|
+
problems: [error.problem, ...warnings],
|
|
340
|
+
cause: error,
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
//# sourceMappingURL=preview.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"preview.js","sourceRoot":"","sources":["../../../src/core/build/preview.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAGjG,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AA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
|
|
20
20
|
/** Repository-relative source directory, used in messages and the generated comment. */
|
|
21
21
|
sourcePath: string;
|
|
22
22
|
}
|
|
23
|
+
/** One Markdown file handed to a `CompileSkillOptions.render` callback. */
|
|
24
|
+
export interface RenderFile {
|
|
25
|
+
/** Path inside the installed skill directory, with forward slashes. */
|
|
26
|
+
relative: string;
|
|
27
|
+
/** LF-normalised body text: everything after the frontmatter, when the file has one. */
|
|
28
|
+
text: string;
|
|
29
|
+
/** Line number of `text`'s first line in the source file. */
|
|
30
|
+
firstLine: number;
|
|
31
|
+
}
|
|
23
32
|
export interface CompileSkillOptions {
|
|
24
33
|
/** Absolute `.factory` directory. Symlinks must resolve inside it. */
|
|
25
34
|
factoryDir: string;
|
|
@@ -31,6 +40,13 @@ export interface CompileSkillOptions {
|
|
|
31
40
|
linkRoots?: string[];
|
|
32
41
|
/** Generated comment line. Defaults to the factory skill comment. */
|
|
33
42
|
comment?: string;
|
|
43
|
+
/**
|
|
44
|
+
* Renders a Markdown file for the environment selection. Absent copies the
|
|
45
|
+
* text unchanged, as the built-in skill and adoption do.
|
|
46
|
+
*/
|
|
47
|
+
render?: (file: RenderFile) => Promise<string>;
|
|
48
|
+
/** A partial match's label, for `**Applies only <label>.**` after the comment. */
|
|
49
|
+
appliesOnly?: string;
|
|
34
50
|
}
|
|
35
51
|
export interface CompiledSkill {
|
|
36
52
|
name: string;
|
|
@@ -38,6 +54,20 @@ export interface CompiledSkill {
|
|
|
38
54
|
files: CompiledFile[];
|
|
39
55
|
problems: Problem[];
|
|
40
56
|
}
|
|
57
|
+
/** A source file the walk found, before it is read. */
|
|
58
|
+
export interface CollectedFile {
|
|
59
|
+
/** Path inside the installed skill directory, with forward slashes. */
|
|
60
|
+
relative: string;
|
|
61
|
+
/** Absolute path to read, after following a symlink. */
|
|
62
|
+
target: string;
|
|
63
|
+
/** Installed permission bits: 0o755 when the source has any executable bit, else 0o644. */
|
|
64
|
+
mode: number;
|
|
65
|
+
}
|
|
66
|
+
export interface CollectedSkillFiles {
|
|
67
|
+
/** Sorted by `relative`, so two runs list a tree in the same order. */
|
|
68
|
+
files: CollectedFile[];
|
|
69
|
+
problems: Problem[];
|
|
70
|
+
}
|
|
41
71
|
export declare function generatedComment(sourcePath: string): string;
|
|
42
72
|
export declare function builtinComment(version: string): string;
|
|
43
73
|
export declare function cannotReadProblem(relativePath: string): Problem;
|
|
@@ -57,4 +87,13 @@ export declare function dedupeReadErrors(problems: readonly Problem[]): Problem[
|
|
|
57
87
|
* A missing `SKILL.md` (ENOENT/ENOTDIR) is simply not owned.
|
|
58
88
|
*/
|
|
59
89
|
export declare function isOwned(dir: string, displayPath?: string): Promise<boolean | Problem>;
|
|
90
|
+
/** True when `target` is `root` or below it, both absolute native paths. */
|
|
91
|
+
export declare function isInside(root: string, target: string): boolean;
|
|
92
|
+
/**
|
|
93
|
+
* Lists a skill's source files as `build` installs them, with the symlink
|
|
94
|
+
* containment and loop rules of the walk. `compileSkill` reads the files it
|
|
95
|
+
* returns; the environment validation walks the same list to check every
|
|
96
|
+
* Markdown file `build` would render.
|
|
97
|
+
*/
|
|
98
|
+
export declare function collectSkillFiles(source: SkillSource, options: CompileSkillOptions): Promise<CollectedSkillFiles>;
|
|
60
99
|
export declare function compileSkill(source: SkillSource, options: CompileSkillOptions): Promise<CompiledSkill>;
|
package/dist/core/build/skill.js
CHANGED
|
@@ -2,6 +2,7 @@ import { lstat, readFile, realpath, stat } from "node:fs/promises";
|
|
|
2
2
|
import path from "node:path";
|
|
3
3
|
import { readdirTyped } from "../../util/fs.js";
|
|
4
4
|
import { toLF } from "../../util/text.js";
|
|
5
|
+
import { markdownParts } from "../env/markdown.js";
|
|
5
6
|
export const GENERATED_COMMENT_PREFIX = "<!-- Generated by metafab build";
|
|
6
7
|
const utf8 = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
|
|
7
8
|
/**
|
|
@@ -97,30 +98,58 @@ export async function isOwned(dir, displayPath = dir) {
|
|
|
97
98
|
return unreadablePath(skillDisplay, error) ?? false;
|
|
98
99
|
}
|
|
99
100
|
}
|
|
100
|
-
|
|
101
|
+
/** True when `target` is `root` or below it, both absolute native paths. */
|
|
102
|
+
export function isInside(root, target) {
|
|
101
103
|
const relative = path.relative(root, target);
|
|
102
104
|
return (relative === "" ||
|
|
103
105
|
(!relative.startsWith(`..${path.sep}`) && relative !== ".." && !path.isAbsolute(relative)));
|
|
104
106
|
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
107
|
+
/** True for a file `build` renders: its extension, lowercased, is `.md`. */
|
|
108
|
+
function isMarkdown(relative) {
|
|
109
|
+
return path.extname(relative).toLowerCase() === ".md";
|
|
110
|
+
}
|
|
111
|
+
/** A Markdown file's text for the selection; anything else is left alone. */
|
|
112
|
+
async function renderMarkdown(relative, text, firstLine, options) {
|
|
113
|
+
if (options.render === undefined || !isMarkdown(relative))
|
|
114
|
+
return text;
|
|
115
|
+
return options.render({ relative, text, firstLine });
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Inserts the generated comment after the frontmatter, and the `Applies only`
|
|
119
|
+
* line after it when the skill applies in some environments only. Only the body
|
|
120
|
+
* below the frontmatter is rendered, so name and description stay the same in
|
|
121
|
+
* every environment.
|
|
122
|
+
*/
|
|
123
|
+
async function insertComment(text, comment, options) {
|
|
124
|
+
const parts = markdownParts(text);
|
|
125
|
+
if (parts.frontmatter === undefined) {
|
|
108
126
|
return {
|
|
109
127
|
ok: false,
|
|
110
128
|
message: "SKILL.md must start with a '---' frontmatter block",
|
|
111
129
|
fix: "Add a frontmatter block that starts with --- and ends with ---",
|
|
112
130
|
};
|
|
113
131
|
}
|
|
114
|
-
|
|
115
|
-
if (closing === -1) {
|
|
132
|
+
if (!parts.closed) {
|
|
116
133
|
return {
|
|
117
134
|
ok: false,
|
|
118
135
|
message: "SKILL.md has unterminated frontmatter",
|
|
119
136
|
fix: "Add a line that contains only --- after the frontmatter",
|
|
120
137
|
};
|
|
121
138
|
}
|
|
122
|
-
|
|
123
|
-
|
|
139
|
+
const body = await renderMarkdown("SKILL.md", parts.body, parts.firstLine, options);
|
|
140
|
+
const rendered = body.split("\n");
|
|
141
|
+
// `prefix` ends with the newline that starts the body, or with the closing
|
|
142
|
+
// `---` itself when the file ends there, so only then does the comment need
|
|
143
|
+
// one of its own.
|
|
144
|
+
const head = `${parts.prefix}${parts.prefix.endsWith("\n") ? "" : "\n"}${comment}`;
|
|
145
|
+
const next = [head];
|
|
146
|
+
if (options.appliesOnly !== undefined)
|
|
147
|
+
next.push(`**Applies only ${options.appliesOnly}.**`);
|
|
148
|
+
// A file that ends at its closing `---` has no body line, not even the empty
|
|
149
|
+
// one a final newline leaves behind.
|
|
150
|
+
if (parts.body !== "" || text.endsWith("\n"))
|
|
151
|
+
next.push(...rendered);
|
|
152
|
+
return { ok: true, text: next.join("\n") };
|
|
124
153
|
}
|
|
125
154
|
function addProblem(context, file, message, fix) {
|
|
126
155
|
context.problems.push({ level: "error", file, line: 1, message, fix });
|
|
@@ -129,41 +158,19 @@ function problemFile(context, relative) {
|
|
|
129
158
|
return `${context.sourcePath}/${relative}`;
|
|
130
159
|
}
|
|
131
160
|
async function addFile(target, relative, context, mode) {
|
|
132
|
-
let data;
|
|
133
161
|
let fileMode;
|
|
134
162
|
try {
|
|
135
|
-
data = await readFile(target);
|
|
136
163
|
fileMode = mode ?? (await stat(target)).mode;
|
|
137
164
|
}
|
|
138
165
|
catch {
|
|
139
166
|
addProblem(context, problemFile(context, relative), `Cannot read file ${problemFile(context, relative)}`, "Check its permissions or restore the file");
|
|
140
167
|
return;
|
|
141
168
|
}
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
if (decoded !== undefined) {
|
|
146
|
-
text = toLF(decoded);
|
|
147
|
-
content = Buffer.from(text, "utf8");
|
|
148
|
-
}
|
|
149
|
-
if (relative === "SKILL.md") {
|
|
150
|
-
if (text === undefined) {
|
|
151
|
-
addProblem(context, problemFile(context, relative), "SKILL.md is binary", "Replace it with a UTF-8 text file");
|
|
152
|
-
return;
|
|
153
|
-
}
|
|
154
|
-
const inserted = insertComment(text, context.comment);
|
|
155
|
-
if (!inserted.ok) {
|
|
156
|
-
addProblem(context, problemFile(context, relative), inserted.message, inserted.fix);
|
|
157
|
-
return;
|
|
158
|
-
}
|
|
159
|
-
content = Buffer.from(inserted.text, "utf8");
|
|
160
|
-
}
|
|
161
|
-
const file = {
|
|
162
|
-
path: relative,
|
|
163
|
-
content,
|
|
169
|
+
context.files.push({
|
|
170
|
+
relative,
|
|
171
|
+
target,
|
|
164
172
|
mode: (fileMode & 0o111) !== 0 ? 0o755 : 0o644,
|
|
165
|
-
};
|
|
166
|
-
context.files.push(file);
|
|
173
|
+
});
|
|
167
174
|
}
|
|
168
175
|
async function addSymlink(target, relative, context, ancestors) {
|
|
169
176
|
let resolved;
|
|
@@ -251,25 +258,97 @@ async function walk(directory, prefix, context, ancestors) {
|
|
|
251
258
|
}
|
|
252
259
|
}
|
|
253
260
|
}
|
|
254
|
-
|
|
261
|
+
/**
|
|
262
|
+
* Lists a skill's source files as `build` installs them, with the symlink
|
|
263
|
+
* containment and loop rules of the walk. `compileSkill` reads the files it
|
|
264
|
+
* returns; the environment validation walks the same list to check every
|
|
265
|
+
* Markdown file `build` would render.
|
|
266
|
+
*/
|
|
267
|
+
export async function collectSkillFiles(source, options) {
|
|
255
268
|
const context = {
|
|
256
269
|
factoryDir: options.factoryDir,
|
|
257
270
|
linkRoots: options.linkRoots ?? [],
|
|
258
|
-
comment: options.comment ?? generatedComment(source.sourcePath),
|
|
259
271
|
sourcePath: source.sourcePath,
|
|
260
272
|
files: [],
|
|
261
273
|
problems: [],
|
|
262
274
|
};
|
|
263
275
|
await walk(source.dir, "", context, new Set());
|
|
264
|
-
|
|
265
|
-
|
|
276
|
+
context.files.sort((left, right) => left.relative < right.relative ? -1 : left.relative > right.relative ? 1 : 0);
|
|
277
|
+
return { files: context.files, problems: context.problems };
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* Reads one collected file into its installed content: text is written as LF,
|
|
281
|
+
* Markdown is rendered for the selection, and `SKILL.md` gains the generated
|
|
282
|
+
* comment after its frontmatter. A file that cannot be read, or a `SKILL.md`
|
|
283
|
+
* that is binary or has no frontmatter, reports its problem and is left out.
|
|
284
|
+
*/
|
|
285
|
+
async function compiledFile(file, displayPath, comment, options, problems) {
|
|
286
|
+
const error = (message, fix) => {
|
|
287
|
+
problems.push({ level: "error", file: displayPath, line: 1, message, fix });
|
|
288
|
+
return undefined;
|
|
289
|
+
};
|
|
290
|
+
let data;
|
|
291
|
+
try {
|
|
292
|
+
data = await readFile(file.target);
|
|
293
|
+
}
|
|
294
|
+
catch {
|
|
295
|
+
return error(`Cannot read file ${displayPath}`, "Check its permissions or restore the file");
|
|
296
|
+
}
|
|
297
|
+
const decoded = textContent(data);
|
|
298
|
+
if (decoded === undefined) {
|
|
299
|
+
if (file.relative === "SKILL.md") {
|
|
300
|
+
return error("SKILL.md is binary", "Replace it with a UTF-8 text file");
|
|
301
|
+
}
|
|
302
|
+
return { path: file.relative, content: data, mode: file.mode };
|
|
303
|
+
}
|
|
304
|
+
const text = toLF(decoded);
|
|
305
|
+
if (file.relative !== "SKILL.md") {
|
|
306
|
+
// Only a Markdown file with a renderer is split and rendered. Every other
|
|
307
|
+
// file is written as read, byte for byte, so format 1 output and the
|
|
308
|
+
// built-in skill never change.
|
|
309
|
+
if (options.render === undefined || !isMarkdown(file.relative)) {
|
|
310
|
+
return { path: file.relative, content: Buffer.from(text, "utf8"), mode: file.mode };
|
|
311
|
+
}
|
|
312
|
+
// The frontmatter is kept byte for byte and only the body is rendered, the
|
|
313
|
+
// same split validation parses, so a fence in frontmatter cannot hide a
|
|
314
|
+
// directive in the body.
|
|
315
|
+
const parts = markdownParts(text);
|
|
316
|
+
const rendered = await renderMarkdown(file.relative, parts.body, parts.firstLine, options);
|
|
317
|
+
return {
|
|
318
|
+
path: file.relative,
|
|
319
|
+
content: Buffer.from(`${parts.prefix}${rendered}`, "utf8"),
|
|
320
|
+
mode: file.mode,
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
const inserted = await insertComment(text, comment, options);
|
|
324
|
+
if (!inserted.ok)
|
|
325
|
+
return error(inserted.message, inserted.fix);
|
|
326
|
+
return { path: file.relative, content: Buffer.from(inserted.text, "utf8"), mode: file.mode };
|
|
327
|
+
}
|
|
328
|
+
export async function compileSkill(source, options) {
|
|
329
|
+
const collected = await collectSkillFiles(source, options);
|
|
330
|
+
const problems = [...collected.problems];
|
|
331
|
+
const files = [];
|
|
332
|
+
const comment = options.comment ?? generatedComment(source.sourcePath);
|
|
333
|
+
for (const file of collected.files) {
|
|
334
|
+
const compiled = await compiledFile(file, `${source.sourcePath}/${file.relative}`, comment, options, problems);
|
|
335
|
+
if (compiled !== undefined)
|
|
336
|
+
files.push(compiled);
|
|
337
|
+
}
|
|
338
|
+
if (!files.some((file) => file.path === "SKILL.md") && problems.length === 0) {
|
|
339
|
+
problems.push({
|
|
340
|
+
level: "error",
|
|
341
|
+
file: `${source.sourcePath}/SKILL.md`,
|
|
342
|
+
line: 1,
|
|
343
|
+
message: `Missing SKILL.md in ${source.sourcePath}`,
|
|
344
|
+
fix: "Add a SKILL.md with name and description frontmatter",
|
|
345
|
+
});
|
|
266
346
|
}
|
|
267
|
-
context.files.sort((left, right) => left.path < right.path ? -1 : left.path > right.path ? 1 : 0);
|
|
268
347
|
return {
|
|
269
348
|
name: source.name,
|
|
270
349
|
sourcePath: source.sourcePath,
|
|
271
|
-
files
|
|
272
|
-
problems
|
|
350
|
+
files,
|
|
351
|
+
problems,
|
|
273
352
|
};
|
|
274
353
|
}
|
|
275
354
|
//# sourceMappingURL=skill.js.map
|