@seasb/metafab 0.0.1 → 1.0.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 +46 -1
- package/dist/cli.d.ts +11 -0
- package/dist/cli.js +317 -0
- package/dist/cli.js.map +1 -0
- package/dist/commands/build.d.ts +2 -0
- package/dist/commands/build.js +70 -0
- package/dist/commands/build.js.map +1 -0
- package/dist/commands/check.d.ts +2 -0
- package/dist/commands/check.js +41 -0
- package/dist/commands/check.js.map +1 -0
- package/dist/commands/create.d.ts +2 -0
- package/dist/commands/create.js +74 -0
- package/dist/commands/create.js.map +1 -0
- package/dist/commands/detach.d.ts +2 -0
- package/dist/commands/detach.js +24 -0
- package/dist/commands/detach.js.map +1 -0
- package/dist/commands/diff.d.ts +2 -0
- package/dist/commands/diff.js +35 -0
- package/dist/commands/diff.js.map +1 -0
- package/dist/commands/import.d.ts +2 -0
- package/dist/commands/import.js +107 -0
- package/dist/commands/import.js.map +1 -0
- package/dist/commands/init.d.ts +2 -0
- package/dist/commands/init.js +77 -0
- package/dist/commands/init.js.map +1 -0
- package/dist/commands/map.d.ts +2 -0
- package/dist/commands/map.js +43 -0
- package/dist/commands/map.js.map +1 -0
- package/dist/commands/not-implemented.d.ts +2 -0
- package/dist/commands/not-implemented.js +19 -0
- package/dist/commands/not-implemented.js.map +1 -0
- package/dist/commands/pipeline-add.d.ts +2 -0
- package/dist/commands/pipeline-add.js +67 -0
- package/dist/commands/pipeline-add.js.map +1 -0
- package/dist/commands/pipeline-remove.d.ts +2 -0
- package/dist/commands/pipeline-remove.js +44 -0
- package/dist/commands/pipeline-remove.js.map +1 -0
- package/dist/commands/publish.d.ts +2 -0
- package/dist/commands/publish.js +147 -0
- package/dist/commands/publish.js.map +1 -0
- package/dist/commands/types.d.ts +45 -0
- package/dist/commands/types.js +2 -0
- package/dist/commands/types.js.map +1 -0
- package/dist/commands/update.d.ts +2 -0
- package/dist/commands/update.js +97 -0
- package/dist/commands/update.js.map +1 -0
- package/dist/core/adopt.d.ts +53 -0
- package/dist/core/adopt.js +676 -0
- package/dist/core/adopt.js.map +1 -0
- package/dist/core/build/agents.d.ts +23 -0
- package/dist/core/build/agents.js +60 -0
- package/dist/core/build/agents.js.map +1 -0
- package/dist/core/build/index.d.ts +4 -0
- package/dist/core/build/index.js +5 -0
- package/dist/core/build/index.js.map +1 -0
- package/dist/core/build/markers.d.ts +34 -0
- package/dist/core/build/markers.js +97 -0
- package/dist/core/build/markers.js.map +1 -0
- package/dist/core/build/plan.d.ts +48 -0
- package/dist/core/build/plan.js +792 -0
- package/dist/core/build/plan.js.map +1 -0
- package/dist/core/build/skill.d.ts +60 -0
- package/dist/core/build/skill.js +275 -0
- package/dist/core/build/skill.js.map +1 -0
- package/dist/core/check/drift.d.ts +13 -0
- package/dist/core/check/drift.js +205 -0
- package/dist/core/check/drift.js.map +1 -0
- package/dist/core/check/index.d.ts +12 -0
- package/dist/core/check/index.js +78 -0
- package/dist/core/check/index.js.map +1 -0
- package/dist/core/check/markers.d.ts +9 -0
- package/dist/core/check/markers.js +220 -0
- package/dist/core/check/markers.js.map +1 -0
- package/dist/core/config/destinations.d.ts +5 -0
- package/dist/core/config/destinations.js +20 -0
- package/dist/core/config/destinations.js.map +1 -0
- package/dist/core/config/load.d.ts +4 -0
- package/dist/core/config/load.js +41 -0
- package/dist/core/config/load.js.map +1 -0
- package/dist/core/config/parse.d.ts +8 -0
- package/dist/core/config/parse.js +198 -0
- package/dist/core/config/parse.js.map +1 -0
- package/dist/core/config/source.d.ts +14 -0
- package/dist/core/config/source.js +44 -0
- package/dist/core/config/source.js.map +1 -0
- package/dist/core/config/types.d.ts +48 -0
- package/dist/core/config/types.js +2 -0
- package/dist/core/config/types.js.map +1 -0
- package/dist/core/config/validate.d.ts +11 -0
- package/dist/core/config/validate.js +351 -0
- package/dist/core/config/validate.js.map +1 -0
- package/dist/core/config/writers.d.ts +14 -0
- package/dist/core/config/writers.js +261 -0
- package/dist/core/config/writers.js.map +1 -0
- package/dist/core/config.d.ts +7 -0
- package/dist/core/config.js +8 -0
- package/dist/core/config.js.map +1 -0
- package/dist/core/discovery.d.ts +34 -0
- package/dist/core/discovery.js +272 -0
- package/dist/core/discovery.js.map +1 -0
- package/dist/core/edit/create.d.ts +18 -0
- package/dist/core/edit/create.js +190 -0
- package/dist/core/edit/create.js.map +1 -0
- package/dist/core/edit/document.d.ts +4 -0
- package/dist/core/edit/document.js +16 -0
- package/dist/core/edit/document.js.map +1 -0
- package/dist/core/edit/factory-yaml.d.ts +17 -0
- package/dist/core/edit/factory-yaml.js +211 -0
- package/dist/core/edit/factory-yaml.js.map +1 -0
- package/dist/core/edit/files.d.ts +6 -0
- package/dist/core/edit/files.js +47 -0
- package/dist/core/edit/files.js.map +1 -0
- package/dist/core/edit/index.d.ts +9 -0
- package/dist/core/edit/index.js +10 -0
- package/dist/core/edit/index.js.map +1 -0
- package/dist/core/edit/map.d.ts +24 -0
- package/dist/core/edit/map.js +82 -0
- package/dist/core/edit/map.js.map +1 -0
- package/dist/core/edit/names.d.ts +2 -0
- package/dist/core/edit/names.js +17 -0
- package/dist/core/edit/names.js.map +1 -0
- package/dist/core/edit/paths.d.ts +8 -0
- package/dist/core/edit/paths.js +76 -0
- package/dist/core/edit/paths.js.map +1 -0
- package/dist/core/edit/pipeline.d.ts +19 -0
- package/dist/core/edit/pipeline.js +146 -0
- package/dist/core/edit/pipeline.js.map +1 -0
- package/dist/core/edit/skill.d.ts +1 -0
- package/dist/core/edit/skill.js +10 -0
- package/dist/core/edit/skill.js.map +1 -0
- package/dist/core/factory/discovery.d.ts +9 -0
- package/dist/core/factory/discovery.js +139 -0
- package/dist/core/factory/discovery.js.map +1 -0
- package/dist/core/factory/factory-yaml.d.ts +16 -0
- package/dist/core/factory/factory-yaml.js +375 -0
- package/dist/core/factory/factory-yaml.js.map +1 -0
- package/dist/core/factory/index.d.ts +5 -0
- package/dist/core/factory/index.js +6 -0
- package/dist/core/factory/index.js.map +1 -0
- package/dist/core/factory/lines.d.ts +8 -0
- package/dist/core/factory/lines.js +11 -0
- package/dist/core/factory/lines.js.map +1 -0
- package/dist/core/factory/load.d.ts +4 -0
- package/dist/core/factory/load.js +81 -0
- package/dist/core/factory/load.js.map +1 -0
- package/dist/core/factory/requires.d.ts +9 -0
- package/dist/core/factory/requires.js +123 -0
- package/dist/core/factory/requires.js.map +1 -0
- package/dist/core/factory/types.d.ts +45 -0
- package/dist/core/factory/types.js +2 -0
- package/dist/core/factory/types.js.map +1 -0
- package/dist/core/factory/validate.d.ts +41 -0
- package/dist/core/factory/validate.js +441 -0
- package/dist/core/factory/validate.js.map +1 -0
- package/dist/core/hooks/index.d.ts +8 -0
- package/dist/core/hooks/index.js +15 -0
- package/dist/core/hooks/index.js.map +1 -0
- package/dist/core/hooks/script.d.ts +21 -0
- package/dist/core/hooks/script.js +34 -0
- package/dist/core/hooks/script.js.map +1 -0
- package/dist/core/hooks/sync.d.ts +17 -0
- package/dist/core/hooks/sync.js +262 -0
- package/dist/core/hooks/sync.js.map +1 -0
- package/dist/core/import/conflicts.d.ts +82 -0
- package/dist/core/import/conflicts.js +256 -0
- package/dist/core/import/conflicts.js.map +1 -0
- package/dist/core/import/copy.d.ts +10 -0
- package/dist/core/import/copy.js +76 -0
- package/dist/core/import/copy.js.map +1 -0
- package/dist/core/import/file-tree.d.ts +43 -0
- package/dist/core/import/file-tree.js +107 -0
- package/dist/core/import/file-tree.js.map +1 -0
- package/dist/core/import/index.d.ts +5 -0
- package/dist/core/import/index.js +6 -0
- package/dist/core/import/index.js.map +1 -0
- package/dist/core/import/pipeline-changes.d.ts +8 -0
- package/dist/core/import/pipeline-changes.js +89 -0
- package/dist/core/import/pipeline-changes.js.map +1 -0
- package/dist/core/import/run.d.ts +76 -0
- package/dist/core/import/run.js +258 -0
- package/dist/core/import/run.js.map +1 -0
- package/dist/core/init/gitattributes.d.ts +20 -0
- package/dist/core/init/gitattributes.js +89 -0
- package/dist/core/init/gitattributes.js.map +1 -0
- package/dist/core/init/index.d.ts +4 -0
- package/dist/core/init/index.js +5 -0
- package/dist/core/init/index.js.map +1 -0
- package/dist/core/init/name.d.ts +1 -0
- package/dist/core/init/name.js +4 -0
- package/dist/core/init/name.js.map +1 -0
- package/dist/core/init/run.d.ts +51 -0
- package/dist/core/init/run.js +261 -0
- package/dist/core/init/run.js.map +1 -0
- package/dist/core/init/targets.d.ts +17 -0
- package/dist/core/init/targets.js +87 -0
- package/dist/core/init/targets.js.map +1 -0
- package/dist/core/merge.d.ts +88 -0
- package/dist/core/merge.js +239 -0
- package/dist/core/merge.js.map +1 -0
- package/dist/core/publish/exclude.d.ts +9 -0
- package/dist/core/publish/exclude.js +32 -0
- package/dist/core/publish/exclude.js.map +1 -0
- package/dist/core/publish/git-target.d.ts +32 -0
- package/dist/core/publish/git-target.js +447 -0
- package/dist/core/publish/git-target.js.map +1 -0
- package/dist/core/publish/index.d.ts +10 -0
- package/dist/core/publish/index.js +11 -0
- package/dist/core/publish/index.js.map +1 -0
- package/dist/core/publish/npm-target.d.ts +15 -0
- package/dist/core/publish/npm-target.js +170 -0
- package/dist/core/publish/npm-target.js.map +1 -0
- package/dist/core/publish/package-json.d.ts +31 -0
- package/dist/core/publish/package-json.js +74 -0
- package/dist/core/publish/package-json.js.map +1 -0
- package/dist/core/publish/prepare.d.ts +24 -0
- package/dist/core/publish/prepare.js +93 -0
- package/dist/core/publish/prepare.js.map +1 -0
- package/dist/core/publish/provenance.d.ts +1 -0
- package/dist/core/publish/provenance.js +20 -0
- package/dist/core/publish/provenance.js.map +1 -0
- package/dist/core/publish/publish.d.ts +13 -0
- package/dist/core/publish/publish.js +130 -0
- package/dist/core/publish/publish.js.map +1 -0
- package/dist/core/publish/readme.d.ts +22 -0
- package/dist/core/publish/readme.js +112 -0
- package/dist/core/publish/readme.js.map +1 -0
- package/dist/core/publish/stage.d.ts +19 -0
- package/dist/core/publish/stage.js +333 -0
- package/dist/core/publish/stage.js.map +1 -0
- package/dist/core/publish/types.d.ts +71 -0
- package/dist/core/publish/types.js +2 -0
- package/dist/core/publish/types.js.map +1 -0
- package/dist/core/requires/check.d.ts +15 -0
- package/dist/core/requires/check.js +83 -0
- package/dist/core/requires/check.js.map +1 -0
- package/dist/core/requires/index.d.ts +4 -0
- package/dist/core/requires/index.js +5 -0
- package/dist/core/requires/index.js.map +1 -0
- package/dist/core/requires/inspect.d.ts +44 -0
- package/dist/core/requires/inspect.js +114 -0
- package/dist/core/requires/inspect.js.map +1 -0
- package/dist/core/requires/paths.d.ts +29 -0
- package/dist/core/requires/paths.js +83 -0
- package/dist/core/requires/paths.js.map +1 -0
- package/dist/core/requires/setup.d.ts +37 -0
- package/dist/core/requires/setup.js +147 -0
- package/dist/core/requires/setup.js.map +1 -0
- package/dist/core/setup.d.ts +27 -0
- package/dist/core/setup.js +84 -0
- package/dist/core/setup.js.map +1 -0
- package/dist/core/sources/cache.d.ts +38 -0
- package/dist/core/sources/cache.js +200 -0
- package/dist/core/sources/cache.js.map +1 -0
- package/dist/core/sources/containment.d.ts +21 -0
- package/dist/core/sources/containment.js +50 -0
- package/dist/core/sources/containment.js.map +1 -0
- package/dist/core/sources/env.d.ts +12 -0
- package/dist/core/sources/env.js +28 -0
- package/dist/core/sources/env.js.map +1 -0
- package/dist/core/sources/extract.d.ts +25 -0
- package/dist/core/sources/extract.js +676 -0
- package/dist/core/sources/extract.js.map +1 -0
- package/dist/core/sources/git-process.d.ts +26 -0
- package/dist/core/sources/git-process.js +51 -0
- package/dist/core/sources/git-process.js.map +1 -0
- package/dist/core/sources/git.d.ts +41 -0
- package/dist/core/sources/git.js +511 -0
- package/dist/core/sources/git.js.map +1 -0
- package/dist/core/sources/https.d.ts +21 -0
- package/dist/core/sources/https.js +208 -0
- package/dist/core/sources/https.js.map +1 -0
- package/dist/core/sources/index.d.ts +13 -0
- package/dist/core/sources/index.js +14 -0
- package/dist/core/sources/index.js.map +1 -0
- package/dist/core/sources/npm-cli.d.ts +19 -0
- package/dist/core/sources/npm-cli.js +43 -0
- package/dist/core/sources/npm-cli.js.map +1 -0
- package/dist/core/sources/npm.d.ts +29 -0
- package/dist/core/sources/npm.js +386 -0
- package/dist/core/sources/npm.js.map +1 -0
- package/dist/core/sources/parse.d.ts +32 -0
- package/dist/core/sources/parse.js +389 -0
- package/dist/core/sources/parse.js.map +1 -0
- package/dist/core/sources/path.d.ts +10 -0
- package/dist/core/sources/path.js +316 -0
- package/dist/core/sources/path.js.map +1 -0
- package/dist/core/sources/redact.d.ts +2 -0
- package/dist/core/sources/redact.js +70 -0
- package/dist/core/sources/redact.js.map +1 -0
- package/dist/core/sources/resolve.d.ts +9 -0
- package/dist/core/sources/resolve.js +29 -0
- package/dist/core/sources/resolve.js.map +1 -0
- package/dist/core/sources/types.d.ts +86 -0
- package/dist/core/sources/types.js +2 -0
- package/dist/core/sources/types.js.map +1 -0
- package/dist/core/sources/validate.d.ts +9 -0
- package/dist/core/sources/validate.js +111 -0
- package/dist/core/sources/validate.js.map +1 -0
- package/dist/core/summary.d.ts +77 -0
- package/dist/core/summary.js +405 -0
- package/dist/core/summary.js.map +1 -0
- package/dist/core/update/clashes.d.ts +63 -0
- package/dist/core/update/clashes.js +192 -0
- package/dist/core/update/clashes.js.map +1 -0
- package/dist/core/update/detach.d.ts +21 -0
- package/dist/core/update/detach.js +38 -0
- package/dist/core/update/detach.js.map +1 -0
- package/dist/core/update/diff.d.ts +58 -0
- package/dist/core/update/diff.js +180 -0
- package/dist/core/update/diff.js.map +1 -0
- package/dist/core/update/fetch.d.ts +34 -0
- package/dist/core/update/fetch.js +73 -0
- package/dist/core/update/fetch.js.map +1 -0
- package/dist/core/update/index.d.ts +6 -0
- package/dist/core/update/index.js +7 -0
- package/dist/core/update/index.js.map +1 -0
- package/dist/core/update/restore.d.ts +20 -0
- package/dist/core/update/restore.js +195 -0
- package/dist/core/update/restore.js.map +1 -0
- package/dist/core/update/run.d.ts +113 -0
- package/dist/core/update/run.js +1132 -0
- package/dist/core/update/run.js.map +1 -0
- package/dist/core/update/select.d.ts +39 -0
- package/dist/core/update/select.js +157 -0
- package/dist/core/update/select.js.map +1 -0
- package/dist/core/update/spelling.d.ts +51 -0
- package/dist/core/update/spelling.js +185 -0
- package/dist/core/update/spelling.js.map +1 -0
- package/dist/core/update/trees.d.ts +24 -0
- package/dist/core/update/trees.js +72 -0
- package/dist/core/update/trees.js.map +1 -0
- package/dist/errors.d.ts +29 -0
- package/dist/errors.js +24 -0
- package/dist/errors.js.map +1 -0
- package/dist/help.d.ts +46 -0
- package/dist/help.js +103 -0
- package/dist/help.js.map +1 -0
- package/dist/input.d.ts +25 -0
- package/dist/input.js +173 -0
- package/dist/input.js.map +1 -0
- package/dist/output.d.ts +21 -0
- package/dist/output.js +37 -0
- package/dist/output.js.map +1 -0
- package/dist/util/frontmatter.d.ts +12 -0
- package/dist/util/frontmatter.js +58 -0
- package/dist/util/frontmatter.js.map +1 -0
- package/dist/util/fs.d.ts +32 -0
- package/dist/util/fs.js +126 -0
- package/dist/util/fs.js.map +1 -0
- package/dist/util/git-names.d.ts +8 -0
- package/dist/util/git-names.js +25 -0
- package/dist/util/git-names.js.map +1 -0
- package/dist/util/git.d.ts +12 -0
- package/dist/util/git.js +33 -0
- package/dist/util/git.js.map +1 -0
- package/dist/util/suggest.d.ts +6 -0
- package/dist/util/suggest.js +48 -0
- package/dist/util/suggest.js.map +1 -0
- package/dist/util/text.d.ts +8 -0
- package/dist/util/text.js +19 -0
- package/dist/util/text.js.map +1 -0
- package/dist/util/version.d.ts +7 -0
- package/dist/util/version.js +13 -0
- package/dist/util/version.js.map +1 -0
- package/dist/util/yaml.d.ts +30 -0
- package/dist/util/yaml.js +201 -0
- package/dist/util/yaml.js.map +1 -0
- package/package.json +50 -3
- package/resources/metafab-skill/SKILL.md +99 -0
|
@@ -0,0 +1,792 @@
|
|
|
1
|
+
import { chmod, lstat, mkdir, readFile, readlink, realpath, rm, stat, writeFile, } from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import { CLAUDE_SKILLS_DIRECTORY, AGENTS_SKILLS_DIRECTORY } from "../config/destinations.js";
|
|
5
|
+
import { destinations } from "../config/destinations.js";
|
|
6
|
+
import { readdirTyped } from "../../util/fs.js";
|
|
7
|
+
import { toLF } from "../../util/text.js";
|
|
8
|
+
import { sameFile } from "../sources/containment.js";
|
|
9
|
+
import { AGENTS_BLOCK_LINE_LIMIT, AGENTS_MARKERS, CLAUDE_MARKERS, GITIGNORE_MARKERS, agentsBlockLineCount, claudeBlockBody, gitignoreBlockBody, renderAgentsBlockBody, } from "./agents.js";
|
|
10
|
+
import { appendMarkerBlock, findMarkerBlock, removeMarkerBlock, replaceMarkerBlock, } from "./markers.js";
|
|
11
|
+
import { builtinComment, cannotReadProblem, compileSkill, isOwned, textContent, } from "./skill.js";
|
|
12
|
+
import { syncGitHooks } from "../hooks/index.js";
|
|
13
|
+
const SKILL_DESTINATIONS = [CLAUDE_SKILLS_DIRECTORY, AGENTS_SKILLS_DIRECTORY];
|
|
14
|
+
const BUILTIN_SKILL_DIR = fileURLToPath(new URL("../../../resources/metafab-skill", import.meta.url));
|
|
15
|
+
/** Installed name of the skill that `build` compiles from this package. */
|
|
16
|
+
const BUILTIN_SKILL_NAME = "metafab";
|
|
17
|
+
/**
|
|
18
|
+
* Skill names that `build` installs from the package itself. Adoption must not
|
|
19
|
+
* move a repository skill onto one of these names: the built-in copy would
|
|
20
|
+
* shadow or overwrite the moved skill.
|
|
21
|
+
*/
|
|
22
|
+
export const BUILTIN_SKILL_NAMES = [BUILTIN_SKILL_NAME];
|
|
23
|
+
const IMPORT_AGENTS = /^@(\.\/)?AGENTS\.md\s*$/;
|
|
24
|
+
function absolute(root, relativePath) {
|
|
25
|
+
return path.join(root, ...relativePath.split("/"));
|
|
26
|
+
}
|
|
27
|
+
async function lstatIfExists(target) {
|
|
28
|
+
try {
|
|
29
|
+
return await lstat(target);
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
if (error.code === "ENOENT")
|
|
33
|
+
return undefined;
|
|
34
|
+
throw error;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
function markerProblem(file, problem) {
|
|
38
|
+
return {
|
|
39
|
+
level: "error",
|
|
40
|
+
file,
|
|
41
|
+
line: problem.line,
|
|
42
|
+
message: problem.message,
|
|
43
|
+
fix: problem.fix,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
function planBlockFile(existing, markers, file, body, removeWhenEmpty = false) {
|
|
47
|
+
if (existing === undefined) {
|
|
48
|
+
if (body === undefined)
|
|
49
|
+
return {};
|
|
50
|
+
const appended = appendMarkerBlock("", markers, body);
|
|
51
|
+
if (!appended.ok)
|
|
52
|
+
return { error: markerProblem(file, appended.problem) };
|
|
53
|
+
return { text: appended.text };
|
|
54
|
+
}
|
|
55
|
+
const found = findMarkerBlock(existing, markers);
|
|
56
|
+
if (!found.ok)
|
|
57
|
+
return { error: markerProblem(file, found.problem) };
|
|
58
|
+
if (body !== undefined) {
|
|
59
|
+
const result = found.range === undefined
|
|
60
|
+
? appendMarkerBlock(existing, markers, body)
|
|
61
|
+
: replaceMarkerBlock(existing, markers, body);
|
|
62
|
+
if (!result.ok)
|
|
63
|
+
return { error: markerProblem(file, result.problem) };
|
|
64
|
+
return result.changed ? { text: result.text } : {};
|
|
65
|
+
}
|
|
66
|
+
if (found.range === undefined)
|
|
67
|
+
return {};
|
|
68
|
+
const removed = removeMarkerBlock(existing, markers);
|
|
69
|
+
if (!removed.ok)
|
|
70
|
+
return { error: markerProblem(file, removed.problem) };
|
|
71
|
+
if (!removed.changed)
|
|
72
|
+
return {};
|
|
73
|
+
if (removed.text === "" && removeWhenEmpty)
|
|
74
|
+
return { removeFile: true };
|
|
75
|
+
return { text: removed.text };
|
|
76
|
+
}
|
|
77
|
+
function addBlockPlan(plan, file, entry, result, removals) {
|
|
78
|
+
if (result.error === undefined && result.text === undefined && result.removeFile !== true) {
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
const conflict = rootEntryConflict(file, entry.kind);
|
|
82
|
+
if (conflict !== undefined) {
|
|
83
|
+
plan.conflicts.push(conflict);
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
if (result.error !== undefined) {
|
|
87
|
+
plan.errors.push(result.error);
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
if (result.text !== undefined) {
|
|
91
|
+
plan.writes.push({ path: file, content: Buffer.from(result.text, "utf8") });
|
|
92
|
+
}
|
|
93
|
+
if (result.removeFile === true)
|
|
94
|
+
removals.add(file);
|
|
95
|
+
}
|
|
96
|
+
function linesOutsideRange(text, range) {
|
|
97
|
+
const lines = toLF(text).split("\n");
|
|
98
|
+
if (range === undefined)
|
|
99
|
+
return lines;
|
|
100
|
+
return [...lines.slice(0, range.start), ...lines.slice(range.end + 1)];
|
|
101
|
+
}
|
|
102
|
+
function importsAgents(text, range) {
|
|
103
|
+
return linesOutsideRange(text, range).some((line) => IMPORT_AGENTS.test(line));
|
|
104
|
+
}
|
|
105
|
+
function claudeBlock() {
|
|
106
|
+
return [CLAUDE_MARKERS.start, claudeBlockBody(), CLAUDE_MARKERS.end].join("\n");
|
|
107
|
+
}
|
|
108
|
+
function skillWrites(destination, skill) {
|
|
109
|
+
return skill.files.map((file) => ({
|
|
110
|
+
path: `${destination}/${skill.name}/${file.path}`,
|
|
111
|
+
content: file.content,
|
|
112
|
+
mode: file.mode,
|
|
113
|
+
}));
|
|
114
|
+
}
|
|
115
|
+
async function compileFactorySkills(factory, errors) {
|
|
116
|
+
const desired = [];
|
|
117
|
+
for (const skill of factory.skills) {
|
|
118
|
+
const compiled = await compileSkill({
|
|
119
|
+
dir: path.join(factory.dir, ...skill.relDir.split("/")),
|
|
120
|
+
name: skill.name,
|
|
121
|
+
sourcePath: `.factory/${skill.relDir}`,
|
|
122
|
+
}, { factoryDir: factory.dir });
|
|
123
|
+
if (compiled.problems.length > 0)
|
|
124
|
+
errors.push(...compiled.problems);
|
|
125
|
+
desired.push({
|
|
126
|
+
name: compiled.name,
|
|
127
|
+
files: compiled.files.map((file) => ({
|
|
128
|
+
path: file.path,
|
|
129
|
+
content: file.content,
|
|
130
|
+
mode: file.mode,
|
|
131
|
+
})),
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
return desired;
|
|
135
|
+
}
|
|
136
|
+
async function compileBuiltinSkill(config, cliVersion, errors) {
|
|
137
|
+
if (!config.metafabSkill)
|
|
138
|
+
return undefined;
|
|
139
|
+
const compiled = await compileSkill({ dir: BUILTIN_SKILL_DIR, name: BUILTIN_SKILL_NAME, sourcePath: "resources/metafab-skill" }, { factoryDir: BUILTIN_SKILL_DIR, comment: builtinComment(cliVersion) });
|
|
140
|
+
if (compiled.problems.length > 0)
|
|
141
|
+
errors.push(...compiled.problems);
|
|
142
|
+
return {
|
|
143
|
+
name: BUILTIN_SKILL_NAME,
|
|
144
|
+
files: compiled.files.map((file) => ({
|
|
145
|
+
path: file.path,
|
|
146
|
+
content: file.content,
|
|
147
|
+
mode: file.mode,
|
|
148
|
+
})),
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
function permissionProblem(relativePath, error) {
|
|
152
|
+
const code = error.code;
|
|
153
|
+
if (code !== "EACCES" && code !== "EPERM")
|
|
154
|
+
return undefined;
|
|
155
|
+
return cannotReadProblem(relativePath);
|
|
156
|
+
}
|
|
157
|
+
function dedupeErrors(errors) {
|
|
158
|
+
const seen = new Set();
|
|
159
|
+
return errors.filter((problem) => {
|
|
160
|
+
const key = `${problem.file}\u0000${problem.message}`;
|
|
161
|
+
if (seen.has(key))
|
|
162
|
+
return false;
|
|
163
|
+
seen.add(key);
|
|
164
|
+
return true;
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
async function readDirectoryIfExists(directory, relativePath, errors) {
|
|
168
|
+
try {
|
|
169
|
+
return await readdirTyped(directory);
|
|
170
|
+
}
|
|
171
|
+
catch (error) {
|
|
172
|
+
const code = error.code;
|
|
173
|
+
if (code === "ENOENT" || code === "ENOTDIR")
|
|
174
|
+
return undefined;
|
|
175
|
+
const problem = permissionProblem(relativePath, error);
|
|
176
|
+
if (problem === undefined)
|
|
177
|
+
throw error;
|
|
178
|
+
errors?.push(problem);
|
|
179
|
+
return undefined;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
function directoryChainProblem(relativePath, info) {
|
|
183
|
+
if (info.isSymbolicLink()) {
|
|
184
|
+
return {
|
|
185
|
+
level: "error",
|
|
186
|
+
file: relativePath,
|
|
187
|
+
message: `${relativePath} is a symbolic link, not a directory`,
|
|
188
|
+
fix: "Remove the symbolic link, then run metafab build again",
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
if (!info.isDirectory()) {
|
|
192
|
+
return {
|
|
193
|
+
level: "error",
|
|
194
|
+
file: relativePath,
|
|
195
|
+
message: `${relativePath} is a file, not a directory`,
|
|
196
|
+
fix: "Move the file or rename it, then run metafab build again",
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
return undefined;
|
|
200
|
+
}
|
|
201
|
+
async function inspectDestinationChain(root, directory) {
|
|
202
|
+
let current = "";
|
|
203
|
+
let previous;
|
|
204
|
+
for (const part of directory.split("/")) {
|
|
205
|
+
previous = current === "" ? undefined : current;
|
|
206
|
+
current = current === "" ? part : `${current}/${part}`;
|
|
207
|
+
let info;
|
|
208
|
+
try {
|
|
209
|
+
info = await lstatIfExists(absolute(root, current));
|
|
210
|
+
}
|
|
211
|
+
catch (error) {
|
|
212
|
+
// An lstat on a child fails with EACCES when a component above it cannot be
|
|
213
|
+
// searched, so name the component that actually blocks the traversal.
|
|
214
|
+
const code = error.code;
|
|
215
|
+
const blocked = (code === "EACCES" || code === "EPERM") && previous !== undefined ? previous : current;
|
|
216
|
+
return { traversable: false, readError: cannotReadProblem(blocked) };
|
|
217
|
+
}
|
|
218
|
+
if (info === undefined)
|
|
219
|
+
return { traversable: true };
|
|
220
|
+
const problem = directoryChainProblem(current, info);
|
|
221
|
+
if (problem !== undefined) {
|
|
222
|
+
return { traversable: false, symlink: info.isSymbolicLink(), problem };
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
return { traversable: true };
|
|
226
|
+
}
|
|
227
|
+
async function readRootEntry(root, relativePath) {
|
|
228
|
+
const target = absolute(root, relativePath);
|
|
229
|
+
const info = await lstatIfExists(target);
|
|
230
|
+
if (info === undefined)
|
|
231
|
+
return { kind: "absent" };
|
|
232
|
+
if (info.isSymbolicLink()) {
|
|
233
|
+
try {
|
|
234
|
+
return { kind: "symlink", text: await readFile(target, "utf8") };
|
|
235
|
+
}
|
|
236
|
+
catch (error) {
|
|
237
|
+
const code = error.code;
|
|
238
|
+
if (code === "ENOENT" || code === "EISDIR" || code === "ENOTDIR") {
|
|
239
|
+
return { kind: "symlink" };
|
|
240
|
+
}
|
|
241
|
+
return { kind: "symlink", readError: true };
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
if (info.isFile()) {
|
|
245
|
+
try {
|
|
246
|
+
return { kind: "file", text: await readFile(target, "utf8") };
|
|
247
|
+
}
|
|
248
|
+
catch (error) {
|
|
249
|
+
const problem = permissionProblem(relativePath, error);
|
|
250
|
+
if (problem === undefined)
|
|
251
|
+
throw error;
|
|
252
|
+
return { kind: "file", problem };
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
return { kind: "other" };
|
|
256
|
+
}
|
|
257
|
+
function symlinkReadConflict(relativePath) {
|
|
258
|
+
return {
|
|
259
|
+
level: "error",
|
|
260
|
+
file: relativePath,
|
|
261
|
+
message: `${relativePath} is a symbolic link that metafab build cannot read`,
|
|
262
|
+
fix: "Remove the symbolic link or replace it with a real file, then run metafab build again",
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
function rootEntryConflict(relativePath, kind) {
|
|
266
|
+
if (kind === "symlink") {
|
|
267
|
+
return {
|
|
268
|
+
level: "error",
|
|
269
|
+
file: relativePath,
|
|
270
|
+
message: `${relativePath} is a symbolic link, not a regular file`,
|
|
271
|
+
fix: "Remove the symbolic link, then run metafab build again",
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
if (kind === "other") {
|
|
275
|
+
return {
|
|
276
|
+
level: "error",
|
|
277
|
+
file: relativePath,
|
|
278
|
+
message: `${relativePath} is not a regular file`,
|
|
279
|
+
fix: "Move it or rename it, then run metafab build again",
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
return undefined;
|
|
283
|
+
}
|
|
284
|
+
async function claudeLinksToRootAgents(root, entry) {
|
|
285
|
+
if (entry.kind !== "symlink")
|
|
286
|
+
return false;
|
|
287
|
+
const claudePath = absolute(root, "CLAUDE.md");
|
|
288
|
+
const agentsPath = absolute(root, "AGENTS.md");
|
|
289
|
+
let rootReal = root;
|
|
290
|
+
try {
|
|
291
|
+
rootReal = await realpath(root);
|
|
292
|
+
}
|
|
293
|
+
catch {
|
|
294
|
+
// Keep the lexical root; the comparisons below still work on a normal checkout.
|
|
295
|
+
}
|
|
296
|
+
try {
|
|
297
|
+
const [claudeStat, agentsStat] = await Promise.all([
|
|
298
|
+
stat(claudePath, { bigint: true }),
|
|
299
|
+
stat(agentsPath, { bigint: true }),
|
|
300
|
+
]);
|
|
301
|
+
if (sameFile(claudeStat, agentsStat))
|
|
302
|
+
return true;
|
|
303
|
+
}
|
|
304
|
+
catch {
|
|
305
|
+
// Either side is missing; the link may still dangle towards AGENTS.md.
|
|
306
|
+
}
|
|
307
|
+
try {
|
|
308
|
+
const [claudeReal, agentsReal] = await Promise.all([realpath(claudePath), realpath(agentsPath)]);
|
|
309
|
+
if (claudeReal === agentsReal)
|
|
310
|
+
return true;
|
|
311
|
+
}
|
|
312
|
+
catch {
|
|
313
|
+
// Fall through to the link target.
|
|
314
|
+
}
|
|
315
|
+
let resolved;
|
|
316
|
+
try {
|
|
317
|
+
const link = await readlink(claudePath);
|
|
318
|
+
resolved = path.resolve(rootReal, link);
|
|
319
|
+
}
|
|
320
|
+
catch {
|
|
321
|
+
return false;
|
|
322
|
+
}
|
|
323
|
+
try {
|
|
324
|
+
if ((await lstatIfExists(resolved)) !== undefined) {
|
|
325
|
+
return resolved === agentsPath;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
catch {
|
|
329
|
+
return false;
|
|
330
|
+
}
|
|
331
|
+
try {
|
|
332
|
+
const parentReal = await realpath(path.dirname(resolved));
|
|
333
|
+
return path.join(parentReal, path.basename(resolved)) === path.join(rootReal, "AGENTS.md");
|
|
334
|
+
}
|
|
335
|
+
catch {
|
|
336
|
+
return false;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
async function symlinkedEntry(relativePath, target) {
|
|
340
|
+
let directory = false;
|
|
341
|
+
try {
|
|
342
|
+
directory = (await stat(target)).isDirectory();
|
|
343
|
+
}
|
|
344
|
+
catch {
|
|
345
|
+
directory = false;
|
|
346
|
+
}
|
|
347
|
+
return {
|
|
348
|
+
level: "error",
|
|
349
|
+
file: relativePath,
|
|
350
|
+
message: directory
|
|
351
|
+
? `${relativePath} is a symbolic link, not a directory`
|
|
352
|
+
: `${relativePath} is a symbolic link, not a regular file`,
|
|
353
|
+
fix: directory
|
|
354
|
+
? "Remove the symbolic link or replace it with a real directory, then run metafab build again"
|
|
355
|
+
: "Remove the symbolic link or replace it with a real file, then run metafab build again",
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
function desiredFileTree(skill) {
|
|
359
|
+
const files = new Set();
|
|
360
|
+
const directories = new Set();
|
|
361
|
+
for (const file of skill.files) {
|
|
362
|
+
files.add(file.path);
|
|
363
|
+
let directory = path.posix.dirname(file.path);
|
|
364
|
+
while (directory !== "." && directory !== "") {
|
|
365
|
+
directories.add(directory);
|
|
366
|
+
directory = path.posix.dirname(directory);
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
return { files, directories };
|
|
370
|
+
}
|
|
371
|
+
function unownedEntry(relativePath, directory) {
|
|
372
|
+
return {
|
|
373
|
+
level: "error",
|
|
374
|
+
file: relativePath,
|
|
375
|
+
message: directory
|
|
376
|
+
? `Unowned directory ${relativePath} is in the way`
|
|
377
|
+
: `Unowned file ${relativePath} is in the way`,
|
|
378
|
+
fix: "Move it into .factory/skills/, or run metafab init to adopt it",
|
|
379
|
+
};
|
|
380
|
+
}
|
|
381
|
+
async function reconcileOwnedDirectory(target, installedPath, skillPath, desired, removals, errors) {
|
|
382
|
+
const entries = (await readDirectoryIfExists(target, installedPath, errors)) ?? [];
|
|
383
|
+
entries.sort((left, right) => (left.name < right.name ? -1 : left.name > right.name ? 1 : 0));
|
|
384
|
+
for (const entry of entries) {
|
|
385
|
+
const childInstalled = `${installedPath}/${entry.name}`;
|
|
386
|
+
const childSkill = skillPath === "" ? entry.name : `${skillPath}/${entry.name}`;
|
|
387
|
+
if (desired.files.has(childSkill)) {
|
|
388
|
+
if (!entry.isFile())
|
|
389
|
+
removals.add(childInstalled);
|
|
390
|
+
continue;
|
|
391
|
+
}
|
|
392
|
+
if (desired.directories.has(childSkill)) {
|
|
393
|
+
if (entry.isDirectory()) {
|
|
394
|
+
await reconcileOwnedDirectory(path.join(target, entry.name), childInstalled, childSkill, desired, removals, errors);
|
|
395
|
+
}
|
|
396
|
+
else {
|
|
397
|
+
removals.add(childInstalled);
|
|
398
|
+
}
|
|
399
|
+
continue;
|
|
400
|
+
}
|
|
401
|
+
removals.add(childInstalled);
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
async function scanDestination(context, directory) {
|
|
405
|
+
const target = absolute(context.root, directory);
|
|
406
|
+
const info = await lstatIfExists(target);
|
|
407
|
+
if (info === undefined)
|
|
408
|
+
return;
|
|
409
|
+
if (!info.isDirectory()) {
|
|
410
|
+
if (context.selected) {
|
|
411
|
+
const symlink = info.isSymbolicLink();
|
|
412
|
+
context.conflicts.push({
|
|
413
|
+
level: "error",
|
|
414
|
+
file: directory,
|
|
415
|
+
message: symlink
|
|
416
|
+
? `${directory} is a symbolic link, not a directory`
|
|
417
|
+
: `${directory} is a file, not a directory`,
|
|
418
|
+
fix: symlink
|
|
419
|
+
? "Remove the symbolic link, then run metafab build again"
|
|
420
|
+
: "Move the file or rename it, then run metafab build again",
|
|
421
|
+
});
|
|
422
|
+
}
|
|
423
|
+
return;
|
|
424
|
+
}
|
|
425
|
+
const entries = (await readDirectoryIfExists(target, directory, context.errors)) ?? [];
|
|
426
|
+
entries.sort((left, right) => (left.name < right.name ? -1 : left.name > right.name ? 1 : 0));
|
|
427
|
+
for (const entry of entries) {
|
|
428
|
+
const relativePath = `${directory}/${entry.name}`;
|
|
429
|
+
if (entry.isSymbolicLink()) {
|
|
430
|
+
if (context.selected) {
|
|
431
|
+
context.conflicts.push(await symlinkedEntry(relativePath, absolute(context.root, relativePath)));
|
|
432
|
+
}
|
|
433
|
+
continue;
|
|
434
|
+
}
|
|
435
|
+
const ownedResult = entry.isDirectory()
|
|
436
|
+
? await isOwned(absolute(context.root, relativePath), relativePath)
|
|
437
|
+
: false;
|
|
438
|
+
if (typeof ownedResult !== "boolean") {
|
|
439
|
+
context.errors.push(ownedResult);
|
|
440
|
+
continue;
|
|
441
|
+
}
|
|
442
|
+
const owned = ownedResult;
|
|
443
|
+
if (context.selected) {
|
|
444
|
+
if (!owned) {
|
|
445
|
+
context.conflicts.push(unownedEntry(relativePath, entry.isDirectory()));
|
|
446
|
+
continue;
|
|
447
|
+
}
|
|
448
|
+
const desired = context.desired.get(entry.name);
|
|
449
|
+
if (desired === undefined) {
|
|
450
|
+
context.removals.add(relativePath);
|
|
451
|
+
continue;
|
|
452
|
+
}
|
|
453
|
+
await reconcileOwnedDirectory(absolute(context.root, relativePath), relativePath, "", desired, context.removals, context.errors);
|
|
454
|
+
continue;
|
|
455
|
+
}
|
|
456
|
+
if (owned)
|
|
457
|
+
context.removals.add(relativePath);
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
async function deselectedSymlinkConflict(root, directory, linkPath) {
|
|
461
|
+
const target = absolute(root, directory);
|
|
462
|
+
let entries;
|
|
463
|
+
try {
|
|
464
|
+
entries = await readdirTyped(target);
|
|
465
|
+
}
|
|
466
|
+
catch (error) {
|
|
467
|
+
const code = error.code;
|
|
468
|
+
if (code === "ENOENT" || code === "ENOTDIR")
|
|
469
|
+
return undefined;
|
|
470
|
+
return symlinkReadConflict(linkPath);
|
|
471
|
+
}
|
|
472
|
+
for (const entry of entries) {
|
|
473
|
+
if (!entry.isDirectory())
|
|
474
|
+
continue;
|
|
475
|
+
const relativePath = `${directory}/${entry.name}`;
|
|
476
|
+
const owned = await isOwned(path.join(target, entry.name), relativePath);
|
|
477
|
+
if (typeof owned !== "boolean")
|
|
478
|
+
return { error: owned };
|
|
479
|
+
if (!owned)
|
|
480
|
+
continue;
|
|
481
|
+
return {
|
|
482
|
+
level: "error",
|
|
483
|
+
file: linkPath,
|
|
484
|
+
message: `${linkPath} is a symbolic link, and ${directory} behind it holds owned output that metafab build cannot remove`,
|
|
485
|
+
fix: "Remove the owned output by hand or replace the symbolic link with a real directory, then run metafab build again",
|
|
486
|
+
};
|
|
487
|
+
}
|
|
488
|
+
return undefined;
|
|
489
|
+
}
|
|
490
|
+
function hasWriteAtOrBeneath(writes, directory) {
|
|
491
|
+
const prefix = `${directory}/`;
|
|
492
|
+
return writes.some((write) => write === directory || write.startsWith(prefix));
|
|
493
|
+
}
|
|
494
|
+
async function planEmptyDirectories(root, directory, removals, writes, errors) {
|
|
495
|
+
const target = absolute(root, directory);
|
|
496
|
+
const info = await lstatIfExists(target);
|
|
497
|
+
if (info === undefined || !info.isDirectory())
|
|
498
|
+
return;
|
|
499
|
+
const entries = await readDirectoryIfExists(target, directory, errors);
|
|
500
|
+
if (entries === undefined || entries.length === 0)
|
|
501
|
+
return;
|
|
502
|
+
const removedChildren = entries.filter((entry) => removals.has(`${directory}/${entry.name}`));
|
|
503
|
+
if (removedChildren.length === 0)
|
|
504
|
+
return;
|
|
505
|
+
const remaining = entries.filter((entry) => !removals.has(`${directory}/${entry.name}`));
|
|
506
|
+
if (remaining.length > 0)
|
|
507
|
+
return;
|
|
508
|
+
if (hasWriteAtOrBeneath(writes, directory))
|
|
509
|
+
return;
|
|
510
|
+
removals.add(directory);
|
|
511
|
+
const parent = path.posix.dirname(directory);
|
|
512
|
+
if (parent === "." || parent === "")
|
|
513
|
+
return;
|
|
514
|
+
const parentTarget = absolute(root, parent);
|
|
515
|
+
const parentInfo = await lstatIfExists(parentTarget);
|
|
516
|
+
if (parentInfo === undefined || !parentInfo.isDirectory())
|
|
517
|
+
return;
|
|
518
|
+
const parentEntries = await readDirectoryIfExists(parentTarget, parent, errors);
|
|
519
|
+
if (parentEntries === undefined)
|
|
520
|
+
return;
|
|
521
|
+
const parentRemaining = parentEntries.filter((entry) => !removals.has(`${parent}/${entry.name}`));
|
|
522
|
+
if (parentRemaining.length === 0 && !hasWriteAtOrBeneath(writes, parent)) {
|
|
523
|
+
removals.add(parent);
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
function sameContent(existing, desired) {
|
|
527
|
+
const existingText = textContent(existing);
|
|
528
|
+
const desiredText = textContent(desired);
|
|
529
|
+
if (existingText === undefined || desiredText === undefined)
|
|
530
|
+
return existing.equals(desired);
|
|
531
|
+
return toLF(existingText) === toLF(desiredText);
|
|
532
|
+
}
|
|
533
|
+
export async function fileMatches(root, write) {
|
|
534
|
+
const target = absolute(root, write.path);
|
|
535
|
+
const info = await lstatIfExists(target);
|
|
536
|
+
if (info === undefined) {
|
|
537
|
+
return { exists: false, contentSame: false, modeSame: false, same: false };
|
|
538
|
+
}
|
|
539
|
+
if (!info.isFile()) {
|
|
540
|
+
return { exists: true, contentSame: false, modeSame: false, same: false };
|
|
541
|
+
}
|
|
542
|
+
const existing = await readFile(target);
|
|
543
|
+
const contentSame = sameContent(existing, write.content);
|
|
544
|
+
const mode = info.mode & 0o777;
|
|
545
|
+
// Windows has no permission bits to compare; content alone decides.
|
|
546
|
+
const modeSame = process.platform === "win32" || write.mode === undefined || mode === (write.mode & 0o777);
|
|
547
|
+
return { exists: true, contentSame, modeSame, mode, same: contentSame && modeSame };
|
|
548
|
+
}
|
|
549
|
+
export async function planBuild(factory, config, cliVersion) {
|
|
550
|
+
const root = factory.root;
|
|
551
|
+
const plan = {
|
|
552
|
+
root,
|
|
553
|
+
gitHooks: config.gitHooks,
|
|
554
|
+
cliVersion,
|
|
555
|
+
writes: [],
|
|
556
|
+
removals: [],
|
|
557
|
+
conflicts: [],
|
|
558
|
+
warnings: [],
|
|
559
|
+
errors: [],
|
|
560
|
+
};
|
|
561
|
+
for (const problem of [...factory.problems, ...config.problems]) {
|
|
562
|
+
if (problem.level === "error")
|
|
563
|
+
plan.errors.push(problem);
|
|
564
|
+
}
|
|
565
|
+
const selectedDestinations = destinations(config.agents);
|
|
566
|
+
const factorySkills = await compileFactorySkills(factory, plan.errors);
|
|
567
|
+
const builtin = await compileBuiltinSkill(config, cliVersion, plan.errors);
|
|
568
|
+
const desiredPerDestination = new Map();
|
|
569
|
+
for (const destination of selectedDestinations) {
|
|
570
|
+
const skills = [...factorySkills];
|
|
571
|
+
if (builtin !== undefined)
|
|
572
|
+
skills.push(builtin);
|
|
573
|
+
desiredPerDestination.set(destination.directory, skills);
|
|
574
|
+
for (const skill of skills) {
|
|
575
|
+
plan.writes.push(...skillWrites(destination.directory, skill));
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
const removals = new Set();
|
|
579
|
+
const untraversable = new Set();
|
|
580
|
+
for (const directory of SKILL_DESTINATIONS) {
|
|
581
|
+
const desired = desiredPerDestination.get(directory);
|
|
582
|
+
const selected = desired !== undefined;
|
|
583
|
+
const chain = await inspectDestinationChain(root, directory);
|
|
584
|
+
if (!chain.traversable) {
|
|
585
|
+
untraversable.add(directory);
|
|
586
|
+
if (chain.readError !== undefined)
|
|
587
|
+
plan.errors.push(chain.readError);
|
|
588
|
+
if (chain.problem !== undefined) {
|
|
589
|
+
if (selected) {
|
|
590
|
+
plan.conflicts.push(chain.problem);
|
|
591
|
+
}
|
|
592
|
+
else if (chain.symlink === true) {
|
|
593
|
+
const result = await deselectedSymlinkConflict(root, directory, chain.problem.file);
|
|
594
|
+
if (result !== undefined) {
|
|
595
|
+
if ("error" in result)
|
|
596
|
+
plan.errors.push(result.error);
|
|
597
|
+
else
|
|
598
|
+
plan.conflicts.push(result);
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
continue;
|
|
603
|
+
}
|
|
604
|
+
const context = {
|
|
605
|
+
root,
|
|
606
|
+
selected,
|
|
607
|
+
desired: new Map((desired ?? []).map((skill) => [skill.name, desiredFileTree(skill)])),
|
|
608
|
+
removals,
|
|
609
|
+
conflicts: plan.conflicts,
|
|
610
|
+
errors: plan.errors,
|
|
611
|
+
};
|
|
612
|
+
await scanDestination(context, directory);
|
|
613
|
+
}
|
|
614
|
+
for (const directory of SKILL_DESTINATIONS) {
|
|
615
|
+
if (untraversable.has(directory))
|
|
616
|
+
continue;
|
|
617
|
+
await planEmptyDirectories(root, directory, removals, plan.writes.map((write) => write.path), plan.errors);
|
|
618
|
+
}
|
|
619
|
+
const agentsFile = "AGENTS.md";
|
|
620
|
+
const agentsEntry = await readRootEntry(root, agentsFile);
|
|
621
|
+
const agentsSelected = selectedDestinations.length > 0;
|
|
622
|
+
if (agentsEntry.problem !== undefined) {
|
|
623
|
+
plan.errors.push(agentsEntry.problem);
|
|
624
|
+
}
|
|
625
|
+
else if (agentsEntry.readError === true) {
|
|
626
|
+
plan.conflicts.push(symlinkReadConflict(agentsFile));
|
|
627
|
+
}
|
|
628
|
+
else {
|
|
629
|
+
const agentsResult = agentsSelected
|
|
630
|
+
? planBlockFile(agentsEntry.text, AGENTS_MARKERS, agentsFile, renderAgentsBlockBody(factory), true)
|
|
631
|
+
: planBlockFile(agentsEntry.text, AGENTS_MARKERS, agentsFile, undefined, true);
|
|
632
|
+
addBlockPlan(plan, agentsFile, agentsEntry, agentsResult, removals);
|
|
633
|
+
}
|
|
634
|
+
if (agentsSelected) {
|
|
635
|
+
const lineCount = agentsBlockLineCount(factory);
|
|
636
|
+
if (lineCount > AGENTS_BLOCK_LINE_LIMIT) {
|
|
637
|
+
plan.warnings.push({
|
|
638
|
+
level: "warning",
|
|
639
|
+
file: agentsFile,
|
|
640
|
+
message: `The generated AGENTS.md block is ${lineCount} lines; the limit is ${AGENTS_BLOCK_LINE_LIMIT}`,
|
|
641
|
+
fix: "Move detail into .factory/AGENTS.md sections and keep the root block short",
|
|
642
|
+
});
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
const claudeFile = "CLAUDE.md";
|
|
646
|
+
const claudeSelected = config.agents.includes("claude");
|
|
647
|
+
const claudeEntry = await readRootEntry(root, claudeFile);
|
|
648
|
+
const claudeFollowsAgents = await claudeLinksToRootAgents(root, claudeEntry);
|
|
649
|
+
if (claudeEntry.problem !== undefined) {
|
|
650
|
+
plan.errors.push(claudeEntry.problem);
|
|
651
|
+
}
|
|
652
|
+
else if (claudeEntry.readError === true) {
|
|
653
|
+
plan.conflicts.push(symlinkReadConflict(claudeFile));
|
|
654
|
+
}
|
|
655
|
+
else if (!claudeFollowsAgents && claudeEntry.kind !== "absent") {
|
|
656
|
+
const claudeExisting = claudeEntry.text ?? "";
|
|
657
|
+
const found = findMarkerBlock(claudeExisting, CLAUDE_MARKERS);
|
|
658
|
+
if (!found.ok) {
|
|
659
|
+
const conflict = rootEntryConflict(claudeFile, claudeEntry.kind);
|
|
660
|
+
if (conflict !== undefined)
|
|
661
|
+
plan.conflicts.push(conflict);
|
|
662
|
+
else
|
|
663
|
+
plan.errors.push(markerProblem(claudeFile, found.problem));
|
|
664
|
+
}
|
|
665
|
+
else if (claudeSelected) {
|
|
666
|
+
if (!importsAgents(claudeExisting, found.range)) {
|
|
667
|
+
const without = found.range === undefined
|
|
668
|
+
? { ok: true, text: claudeExisting }
|
|
669
|
+
: removeMarkerBlock(claudeExisting, CLAUDE_MARKERS);
|
|
670
|
+
if (!without.ok) {
|
|
671
|
+
plan.errors.push(markerProblem(claudeFile, without.problem));
|
|
672
|
+
}
|
|
673
|
+
else {
|
|
674
|
+
const rest = toLF(without.text).replace(/^\n+/, "");
|
|
675
|
+
const block = claudeBlock();
|
|
676
|
+
const next = rest === "" ? `${block}\n` : `${block}\n\n${rest}`;
|
|
677
|
+
if (next !== claudeExisting) {
|
|
678
|
+
const conflict = rootEntryConflict(claudeFile, claudeEntry.kind);
|
|
679
|
+
if (conflict !== undefined) {
|
|
680
|
+
plan.conflicts.push(conflict);
|
|
681
|
+
}
|
|
682
|
+
else {
|
|
683
|
+
plan.writes.push({ path: claudeFile, content: Buffer.from(next, "utf8") });
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
else if (found.range !== undefined) {
|
|
690
|
+
const removed = removeMarkerBlock(claudeExisting, CLAUDE_MARKERS);
|
|
691
|
+
if (!removed.ok) {
|
|
692
|
+
plan.errors.push(markerProblem(claudeFile, removed.problem));
|
|
693
|
+
}
|
|
694
|
+
else if (removed.changed) {
|
|
695
|
+
const conflict = rootEntryConflict(claudeFile, claudeEntry.kind);
|
|
696
|
+
if (conflict !== undefined) {
|
|
697
|
+
plan.conflicts.push(conflict);
|
|
698
|
+
}
|
|
699
|
+
else {
|
|
700
|
+
plan.writes.push({ path: claudeFile, content: Buffer.from(removed.text, "utf8") });
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
const gitignoreFile = ".gitignore";
|
|
706
|
+
const gitignoreEntry = await readRootEntry(root, gitignoreFile);
|
|
707
|
+
if (gitignoreEntry.problem !== undefined) {
|
|
708
|
+
plan.errors.push(gitignoreEntry.problem);
|
|
709
|
+
}
|
|
710
|
+
else if (gitignoreEntry.readError === true) {
|
|
711
|
+
plan.conflicts.push(symlinkReadConflict(gitignoreFile));
|
|
712
|
+
}
|
|
713
|
+
else {
|
|
714
|
+
const gitignoreLines = gitignoreBlockBody(config.agents);
|
|
715
|
+
const gitignoreBody = gitignoreLines.length === 0 ? undefined : gitignoreLines.join("\n");
|
|
716
|
+
const gitignoreResult = planBlockFile(gitignoreEntry.text, GITIGNORE_MARKERS, gitignoreFile, gitignoreBody);
|
|
717
|
+
addBlockPlan(plan, gitignoreFile, gitignoreEntry, gitignoreResult, removals);
|
|
718
|
+
}
|
|
719
|
+
plan.removals = [...removals].sort();
|
|
720
|
+
plan.errors = dedupeErrors(plan.errors);
|
|
721
|
+
return plan;
|
|
722
|
+
}
|
|
723
|
+
function hookFailureWarning(error) {
|
|
724
|
+
const code = error.code ?? "error";
|
|
725
|
+
return {
|
|
726
|
+
level: "warning",
|
|
727
|
+
file: ".factory/metafab.yaml",
|
|
728
|
+
message: `Git hooks could not be synced: ${code}`,
|
|
729
|
+
fix: "Check the permissions of the repository's hooks directory, then run metafab build again",
|
|
730
|
+
};
|
|
731
|
+
}
|
|
732
|
+
export async function applyBuild(plan) {
|
|
733
|
+
const changed = [];
|
|
734
|
+
if (plan.errors.length > 0 || plan.conflicts.length > 0) {
|
|
735
|
+
return { changed, hooks: [], hookWarnings: [] };
|
|
736
|
+
}
|
|
737
|
+
const existingRemovals = [];
|
|
738
|
+
for (const removal of plan.removals) {
|
|
739
|
+
if ((await lstatIfExists(absolute(plan.root, removal))) !== undefined) {
|
|
740
|
+
existingRemovals.push(removal);
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
const deepestFirst = [...existingRemovals].sort((left, right) => right.length - left.length);
|
|
744
|
+
for (const removal of deepestFirst) {
|
|
745
|
+
await rm(absolute(plan.root, removal), { recursive: true, force: true });
|
|
746
|
+
}
|
|
747
|
+
const removed = new Set(existingRemovals);
|
|
748
|
+
const writePaths = plan.writes.map((write) => write.path);
|
|
749
|
+
const written = new Set(writePaths);
|
|
750
|
+
for (const write of plan.writes) {
|
|
751
|
+
const target = absolute(plan.root, write.path);
|
|
752
|
+
const comparison = await fileMatches(plan.root, write);
|
|
753
|
+
if (comparison.same)
|
|
754
|
+
continue;
|
|
755
|
+
await mkdir(path.dirname(target), { recursive: true });
|
|
756
|
+
// Repair the mode before writing: a read-only installed file (0444) cannot
|
|
757
|
+
// be truncated, and an executable one keeps its target mode.
|
|
758
|
+
if (comparison.exists && write.mode !== undefined) {
|
|
759
|
+
const info = await lstatIfExists(target);
|
|
760
|
+
if (info?.isFile() === true)
|
|
761
|
+
await chmod(target, write.mode);
|
|
762
|
+
}
|
|
763
|
+
await writeFile(target, write.content);
|
|
764
|
+
if (write.mode !== undefined)
|
|
765
|
+
await chmod(target, write.mode);
|
|
766
|
+
changed.push({
|
|
767
|
+
path: write.path,
|
|
768
|
+
action: comparison.exists || removed.has(write.path) ? "updated" : "created",
|
|
769
|
+
});
|
|
770
|
+
}
|
|
771
|
+
for (const removal of existingRemovals) {
|
|
772
|
+
if (written.has(removal))
|
|
773
|
+
continue;
|
|
774
|
+
if (writePaths.some((writePath) => writePath.startsWith(`${removal}/`))) {
|
|
775
|
+
changed.push({ path: removal, action: "updated" });
|
|
776
|
+
continue;
|
|
777
|
+
}
|
|
778
|
+
changed.push({ path: removal, action: "removed" });
|
|
779
|
+
}
|
|
780
|
+
changed.sort((left, right) => left.path < right.path ? -1 : left.path > right.path ? 1 : 0);
|
|
781
|
+
// Hooks follow the config like any other build output, but they are local, so
|
|
782
|
+
// they are applied only here: `planBuild` and `check` never touch them. A
|
|
783
|
+
// hook problem never fails the build.
|
|
784
|
+
try {
|
|
785
|
+
const sync = await syncGitHooks(plan.root, plan.gitHooks, plan.cliVersion);
|
|
786
|
+
return { changed, hooks: sync.changed, hookWarnings: sync.warnings };
|
|
787
|
+
}
|
|
788
|
+
catch (error) {
|
|
789
|
+
return { changed, hooks: [], hookWarnings: [hookFailureWarning(error)] };
|
|
790
|
+
}
|
|
791
|
+
}
|
|
792
|
+
//# sourceMappingURL=plan.js.map
|