@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,676 @@
|
|
|
1
|
+
import { cp, lstat, mkdir, readFile, readlink, realpath, rename, rm } from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { ExitCode, MetafabError } from "../errors.js";
|
|
4
|
+
import { readdirTyped } from "../util/fs.js";
|
|
5
|
+
import { parseFrontmatter } from "../util/frontmatter.js";
|
|
6
|
+
import { equalIgnoringEOL } from "../util/text.js";
|
|
7
|
+
import { BUILTIN_SKILL_NAMES } from "./build/plan.js";
|
|
8
|
+
import { compileSkill, isOwned, textContent } from "./build/skill.js";
|
|
9
|
+
import { setSkillLines } from "./factory/lines.js";
|
|
10
|
+
import { rule1RequiredFields, rule2NameMatchesDirectory, rule3NoNestedSkills, rule5NameFormat, rule6SkillGrammar, rule10ReservedPrefix, } from "./factory/validate.js";
|
|
11
|
+
/** Repository-relative directory that receives adopted skills. */
|
|
12
|
+
export const FACTORY_SKILLS_DIRECTORY = ".factory/skills";
|
|
13
|
+
const FACTORY_DIRECTORY = ".factory";
|
|
14
|
+
/** The consent question from the specification. */
|
|
15
|
+
export const ADOPTION_QUESTION = "Metafab manages installed skill directories as generated output. Move existing repository skills into `.factory/skills/` and ignore the generated copies?";
|
|
16
|
+
export const ADOPTION_DESCRIPTION = "Move existing repository skills into .factory/skills/ and ignore the generated copies";
|
|
17
|
+
function absolute(root, relativePath) {
|
|
18
|
+
return path.join(root, ...relativePath.split("/"));
|
|
19
|
+
}
|
|
20
|
+
function normalizeDirectory(directory) {
|
|
21
|
+
return directory.replace(/\\/g, "/").replace(/^\.\//, "").replace(/\/+$/, "");
|
|
22
|
+
}
|
|
23
|
+
function uniqueDirectories(destinations) {
|
|
24
|
+
const unique = [];
|
|
25
|
+
for (const destination of destinations) {
|
|
26
|
+
const normalized = normalizeDirectory(destination);
|
|
27
|
+
if (normalized !== "" && !unique.includes(normalized))
|
|
28
|
+
unique.push(normalized);
|
|
29
|
+
}
|
|
30
|
+
return unique;
|
|
31
|
+
}
|
|
32
|
+
async function lstatIfExists(target) {
|
|
33
|
+
try {
|
|
34
|
+
return await lstat(target);
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
const code = error.code;
|
|
38
|
+
if (code === "ENOENT" || code === "ENOTDIR")
|
|
39
|
+
return undefined;
|
|
40
|
+
throw error;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* The real path of `target` when the path itself does not exist yet: the real
|
|
45
|
+
* path of its deepest existing ancestor, with the missing segments joined back
|
|
46
|
+
* on. The skill's new directory only exists after the move, so the loop check
|
|
47
|
+
* needs this to compare where a link really points, not just what it spells.
|
|
48
|
+
*
|
|
49
|
+
* Returns undefined when no ancestor resolves, as with a symlink loop.
|
|
50
|
+
*/
|
|
51
|
+
async function realPathOf(target) {
|
|
52
|
+
const missing = [];
|
|
53
|
+
let current = target;
|
|
54
|
+
for (;;) {
|
|
55
|
+
try {
|
|
56
|
+
return path.join(await realpath(current), ...missing.reverse());
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
const code = error.code;
|
|
60
|
+
if (code !== "ENOENT" && code !== "ENOTDIR")
|
|
61
|
+
return undefined;
|
|
62
|
+
}
|
|
63
|
+
const parent = path.dirname(current);
|
|
64
|
+
if (parent === current)
|
|
65
|
+
return undefined;
|
|
66
|
+
missing.push(path.basename(current));
|
|
67
|
+
current = parent;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
function loopProblem(linkPath) {
|
|
71
|
+
return {
|
|
72
|
+
level: "error",
|
|
73
|
+
file: linkPath,
|
|
74
|
+
message: `Symlink ${linkPath} creates a loop after the move`,
|
|
75
|
+
fix: "Point it at a file or a directory that does not contain the skill, or remove the symlink",
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
function outsideProblem(linkPath) {
|
|
79
|
+
return {
|
|
80
|
+
level: "error",
|
|
81
|
+
file: linkPath,
|
|
82
|
+
message: `Symlink ${linkPath} points outside .factory/ after the move`,
|
|
83
|
+
fix: "Point it inside .factory/, or remove the symlink",
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
function unreadable(relativePath) {
|
|
87
|
+
return {
|
|
88
|
+
level: "error",
|
|
89
|
+
file: relativePath,
|
|
90
|
+
message: `Cannot read ${relativePath}`,
|
|
91
|
+
fix: "Check its permissions",
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
function notADirectory(directory, symlink) {
|
|
95
|
+
return {
|
|
96
|
+
level: "error",
|
|
97
|
+
file: directory,
|
|
98
|
+
message: symlink
|
|
99
|
+
? `${directory} is a symbolic link, not a directory`
|
|
100
|
+
: `${directory} is a file, not a directory`,
|
|
101
|
+
fix: "Move it or replace it with a directory, then run metafab init again",
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
function unownedFile(relativePath, directory) {
|
|
105
|
+
return {
|
|
106
|
+
level: "error",
|
|
107
|
+
file: relativePath,
|
|
108
|
+
message: `Unowned file ${relativePath} is not part of a valid skill`,
|
|
109
|
+
fix: `Move it out of ${directory}, or add it to a skill in .factory/skills/`,
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
function unownedSymlink(relativePath) {
|
|
113
|
+
return {
|
|
114
|
+
level: "error",
|
|
115
|
+
file: relativePath,
|
|
116
|
+
message: `Unowned entry ${relativePath} is a symbolic link, not part of a valid skill`,
|
|
117
|
+
fix: "Remove the symbolic link or replace it with a real skill directory",
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
function unownedDirectory(relativePath, directory) {
|
|
121
|
+
return {
|
|
122
|
+
level: "error",
|
|
123
|
+
file: relativePath,
|
|
124
|
+
message: `Unowned directory ${relativePath} is not part of a valid skill`,
|
|
125
|
+
fix: `Add a SKILL.md with name and description frontmatter, or move the directory out of ${directory}`,
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
function isInside(root, target) {
|
|
129
|
+
const relative = path.relative(root, target);
|
|
130
|
+
return (relative === "" ||
|
|
131
|
+
(!relative.startsWith(`..${path.sep}`) && relative !== ".." && !path.isAbsolute(relative)));
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* A factory skill stub for a candidate skill, at the path it will occupy after
|
|
135
|
+
* the move, so the factory's own rules validate it exactly as `build` will.
|
|
136
|
+
* The frontmatter line numbers are copied the way `loadFactory` copies them, so
|
|
137
|
+
* problems point at the field they name instead of the top of the file.
|
|
138
|
+
*/
|
|
139
|
+
function candidateSkill(name, parsed) {
|
|
140
|
+
const skill = {
|
|
141
|
+
name,
|
|
142
|
+
relDir: `skills/${name}`,
|
|
143
|
+
group: "skills",
|
|
144
|
+
isWorkflow: false,
|
|
145
|
+
frontmatter: parsed.value,
|
|
146
|
+
frontmatterLines: { start: parsed.startLine, end: parsed.endLine },
|
|
147
|
+
};
|
|
148
|
+
const nameNode = parsed.document.get("name", true);
|
|
149
|
+
const descriptionNode = parsed.document.get("description", true);
|
|
150
|
+
setSkillLines(skill, {
|
|
151
|
+
...(nameNode === undefined ? {} : { name: parsed.lineOf(nameNode) }),
|
|
152
|
+
...(descriptionNode === undefined ? {} : { description: parsed.lineOf(descriptionNode) }),
|
|
153
|
+
});
|
|
154
|
+
return skill;
|
|
155
|
+
}
|
|
156
|
+
/** The factory validation rules that apply to one skill. */
|
|
157
|
+
function candidateSkillProblems(name, parsed) {
|
|
158
|
+
const skill = candidateSkill(name, parsed);
|
|
159
|
+
const problems = [...rule1RequiredFields(skill), ...rule6SkillGrammar(skill)];
|
|
160
|
+
const declared = parsed.value.name;
|
|
161
|
+
if (typeof declared === "string" && declared !== "") {
|
|
162
|
+
problems.push(...rule2NameMatchesDirectory(skill), ...rule5NameFormat(skill), ...rule10ReservedPrefix(skill));
|
|
163
|
+
}
|
|
164
|
+
return problems;
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* A symlink inside the skill that would not work after the move. Adoption moves
|
|
168
|
+
* only the skill directory, so a link into `.factory/` is still valid (and
|
|
169
|
+
* `build` accepts it), while one into the repository or another destination
|
|
170
|
+
* would be left behind or change meaning, and `build` would fail on it after
|
|
171
|
+
* the move.
|
|
172
|
+
*
|
|
173
|
+
* A target that contains the skill's new directory is refused separately: once
|
|
174
|
+
* the skill is there the link points at itself or at an ancestor, which `build`
|
|
175
|
+
* reports as a loop. The check runs on the spelled path and on the real path,
|
|
176
|
+
* so a directory symlink inside `.factory/` or a filename that differs only in
|
|
177
|
+
* case cannot hide the same place on disk.
|
|
178
|
+
*
|
|
179
|
+
* The containment check also runs on the real path: a link can spell a path
|
|
180
|
+
* inside `.factory/` and still leave it through a symlinked directory there.
|
|
181
|
+
* Only the moved location matters, so the real path is resolved through the
|
|
182
|
+
* directories that exist before the move.
|
|
183
|
+
*/
|
|
184
|
+
async function linkProblem(root, relative, linkPath, to) {
|
|
185
|
+
let target;
|
|
186
|
+
try {
|
|
187
|
+
target = await readlink(absolute(root, linkPath));
|
|
188
|
+
}
|
|
189
|
+
catch {
|
|
190
|
+
return unreadable(linkPath);
|
|
191
|
+
}
|
|
192
|
+
const destinationLink = absolute(root, `${to}/${relative}`);
|
|
193
|
+
const destinationTarget = path.resolve(path.dirname(destinationLink), target);
|
|
194
|
+
if (!isInside(absolute(root, FACTORY_DIRECTORY), destinationTarget)) {
|
|
195
|
+
return outsideProblem(linkPath);
|
|
196
|
+
}
|
|
197
|
+
// The lexical check above misses a target that reaches outside through a
|
|
198
|
+
// directory symlink inside `.factory/`. Compare the real paths as well,
|
|
199
|
+
// resolved through the directories that exist before the move.
|
|
200
|
+
const realTarget = await realPathOf(destinationTarget);
|
|
201
|
+
const realFactory = await realPathOf(absolute(root, FACTORY_DIRECTORY));
|
|
202
|
+
if (realTarget !== undefined &&
|
|
203
|
+
realFactory !== undefined &&
|
|
204
|
+
!isInside(realFactory, realTarget)) {
|
|
205
|
+
return outsideProblem(linkPath);
|
|
206
|
+
}
|
|
207
|
+
const destinationSkill = absolute(root, to);
|
|
208
|
+
if (isInside(destinationTarget, destinationSkill))
|
|
209
|
+
return loopProblem(linkPath);
|
|
210
|
+
// A filename that differs only in case can reach the skill on a
|
|
211
|
+
// case-insensitive filesystem.
|
|
212
|
+
const realSkill = await realPathOf(destinationSkill);
|
|
213
|
+
if (realTarget !== undefined && realSkill !== undefined && isInside(realTarget, realSkill)) {
|
|
214
|
+
return loopProblem(linkPath);
|
|
215
|
+
}
|
|
216
|
+
return undefined;
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* The first entry in the candidate tree that the move cannot carry: a symlink
|
|
220
|
+
* that would leave `.factory/` or point back at the skill, or a nested skill
|
|
221
|
+
* directory.
|
|
222
|
+
*
|
|
223
|
+
* `compileSkill` copies every directory it can read, dot directories included,
|
|
224
|
+
* so links inside one are judged here too: a hidden directory that loops or
|
|
225
|
+
* leaves `.factory/` fails the build after the move, whatever its name. The
|
|
226
|
+
* nested-skill rule does not apply inside a dot directory, because factory
|
|
227
|
+
* discovery skips those directories and `build` accepts the `SKILL.md` there.
|
|
228
|
+
*/
|
|
229
|
+
async function treeProblems(root, from, to) {
|
|
230
|
+
const pending = [{ relative: "", hidden: false }];
|
|
231
|
+
while (pending.length > 0) {
|
|
232
|
+
const { relative, hidden } = pending.shift();
|
|
233
|
+
const current = relative === "" ? from : `${from}/${relative}`;
|
|
234
|
+
let entries;
|
|
235
|
+
try {
|
|
236
|
+
entries = await readdirTyped(absolute(root, current));
|
|
237
|
+
}
|
|
238
|
+
catch {
|
|
239
|
+
// `compileSkill` reports the unreadable directory itself.
|
|
240
|
+
continue;
|
|
241
|
+
}
|
|
242
|
+
entries.sort((left, right) => (left.name < right.name ? -1 : left.name > right.name ? 1 : 0));
|
|
243
|
+
for (const entry of entries) {
|
|
244
|
+
const child = relative === "" ? entry.name : `${relative}/${entry.name}`;
|
|
245
|
+
const childPath = `${from}/${child}`;
|
|
246
|
+
if (entry.isSymbolicLink()) {
|
|
247
|
+
const problem = await linkProblem(root, child, childPath, to);
|
|
248
|
+
if (problem !== undefined)
|
|
249
|
+
return problem;
|
|
250
|
+
continue;
|
|
251
|
+
}
|
|
252
|
+
if (!entry.isDirectory())
|
|
253
|
+
continue;
|
|
254
|
+
const hiding = entry.name.startsWith(".");
|
|
255
|
+
if (!hidden && !hiding) {
|
|
256
|
+
const nestedSkill = await lstatIfExists(absolute(root, `${childPath}/SKILL.md`));
|
|
257
|
+
if (nestedSkill !== undefined && nestedSkill.isFile()) {
|
|
258
|
+
return rule3NoNestedSkills(`${childPath}/SKILL.md`, 1, childPath, from)[0];
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
pending.push({ relative: child, hidden: hidden || hiding });
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
return undefined;
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* Problems that `build` would report for the skill after the move. The walk is
|
|
268
|
+
* `build`'s own `compileSkill`, so loop detection and file reads are the same
|
|
269
|
+
* code: it runs on the source tree with the skill directory as the containment
|
|
270
|
+
* root, because the directory moves as a unit, and a link that stays inside it
|
|
271
|
+
* stays inside `.factory/skills/<name>/` too.
|
|
272
|
+
*
|
|
273
|
+
* A link whose target sits elsewhere in `.factory/` is accepted by
|
|
274
|
+
* `treeProblems` and must pass here as well, so the factory directory joins the
|
|
275
|
+
* roots a link may resolve into. So does the current directory of every other
|
|
276
|
+
* skill adopted in the same run: a link into a sibling skill resolves into that
|
|
277
|
+
* skill's current directory now, and into `.factory/skills/` after both moves.
|
|
278
|
+
* Build never passes `linkRoots`.
|
|
279
|
+
*/
|
|
280
|
+
async function checkSkillTree(root, name, from, adoptedDirs) {
|
|
281
|
+
const relocation = await treeProblems(root, from, `${FACTORY_SKILLS_DIRECTORY}/${name}`);
|
|
282
|
+
if (relocation !== undefined)
|
|
283
|
+
return relocation;
|
|
284
|
+
const skillDir = absolute(root, from);
|
|
285
|
+
const factoryDirectory = absolute(root, FACTORY_DIRECTORY);
|
|
286
|
+
const factoryExists = (await lstatIfExists(factoryDirectory)) !== undefined;
|
|
287
|
+
const compiled = await compileSkill({ dir: skillDir, name, sourcePath: from }, {
|
|
288
|
+
factoryDir: skillDir,
|
|
289
|
+
linkRoots: [
|
|
290
|
+
...(factoryExists ? [factoryDirectory] : []),
|
|
291
|
+
...adoptedDirs.map((directory) => absolute(root, directory)),
|
|
292
|
+
],
|
|
293
|
+
});
|
|
294
|
+
return compiled.problems[0];
|
|
295
|
+
}
|
|
296
|
+
async function checkSkill(root, name, from, directory) {
|
|
297
|
+
const skillFile = `${from}/SKILL.md`;
|
|
298
|
+
const info = await lstatIfExists(absolute(root, skillFile));
|
|
299
|
+
if (info === undefined)
|
|
300
|
+
return unownedDirectory(from, directory);
|
|
301
|
+
if (!info.isFile()) {
|
|
302
|
+
return {
|
|
303
|
+
level: "error",
|
|
304
|
+
file: skillFile,
|
|
305
|
+
message: `${skillFile} is not a regular file`,
|
|
306
|
+
fix: "Replace it with a SKILL.md file with name and description frontmatter",
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
let source;
|
|
310
|
+
try {
|
|
311
|
+
source = await readFile(absolute(root, skillFile), "utf8");
|
|
312
|
+
}
|
|
313
|
+
catch {
|
|
314
|
+
return {
|
|
315
|
+
level: "error",
|
|
316
|
+
file: skillFile,
|
|
317
|
+
message: `Cannot read ${skillFile}`,
|
|
318
|
+
fix: "Check its permissions",
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
const parsed = parseFrontmatter(source);
|
|
322
|
+
if (!parsed.ok) {
|
|
323
|
+
const problem = parsed.problems[0];
|
|
324
|
+
return {
|
|
325
|
+
level: "error",
|
|
326
|
+
file: skillFile,
|
|
327
|
+
...(problem.line === undefined ? {} : { line: problem.line }),
|
|
328
|
+
message: `Unowned directory ${from} is not part of a valid skill: ${problem.message}`,
|
|
329
|
+
fix: problem.fix ?? `Fix ${skillFile}, or move the directory out of ${directory}`,
|
|
330
|
+
};
|
|
331
|
+
}
|
|
332
|
+
const problems = candidateSkillProblems(name, parsed);
|
|
333
|
+
const problem = problems[0];
|
|
334
|
+
if (problem !== undefined)
|
|
335
|
+
return { ...problem, file: skillFile };
|
|
336
|
+
return undefined;
|
|
337
|
+
}
|
|
338
|
+
async function scanDestination(root, directory, blockers) {
|
|
339
|
+
const target = absolute(root, directory);
|
|
340
|
+
const info = await lstatIfExists(target);
|
|
341
|
+
if (info === undefined)
|
|
342
|
+
return [];
|
|
343
|
+
if (!info.isDirectory()) {
|
|
344
|
+
blockers.push(notADirectory(directory, info.isSymbolicLink()));
|
|
345
|
+
return [];
|
|
346
|
+
}
|
|
347
|
+
let entries;
|
|
348
|
+
try {
|
|
349
|
+
entries = await readdirTyped(target);
|
|
350
|
+
}
|
|
351
|
+
catch {
|
|
352
|
+
blockers.push(unreadable(directory));
|
|
353
|
+
return [];
|
|
354
|
+
}
|
|
355
|
+
entries.sort((left, right) => (left.name < right.name ? -1 : left.name > right.name ? 1 : 0));
|
|
356
|
+
const candidates = [];
|
|
357
|
+
for (const entry of entries) {
|
|
358
|
+
const from = `${directory}/${entry.name}`;
|
|
359
|
+
if (entry.isSymbolicLink()) {
|
|
360
|
+
blockers.push(unownedSymlink(from));
|
|
361
|
+
continue;
|
|
362
|
+
}
|
|
363
|
+
if (!entry.isDirectory()) {
|
|
364
|
+
blockers.push(unownedFile(from, directory));
|
|
365
|
+
continue;
|
|
366
|
+
}
|
|
367
|
+
if (await isOwned(absolute(root, from)))
|
|
368
|
+
continue;
|
|
369
|
+
if (BUILTIN_SKILL_NAMES.includes(entry.name)) {
|
|
370
|
+
blockers.push(builtinClash(entry.name, from));
|
|
371
|
+
continue;
|
|
372
|
+
}
|
|
373
|
+
const blocker = await checkSkill(root, entry.name, from, directory);
|
|
374
|
+
if (blocker !== undefined)
|
|
375
|
+
blockers.push(blocker);
|
|
376
|
+
else
|
|
377
|
+
candidates.push({ name: entry.name, from });
|
|
378
|
+
}
|
|
379
|
+
return candidates;
|
|
380
|
+
}
|
|
381
|
+
async function collectTree(root, directory) {
|
|
382
|
+
const entries = new Map();
|
|
383
|
+
async function walk(relativePath, prefix) {
|
|
384
|
+
const children = await readdirTyped(absolute(root, relativePath));
|
|
385
|
+
children.sort((left, right) => (left.name < right.name ? -1 : left.name > right.name ? 1 : 0));
|
|
386
|
+
for (const child of children) {
|
|
387
|
+
const childPath = `${relativePath}/${child.name}`;
|
|
388
|
+
const key = prefix === "" ? child.name : `${prefix}/${child.name}`;
|
|
389
|
+
if (child.isSymbolicLink()) {
|
|
390
|
+
entries.set(key, {
|
|
391
|
+
kind: "symlink",
|
|
392
|
+
target: await readlink(absolute(root, childPath)),
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
else if (child.isDirectory()) {
|
|
396
|
+
entries.set(key, { kind: "directory" });
|
|
397
|
+
await walk(childPath, key);
|
|
398
|
+
}
|
|
399
|
+
else if (child.isFile()) {
|
|
400
|
+
entries.set(key, { kind: "file", content: await readFile(absolute(root, childPath)) });
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
await walk(directory, "");
|
|
405
|
+
return entries;
|
|
406
|
+
}
|
|
407
|
+
function sameFileContent(left, right) {
|
|
408
|
+
const leftText = textContent(left);
|
|
409
|
+
const rightText = textContent(right);
|
|
410
|
+
if (leftText === undefined || rightText === undefined)
|
|
411
|
+
return left.equals(right);
|
|
412
|
+
return equalIgnoringEOL(leftText, rightText);
|
|
413
|
+
}
|
|
414
|
+
async function sameTree(root, left, right) {
|
|
415
|
+
const leftTree = await collectTree(root, left);
|
|
416
|
+
const rightTree = await collectTree(root, right);
|
|
417
|
+
if (leftTree.size !== rightTree.size)
|
|
418
|
+
return false;
|
|
419
|
+
for (const [key, entry] of leftTree) {
|
|
420
|
+
const other = rightTree.get(key);
|
|
421
|
+
if (other === undefined || other.kind !== entry.kind)
|
|
422
|
+
return false;
|
|
423
|
+
if (entry.kind === "file" && other.kind === "file" && !sameFileContent(entry.content, other.content)) {
|
|
424
|
+
return false;
|
|
425
|
+
}
|
|
426
|
+
if (entry.kind === "symlink" && other.kind === "symlink" && entry.target !== other.target) {
|
|
427
|
+
return false;
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
return true;
|
|
431
|
+
}
|
|
432
|
+
function differs(name, left, right) {
|
|
433
|
+
return {
|
|
434
|
+
level: "error",
|
|
435
|
+
file: right,
|
|
436
|
+
message: `Skill "${name}" differs between ${left} and ${right}`,
|
|
437
|
+
fix: "Make the two copies identical, or remove one by hand",
|
|
438
|
+
};
|
|
439
|
+
}
|
|
440
|
+
function factoryClash(name, from) {
|
|
441
|
+
return {
|
|
442
|
+
level: "error",
|
|
443
|
+
file: from,
|
|
444
|
+
message: `Skill "${name}" already exists in .factory/`,
|
|
445
|
+
fix: "Rename the skill, or remove the existing factory skill",
|
|
446
|
+
};
|
|
447
|
+
}
|
|
448
|
+
function builtinClash(name, from) {
|
|
449
|
+
return {
|
|
450
|
+
level: "error",
|
|
451
|
+
file: from,
|
|
452
|
+
message: `Skill "${name}" clashes with the built-in "${name}" skill that metafab build installs`,
|
|
453
|
+
fix: "Rename the skill, or remove it from the repository",
|
|
454
|
+
};
|
|
455
|
+
}
|
|
456
|
+
function incomingClash(name, from) {
|
|
457
|
+
return {
|
|
458
|
+
level: "error",
|
|
459
|
+
file: from,
|
|
460
|
+
message: `Skill "${name}" already exists in the incoming fab`,
|
|
461
|
+
fix: "Rename the skill, or remove it from the fab",
|
|
462
|
+
};
|
|
463
|
+
}
|
|
464
|
+
function sortProblems(problems) {
|
|
465
|
+
return problems.sort((left, right) => {
|
|
466
|
+
const leftKey = `${left.file}\u0000${left.line ?? 0}\u0000${left.message}`;
|
|
467
|
+
const rightKey = `${right.file}\u0000${right.line ?? 0}\u0000${right.message}`;
|
|
468
|
+
return leftKey < rightKey ? -1 : leftKey > rightKey ? 1 : 0;
|
|
469
|
+
});
|
|
470
|
+
}
|
|
471
|
+
/**
|
|
472
|
+
* Problems with the directories that will receive moves. A symlinked directory
|
|
473
|
+
* would move the skill outside the repository, so adoption refuses it.
|
|
474
|
+
*/
|
|
475
|
+
async function destinationProblems(root) {
|
|
476
|
+
const problems = [];
|
|
477
|
+
for (const directory of [FACTORY_DIRECTORY, FACTORY_SKILLS_DIRECTORY]) {
|
|
478
|
+
const info = await lstatIfExists(absolute(root, directory));
|
|
479
|
+
if (info === undefined)
|
|
480
|
+
continue;
|
|
481
|
+
if (info.isSymbolicLink() || !info.isDirectory()) {
|
|
482
|
+
problems.push({
|
|
483
|
+
level: "error",
|
|
484
|
+
file: directory,
|
|
485
|
+
message: info.isSymbolicLink()
|
|
486
|
+
? `${directory} is a symbolic link, not a directory`
|
|
487
|
+
: `${directory} is a file, not a directory`,
|
|
488
|
+
fix: "Replace it with a real directory, then run metafab init again",
|
|
489
|
+
});
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
return problems;
|
|
493
|
+
}
|
|
494
|
+
/** The error for a plan whose blockers stopped adoption, shared by every caller. */
|
|
495
|
+
export function adoptionBlockedError(plan) {
|
|
496
|
+
const count = plan.blockers.length;
|
|
497
|
+
return new MetafabError(`Adoption is blocked by ${count} ${count === 1 ? "entry" : "entries"}.`, {
|
|
498
|
+
exitCode: ExitCode.NotOwned,
|
|
499
|
+
code: "adoption-blocked",
|
|
500
|
+
problems: [...plan.blockers],
|
|
501
|
+
});
|
|
502
|
+
}
|
|
503
|
+
/** Throws exit 5 when the plan has blockers, so no caller plans around them. */
|
|
504
|
+
export function assertAdoptionAllowed(plan) {
|
|
505
|
+
if (plan.blockers.length > 0)
|
|
506
|
+
throw adoptionBlockedError(plan);
|
|
507
|
+
}
|
|
508
|
+
/**
|
|
509
|
+
* Every name an existing factory skill answers to: its directory name and the
|
|
510
|
+
* `name` in its frontmatter. Validation compares the frontmatter names, so a
|
|
511
|
+
* skill whose directory and declared name disagree blocks a candidate that
|
|
512
|
+
* matches either one. Skills that discovery dropped — an unparseable SKILL.md
|
|
513
|
+
* never reaches this list — contribute nothing and cannot crash planning.
|
|
514
|
+
*/
|
|
515
|
+
function factorySkillNames(factory) {
|
|
516
|
+
const names = new Set();
|
|
517
|
+
for (const skill of factory?.skills ?? []) {
|
|
518
|
+
if (typeof skill.name === "string" && skill.name !== "")
|
|
519
|
+
names.add(skill.name);
|
|
520
|
+
const frontmatter = skill.frontmatter;
|
|
521
|
+
if (typeof frontmatter !== "object" || frontmatter === null)
|
|
522
|
+
continue;
|
|
523
|
+
const declared = frontmatter.name;
|
|
524
|
+
if (typeof declared === "string" && declared !== "")
|
|
525
|
+
names.add(declared);
|
|
526
|
+
}
|
|
527
|
+
return names;
|
|
528
|
+
}
|
|
529
|
+
/**
|
|
530
|
+
* Plans the move of repository skills that Metafab does not own from the selected
|
|
531
|
+
* destinations into `.factory/skills/`. It reads only; `applyAdoption` writes.
|
|
532
|
+
*
|
|
533
|
+
* `factory` supplies the names that already exist in `.factory/`. Pass `undefined`
|
|
534
|
+
* when the repository has no factory yet. `incoming` lists skill names from a fab
|
|
535
|
+
* that `import` is about to copy.
|
|
536
|
+
*/
|
|
537
|
+
export async function planAdoption(root, destinations, factory, incoming = []) {
|
|
538
|
+
const absoluteRoot = path.resolve(root);
|
|
539
|
+
const plan = { root: absoluteRoot, moves: [], blockers: [] };
|
|
540
|
+
const candidates = [];
|
|
541
|
+
for (const directory of uniqueDirectories(destinations)) {
|
|
542
|
+
candidates.push(...(await scanDestination(absoluteRoot, directory, plan.blockers)));
|
|
543
|
+
}
|
|
544
|
+
const byName = new Map();
|
|
545
|
+
for (const candidate of candidates) {
|
|
546
|
+
const list = byName.get(candidate.name);
|
|
547
|
+
if (list === undefined)
|
|
548
|
+
byName.set(candidate.name, [candidate]);
|
|
549
|
+
else
|
|
550
|
+
list.push(candidate);
|
|
551
|
+
}
|
|
552
|
+
const factoryNames = factorySkillNames(factory);
|
|
553
|
+
const incomingNames = new Set(incoming);
|
|
554
|
+
for (const name of [...byName.keys()].sort()) {
|
|
555
|
+
const sources = byName.get(name).map((candidate) => candidate.from).sort();
|
|
556
|
+
const first = sources[0];
|
|
557
|
+
const inFactory = factoryNames.has(name) ||
|
|
558
|
+
(await lstatIfExists(absolute(absoluteRoot, `${FACTORY_SKILLS_DIRECTORY}/${name}`))) !== undefined;
|
|
559
|
+
if (inFactory) {
|
|
560
|
+
plan.blockers.push(factoryClash(name, first));
|
|
561
|
+
continue;
|
|
562
|
+
}
|
|
563
|
+
if (incomingNames.has(name)) {
|
|
564
|
+
plan.blockers.push(incomingClash(name, first));
|
|
565
|
+
continue;
|
|
566
|
+
}
|
|
567
|
+
if (sources.length > 1) {
|
|
568
|
+
let identical = true;
|
|
569
|
+
for (const source of sources.slice(1)) {
|
|
570
|
+
let same;
|
|
571
|
+
try {
|
|
572
|
+
same = await sameTree(absoluteRoot, first, source);
|
|
573
|
+
}
|
|
574
|
+
catch {
|
|
575
|
+
plan.blockers.push(unreadable(source));
|
|
576
|
+
identical = false;
|
|
577
|
+
break;
|
|
578
|
+
}
|
|
579
|
+
if (!same) {
|
|
580
|
+
plan.blockers.push(differs(name, first, source));
|
|
581
|
+
identical = false;
|
|
582
|
+
break;
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
if (!identical)
|
|
586
|
+
continue;
|
|
587
|
+
}
|
|
588
|
+
plan.moves.push({ name, from: sources, to: `${FACTORY_SKILLS_DIRECTORY}/${name}` });
|
|
589
|
+
}
|
|
590
|
+
// The tree check runs once the moves are known, because a link into a
|
|
591
|
+
// sibling skill adopted in the same run has to pass: its target is valid
|
|
592
|
+
// after both moves. Dropping a blocked move removes its directory from the
|
|
593
|
+
// roots the rest may resolve into, so repeat until nothing more is blocked.
|
|
594
|
+
for (;;) {
|
|
595
|
+
const adoptedDirs = plan.moves.flatMap((move) => move.from);
|
|
596
|
+
const blocked = new Map();
|
|
597
|
+
for (const move of plan.moves) {
|
|
598
|
+
const primary = move.from[0];
|
|
599
|
+
const problem = await checkSkillTree(absoluteRoot, move.name, primary, adoptedDirs);
|
|
600
|
+
if (problem !== undefined)
|
|
601
|
+
blocked.set(move, problem);
|
|
602
|
+
}
|
|
603
|
+
if (blocked.size === 0)
|
|
604
|
+
break;
|
|
605
|
+
plan.blockers.push(...blocked.values());
|
|
606
|
+
plan.moves = plan.moves.filter((move) => !blocked.has(move));
|
|
607
|
+
}
|
|
608
|
+
if (plan.moves.length > 0)
|
|
609
|
+
plan.blockers.push(...(await destinationProblems(absoluteRoot)));
|
|
610
|
+
plan.blockers = sortProblems(plan.blockers);
|
|
611
|
+
return plan;
|
|
612
|
+
}
|
|
613
|
+
async function moveDirectory(root, source, target) {
|
|
614
|
+
try {
|
|
615
|
+
await rename(source, target);
|
|
616
|
+
}
|
|
617
|
+
catch (error) {
|
|
618
|
+
if (error.code !== "EXDEV")
|
|
619
|
+
throw error;
|
|
620
|
+
await cp(source, target, { recursive: true, preserveTimestamps: true, verbatimSymlinks: true });
|
|
621
|
+
await rm(source, { recursive: true, force: true });
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
/**
|
|
625
|
+
* Performs the moves in a plan. Each move renames the whole skill directory,
|
|
626
|
+
* including supporting files and modes, then deletes identical duplicates.
|
|
627
|
+
* A plan with blockers is never applied.
|
|
628
|
+
*/
|
|
629
|
+
export async function applyAdoption(plan) {
|
|
630
|
+
assertAdoptionAllowed(plan);
|
|
631
|
+
const unsafe = await destinationProblems(plan.root);
|
|
632
|
+
if (unsafe.length > 0) {
|
|
633
|
+
throw new MetafabError("Adoption cannot write into the factory directory.", {
|
|
634
|
+
exitCode: ExitCode.NotOwned,
|
|
635
|
+
code: "adoption-blocked",
|
|
636
|
+
problems: unsafe,
|
|
637
|
+
});
|
|
638
|
+
}
|
|
639
|
+
for (const move of plan.moves) {
|
|
640
|
+
const primary = move.from[0];
|
|
641
|
+
if (primary === undefined)
|
|
642
|
+
continue;
|
|
643
|
+
const target = absolute(plan.root, move.to);
|
|
644
|
+
if ((await lstatIfExists(target)) !== undefined) {
|
|
645
|
+
throw new MetafabError(`Skill "${move.name}" already exists in .factory/.`, {
|
|
646
|
+
exitCode: ExitCode.NotOwned,
|
|
647
|
+
code: "adoption-blocked",
|
|
648
|
+
problems: [factoryClash(move.name, primary)],
|
|
649
|
+
});
|
|
650
|
+
}
|
|
651
|
+
await mkdir(path.dirname(target), { recursive: true });
|
|
652
|
+
await moveDirectory(plan.root, absolute(plan.root, primary), target);
|
|
653
|
+
for (const duplicate of move.from.slice(1)) {
|
|
654
|
+
await rm(absolute(plan.root, duplicate), { recursive: true, force: true });
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
/**
|
|
659
|
+
* Exit-2 payload for the adoption decision. It names `--adopt` as the flag and
|
|
660
|
+
* lists every move, so an agent or CI job can adopt without a terminal.
|
|
661
|
+
*/
|
|
662
|
+
export function adoptionRequired(plan) {
|
|
663
|
+
return {
|
|
664
|
+
name: "adopt",
|
|
665
|
+
flag: "--adopt",
|
|
666
|
+
description: ADOPTION_DESCRIPTION,
|
|
667
|
+
moves: plan.moves.map((move) => ({ ...move, from: [...move.from] })),
|
|
668
|
+
};
|
|
669
|
+
}
|
|
670
|
+
/** Human-readable list of the planned moves, one per line. */
|
|
671
|
+
export function describeAdoption(plan) {
|
|
672
|
+
return plan.moves
|
|
673
|
+
.map((move) => `${move.name}: ${move.from.join(", ")} -> ${move.to}`)
|
|
674
|
+
.join("\n");
|
|
675
|
+
}
|
|
676
|
+
//# sourceMappingURL=adopt.js.map
|