@okfit/engine 0.15.0 → 0.16.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/config/anchor.js +1 -1
- package/config/layer.js +1 -1
- package/errors.js +81 -6
- package/index.d.ts +288 -7
- package/index.js +7 -3
- package/move/references.js +267 -0
- package/move/run.js +212 -0
- package/package.json +3 -4
- package/render/move.js +52 -0
- package/sync/index.js +4 -3
- package/targets/inside.js +15 -0
- package/targets/resolve.js +146 -0
- package/verify/run.js +63 -27
- package/version.js +1 -1
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
import { DiagnosticRange, Graph } from "@okfit/core";
|
|
2
|
+
import { posix } from "node:path";
|
|
3
|
+
|
|
4
|
+
//#region src/move/references.ts
|
|
5
|
+
const SCHEME = /^[a-z][a-z0-9+.-]+:/i;
|
|
6
|
+
const isUrl = (raw) => raw.includes("://") || SCHEME.test(raw);
|
|
7
|
+
const splitSuffix = (raw) => {
|
|
8
|
+
const at = raw.search(/[?#]/);
|
|
9
|
+
return at < 0 ? [raw, ""] : [raw.slice(0, at), raw.slice(at)];
|
|
10
|
+
};
|
|
11
|
+
const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
12
|
+
/** The frontmatter paths whose values are bundle paths, for this concept. */
|
|
13
|
+
const fieldPaths = (concept, config) => {
|
|
14
|
+
const raw = concept.frontmatter.raw;
|
|
15
|
+
const out = [];
|
|
16
|
+
if (typeof raw.resource === "string") out.push({
|
|
17
|
+
path: ["resource"],
|
|
18
|
+
value: raw.resource,
|
|
19
|
+
field: "resource"
|
|
20
|
+
});
|
|
21
|
+
if (Array.isArray(raw.sources)) raw.sources.forEach((entry, i) => {
|
|
22
|
+
if (isRecord(entry) && typeof entry.resource === "string") out.push({
|
|
23
|
+
path: [
|
|
24
|
+
"sources",
|
|
25
|
+
i,
|
|
26
|
+
"resource"
|
|
27
|
+
],
|
|
28
|
+
value: entry.resource,
|
|
29
|
+
field: "sources.resource"
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
const attested = raw.attested;
|
|
33
|
+
if (isRecord(attested)) {
|
|
34
|
+
if (typeof attested.computation === "string") out.push({
|
|
35
|
+
path: ["attested", "computation"],
|
|
36
|
+
value: attested.computation,
|
|
37
|
+
field: "computation"
|
|
38
|
+
});
|
|
39
|
+
const executor = attested.executor;
|
|
40
|
+
if (isRecord(executor) && typeof executor.resource === "string") out.push({
|
|
41
|
+
path: [
|
|
42
|
+
"attested",
|
|
43
|
+
"executor",
|
|
44
|
+
"resource"
|
|
45
|
+
],
|
|
46
|
+
value: executor.resource,
|
|
47
|
+
field: "executor.resource"
|
|
48
|
+
});
|
|
49
|
+
const attester = attested.attester;
|
|
50
|
+
if (isRecord(attester) && typeof attester.resource === "string") out.push({
|
|
51
|
+
path: [
|
|
52
|
+
"attested",
|
|
53
|
+
"attester",
|
|
54
|
+
"resource"
|
|
55
|
+
],
|
|
56
|
+
value: attester.resource,
|
|
57
|
+
field: "attester.resource"
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
const declared = config.types?.[concept.frontmatter.type]?.fields ?? {};
|
|
61
|
+
for (const [key, declaration] of Object.entries(declared)) {
|
|
62
|
+
if (declaration.kind !== "path" || key === "resource") continue;
|
|
63
|
+
const value = raw[key];
|
|
64
|
+
if (typeof value === "string") out.push({
|
|
65
|
+
path: [key],
|
|
66
|
+
value
|
|
67
|
+
});
|
|
68
|
+
else if (Array.isArray(value)) value.forEach((item, i) => {
|
|
69
|
+
if (typeof item === "string") out.push({
|
|
70
|
+
path: [key, i],
|
|
71
|
+
value: item
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
if (Array.isArray(raw.renders)) raw.renders.forEach((entry, i) => {
|
|
76
|
+
if (isRecord(entry) && typeof entry.path === "string") out.push({
|
|
77
|
+
path: [
|
|
78
|
+
"renders",
|
|
79
|
+
i,
|
|
80
|
+
"path"
|
|
81
|
+
],
|
|
82
|
+
value: entry.path
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
return out;
|
|
86
|
+
};
|
|
87
|
+
const DESTINATION_AFTER = /* @__PURE__ */ new Set([
|
|
88
|
+
")",
|
|
89
|
+
">",
|
|
90
|
+
" ",
|
|
91
|
+
" ",
|
|
92
|
+
"\n"
|
|
93
|
+
]);
|
|
94
|
+
/**
|
|
95
|
+
* The destination of a link, image or definition inside its node's source slice: the first
|
|
96
|
+
* occurrence of `url` right after `](` / `](<` (inline) or after `]:` and whitespace (definition),
|
|
97
|
+
* never the link text or a title.
|
|
98
|
+
*/
|
|
99
|
+
const locateDestination = (slice, url, type) => {
|
|
100
|
+
const opener = type === "definition" ? /\]:[ \t]*\n?[ \t]*<?/y : /\]\([ \t]*\n?[ \t]*<?/y;
|
|
101
|
+
for (let at = slice.indexOf(url); at >= 0; at = slice.indexOf(url, at + 1)) {
|
|
102
|
+
const after = at + url.length >= slice.length ? " " : slice[at + url.length];
|
|
103
|
+
if (!DESTINATION_AFTER.has(after)) continue;
|
|
104
|
+
for (let start = at - 1; start >= 0 && at - start <= 16; start--) {
|
|
105
|
+
if (slice[start] !== "]") continue;
|
|
106
|
+
opener.lastIndex = start;
|
|
107
|
+
const match = opener.exec(slice);
|
|
108
|
+
if (match !== null && start + match[0].length === at) return at;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return -1;
|
|
112
|
+
};
|
|
113
|
+
/**
|
|
114
|
+
* Every rewritable reference in a concept, plus the references that could not be located in their file
|
|
115
|
+
* (`unlocatable` as `file: value` lines, `unlocatableRefs` with the decoded value). Body links, images and definitions with a URL; the path-valued frontmatter fields;
|
|
116
|
+
* every field the concept's type declares with `kind = "path"`; and `renders[].path`.
|
|
117
|
+
*
|
|
118
|
+
* @public
|
|
119
|
+
*/
|
|
120
|
+
const referencesOf = (concept, config) => {
|
|
121
|
+
const source = concept.document.source;
|
|
122
|
+
const references = [];
|
|
123
|
+
const unlocatable = [];
|
|
124
|
+
const unlocatableRefs = [];
|
|
125
|
+
for (const link of concept.document.links) {
|
|
126
|
+
const type = link.node.type;
|
|
127
|
+
if (link.url === void 0 || type !== "link" && type !== "image" && type !== "definition") continue;
|
|
128
|
+
if (isUrl(link.url.trim())) continue;
|
|
129
|
+
const { start, end } = link.node.position;
|
|
130
|
+
const slice = source.slice(start.offset, end.offset);
|
|
131
|
+
const at = locateDestination(slice, link.url, type);
|
|
132
|
+
if (at < 0) {
|
|
133
|
+
unlocatable.push(`${concept.path}: ${link.url}`);
|
|
134
|
+
unlocatableRefs.push({
|
|
135
|
+
entry: `${concept.path}: ${link.url}`,
|
|
136
|
+
raw: link.url
|
|
137
|
+
});
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
references.push({
|
|
141
|
+
file: concept.path,
|
|
142
|
+
offset: start.offset + at,
|
|
143
|
+
length: link.url.length,
|
|
144
|
+
raw: link.url,
|
|
145
|
+
quote: "",
|
|
146
|
+
...at > 0 && slice[at - 1] === "<" ? { angle: true } : {}
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
for (const { path, value, field } of fieldPaths(concept, config)) {
|
|
150
|
+
if (value.trim() === "" || isUrl(value.trim())) continue;
|
|
151
|
+
const range = DiagnosticRange.forFrontmatterPath(concept.document, path);
|
|
152
|
+
const text = range === void 0 ? "" : source.slice(range.offset, range.offset + range.length);
|
|
153
|
+
const first = text[0];
|
|
154
|
+
const quoted = text.length >= 2 && (first === "\"" || first === "'") && text.at(-1) === first;
|
|
155
|
+
const raw = quoted ? text.slice(1, -1) : text;
|
|
156
|
+
if (range === void 0 || raw !== value) {
|
|
157
|
+
unlocatable.push(`${concept.path}: ${value}`);
|
|
158
|
+
unlocatableRefs.push({
|
|
159
|
+
entry: `${concept.path}: ${value}`,
|
|
160
|
+
raw: value,
|
|
161
|
+
...field === void 0 ? {} : { field }
|
|
162
|
+
});
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
references.push({
|
|
166
|
+
file: concept.path,
|
|
167
|
+
offset: range.offset + (quoted ? 1 : 0),
|
|
168
|
+
length: raw.length,
|
|
169
|
+
raw,
|
|
170
|
+
quote: quoted ? first : "",
|
|
171
|
+
...field === void 0 ? {} : { field }
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
return {
|
|
175
|
+
references,
|
|
176
|
+
unlocatable,
|
|
177
|
+
unlocatableRefs
|
|
178
|
+
};
|
|
179
|
+
};
|
|
180
|
+
const stripMd = (path, keep) => keep || !path.endsWith(".md") ? path : path.slice(0, -3);
|
|
181
|
+
/** `to` relative to the bundle-relative directory `fromDir`, where `to` may itself climb out (`../x`). */
|
|
182
|
+
const relativeTo = (fromDir, to) => {
|
|
183
|
+
const from = fromDir === "." || fromDir === "" ? [] : fromDir.split("/");
|
|
184
|
+
const target = to.split("/");
|
|
185
|
+
if (target[0] === "..") return `${"../".repeat(from.length)}${to}`;
|
|
186
|
+
let shared = 0;
|
|
187
|
+
while (shared < from.length && shared < target.length - 1 && from[shared] === target[shared]) shared++;
|
|
188
|
+
return [...Array.from({ length: from.length - shared }, () => ".."), ...target.slice(shared)].join("/");
|
|
189
|
+
};
|
|
190
|
+
const afterMove = /* @__PURE__ */ new WeakMap();
|
|
191
|
+
/** The bundle as it will be once `movedFrom` has become `movedTo`: only its file and concept sets matter. */
|
|
192
|
+
const bundleAfterMove = (bundle, movedFrom, movedTo) => {
|
|
193
|
+
const cached = afterMove.get(bundle)?.get(`${movedFrom}\0${movedTo}`);
|
|
194
|
+
if (cached !== void 0) return cached;
|
|
195
|
+
const fromId = movedFrom.slice(0, -3);
|
|
196
|
+
const concepts = new Map(bundle.concepts);
|
|
197
|
+
const moved = concepts.get(fromId);
|
|
198
|
+
concepts.delete(fromId);
|
|
199
|
+
if (moved !== void 0) concepts.set(movedTo.slice(0, -3), moved);
|
|
200
|
+
const files = [...bundle.files.filter((file) => file !== movedFrom), movedTo];
|
|
201
|
+
const next = {
|
|
202
|
+
...bundle,
|
|
203
|
+
files,
|
|
204
|
+
concepts
|
|
205
|
+
};
|
|
206
|
+
const byMove = afterMove.get(bundle) ?? /* @__PURE__ */ new Map();
|
|
207
|
+
byMove.set(`${movedFrom}\0${movedTo}`, next);
|
|
208
|
+
afterMove.set(bundle, byMove);
|
|
209
|
+
return next;
|
|
210
|
+
};
|
|
211
|
+
/**
|
|
212
|
+
* The new text for one reference after `movedFrom` (a bundle-relative `.md` path) moves to `movedTo`,
|
|
213
|
+
* or `undefined` when the text stays as written. The reference was written in `fromOld`, which is
|
|
214
|
+
* now at `fromNew` (equal unless the file itself is the one that moved). The spelling is kept: a
|
|
215
|
+
* rooted reference stays rooted, a bare one stays bare, `./`, a dropped `.md`, a `#fragment`/`?query`
|
|
216
|
+
* suffix and a trailing `/` survive. The result may hold whitespace even when `angle` is unset (a
|
|
217
|
+
* percent-encoded space decodes); the caller must then refuse rather than write it outside angle brackets.
|
|
218
|
+
*
|
|
219
|
+
* @public
|
|
220
|
+
*/
|
|
221
|
+
const rewriteReference = (raw, options) => {
|
|
222
|
+
const trimmed = raw.trim();
|
|
223
|
+
if (trimmed === "" || isUrl(trimmed)) return void 0;
|
|
224
|
+
if (/\s/.test(trimmed) && !(options.angle === true && options.field === void 0)) return void 0;
|
|
225
|
+
const [pathPart, suffix] = splitSuffix(trimmed);
|
|
226
|
+
if (pathPart === "") return void 0;
|
|
227
|
+
const resolved = Graph.resolve(options.bundle, options.fromOld, trimmed, options.field);
|
|
228
|
+
const explicit = pathPart.startsWith("./") || pathPart.startsWith("../");
|
|
229
|
+
let target;
|
|
230
|
+
let via;
|
|
231
|
+
if (resolved._tag === "node") {
|
|
232
|
+
if (resolved.kind === "missing") return void 0;
|
|
233
|
+
target = resolved.kind === "concept" ? `${resolved.id}.md` : resolved.id;
|
|
234
|
+
via = resolved.via;
|
|
235
|
+
} else if (resolved._tag === "external" && explicit) {
|
|
236
|
+
target = posix.join(posix.dirname(options.fromOld), pathPart);
|
|
237
|
+
via = "file";
|
|
238
|
+
} else return void 0;
|
|
239
|
+
const moved = target === options.movedFrom;
|
|
240
|
+
const next = moved ? options.movedTo : target;
|
|
241
|
+
if (via === "file" && !moved && options.fromNew === options.fromOld) return void 0;
|
|
242
|
+
if (via === "rooted" && !moved) return void 0;
|
|
243
|
+
const keepMd = pathPart.endsWith(".md");
|
|
244
|
+
const directory = pathPart.endsWith("/");
|
|
245
|
+
const stripped = (path) => directory ? `${posix.dirname(path) === "." ? "" : posix.dirname(path)}/` : stripMd(path, keepMd);
|
|
246
|
+
let written;
|
|
247
|
+
if (via === "rooted") written = `/${stripped(next)}`;
|
|
248
|
+
else if (via === "root") {
|
|
249
|
+
const after = bundleAfterMove(options.bundle, options.movedFrom, options.movedTo);
|
|
250
|
+
const bare = stripped(next);
|
|
251
|
+
const lands = (text) => {
|
|
252
|
+
const again = Graph.resolve(after, options.fromNew, `${text}${suffix}`, options.field);
|
|
253
|
+
return again._tag === "node" && again.kind !== "missing" && (again.kind === "concept" ? `${again.id}.md` : again.id) === next;
|
|
254
|
+
};
|
|
255
|
+
written = moved ? lands(bare) ? bare : `/${bare}` : lands(pathPart) ? pathPart : `/${pathPart}`;
|
|
256
|
+
} else {
|
|
257
|
+
const base = directory ? posix.dirname(next) : next;
|
|
258
|
+
let relative = relativeTo(posix.dirname(options.fromNew), base);
|
|
259
|
+
if (directory) relative = `${relative === "" ? "." : relative}/`;
|
|
260
|
+
if (pathPart.startsWith("./") && !relative.startsWith(".")) relative = `./${relative}`;
|
|
261
|
+
written = directory ? relative : stripMd(relative, keepMd);
|
|
262
|
+
}
|
|
263
|
+
return written === pathPart ? void 0 : `${written}${suffix}`;
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
//#endregion
|
|
267
|
+
export { referencesOf, rewriteReference };
|
package/move/run.js
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import { MoveError } from "../errors.js";
|
|
2
|
+
import { referencesOf, rewriteReference } from "./references.js";
|
|
3
|
+
import { writeAtomic } from "../sync/write.js";
|
|
4
|
+
import { relativeInside } from "../targets/inside.js";
|
|
5
|
+
import { Targets } from "../targets/resolve.js";
|
|
6
|
+
import { Bundle, ConceptId } from "@okfit/core";
|
|
7
|
+
import { Effect, FileSystem, Option, Path } from "effect";
|
|
8
|
+
import { Trust } from "@okfit/profiles";
|
|
9
|
+
import { MarkdownEdit } from "@effected/markdown";
|
|
10
|
+
import { posix } from "node:path";
|
|
11
|
+
|
|
12
|
+
//#region src/move/run.ts
|
|
13
|
+
const withMd = (value) => value.endsWith(".md") ? value : `${value}.md`;
|
|
14
|
+
/**
|
|
15
|
+
* The destination's bundle-relative path (R5'): the cwd-relative path (inside the bundle) or the id
|
|
16
|
+
* (a leading `/` tolerated). A destination naming an existing bundle directory, or ending in `/`,
|
|
17
|
+
* means "into that directory, keeping the source's basename". Every candidate must lie inside the
|
|
18
|
+
* bundle; `undefined` when none does.
|
|
19
|
+
*/
|
|
20
|
+
const resolveDestination = Effect.fn("okfit/move/destination")(function* (to, cwd, bundle, sourcePath) {
|
|
21
|
+
const fs = yield* FileSystem.FileSystem;
|
|
22
|
+
const path = yield* Path.Path;
|
|
23
|
+
const realOr = (absolute) => fs.realPath(absolute).pipe(Effect.orElseSucceed(() => absolute));
|
|
24
|
+
/** Real path of the deepest existing ancestor, re-joined with the rest (the file does not exist yet). */
|
|
25
|
+
const realish = (absolute) => Effect.gen(function* () {
|
|
26
|
+
const resolved = yield* fs.realPath(absolute).pipe(Effect.option);
|
|
27
|
+
if (Option.isSome(resolved)) return resolved.value;
|
|
28
|
+
const parent = path.dirname(absolute);
|
|
29
|
+
if (parent === absolute) return absolute;
|
|
30
|
+
return path.join(yield* realish(parent), path.basename(absolute));
|
|
31
|
+
});
|
|
32
|
+
const root = yield* realOr(bundle.root);
|
|
33
|
+
/** Bundle-relative posix path of `absolute` (`""` for the root itself); `undefined` outside the bundle. */
|
|
34
|
+
const inside = (absolute) => relativeInside(path, root, absolute);
|
|
35
|
+
const exists = (absolute) => fs.exists(absolute).pipe(Effect.orElseSucceed(() => false));
|
|
36
|
+
const isDirectory = (absolute) => fs.stat(absolute).pipe(Effect.map((info) => info.type === "Directory"), Effect.orElseSucceed(() => false));
|
|
37
|
+
const trailingSlash = /\/$/.test(to);
|
|
38
|
+
const spelled = trailingSlash ? to.replace(/\/+$/, "") : to;
|
|
39
|
+
const base = posix.basename(sourcePath);
|
|
40
|
+
const underCwd = path.isAbsolute(spelled) ? spelled : path.resolve(cwd, spelled);
|
|
41
|
+
const cwdPath = inside(yield* realish(underCwd));
|
|
42
|
+
const lexicalInside = (base) => {
|
|
43
|
+
const relative = path.relative(base, underCwd);
|
|
44
|
+
return relative !== ".." && !relative.startsWith(`..${path.sep}`) && !path.isAbsolute(relative);
|
|
45
|
+
};
|
|
46
|
+
if (cwdPath === void 0 && (lexicalInside(root) || lexicalInside(bundle.root))) return void 0;
|
|
47
|
+
const idReadable = path.isAbsolute(spelled) ? cwdPath === void 0 && !(yield* exists(path.join(path.parse(spelled).root, spelled.split("/").find((s) => s !== "") ?? ""))) : true;
|
|
48
|
+
const idPath = yield* Effect.gen(function* () {
|
|
49
|
+
if (!idReadable) return void 0;
|
|
50
|
+
const id = Option.getOrUndefined(ConceptId.normalize(spelled));
|
|
51
|
+
if (id === void 0) return void 0;
|
|
52
|
+
return inside(yield* realish(path.join(root, posix.normalize(id))));
|
|
53
|
+
});
|
|
54
|
+
const dirs = [cwdPath, idPath].filter((candidate) => candidate !== void 0);
|
|
55
|
+
const existingDir = yield* Effect.findFirst(dirs, (candidate) => isDirectory(path.join(root, candidate)));
|
|
56
|
+
if (Option.isSome(existingDir)) return posix.join(existingDir.value, base);
|
|
57
|
+
const parentExists = (candidate) => candidate === void 0 ? Effect.succeed(false) : exists(path.join(root, posix.dirname(candidate)));
|
|
58
|
+
if (trailingSlash) {
|
|
59
|
+
const pick = (yield* parentExists(cwdPath === void 0 ? void 0 : `${cwdPath}/x`)) ? cwdPath : (yield* parentExists(idPath === void 0 ? void 0 : `${idPath}/x`)) ? idPath : cwdPath ?? idPath;
|
|
60
|
+
return pick === void 0 ? void 0 : posix.join(pick, base);
|
|
61
|
+
}
|
|
62
|
+
const withExt = (candidate) => candidate === void 0 ? void 0 : withMd(candidate);
|
|
63
|
+
const cwdCandidate = withExt(cwdPath);
|
|
64
|
+
const idCandidate = withExt(idPath);
|
|
65
|
+
if (cwdCandidate === "" || idCandidate === "") return void 0;
|
|
66
|
+
if (yield* parentExists(cwdCandidate)) return cwdCandidate;
|
|
67
|
+
if (yield* parentExists(idCandidate)) return idCandidate;
|
|
68
|
+
return cwdCandidate ?? idCandidate;
|
|
69
|
+
});
|
|
70
|
+
/**
|
|
71
|
+
* Plan moving one concept: resolve both ends, find every reference to it across the bundle (and the
|
|
72
|
+
* moved file's own outbound references), and compute the new text of each file that changes. Pure of
|
|
73
|
+
* writes; refuses with {@link MoveError} when the destination is unusable or any body link cannot be
|
|
74
|
+
* located for rewriting.
|
|
75
|
+
*
|
|
76
|
+
* @public
|
|
77
|
+
*/
|
|
78
|
+
const planMove = Effect.fn("okfit/move/plan")(function* (options) {
|
|
79
|
+
const fs = yield* FileSystem.FileSystem;
|
|
80
|
+
const path = yield* Path.Path;
|
|
81
|
+
const bundle = yield* Bundle.load({ root: options.bundleRoot });
|
|
82
|
+
const [source] = yield* Targets.resolve([options.from], bundle, options.cwd);
|
|
83
|
+
if (source === void 0) return yield* new MoveError({ reason: "same-path" });
|
|
84
|
+
if (source.path === "project.md") return yield* new MoveError({
|
|
85
|
+
reason: "project-file",
|
|
86
|
+
detail: source.path
|
|
87
|
+
});
|
|
88
|
+
if (/\s/.test(options.to)) return yield* new MoveError({
|
|
89
|
+
reason: "invalid-name",
|
|
90
|
+
detail: options.to
|
|
91
|
+
});
|
|
92
|
+
const destination = yield* resolveDestination(options.to, options.cwd, bundle, source.path);
|
|
93
|
+
if (destination === void 0) return yield* new MoveError({
|
|
94
|
+
reason: "outside-bundle",
|
|
95
|
+
detail: options.to
|
|
96
|
+
});
|
|
97
|
+
if (ConceptId.isReservedFile(destination)) return yield* new MoveError({
|
|
98
|
+
reason: "reserved",
|
|
99
|
+
detail: destination
|
|
100
|
+
});
|
|
101
|
+
if (destination === source.path) return yield* new MoveError({ reason: "same-path" });
|
|
102
|
+
const toAbsolute = path.join(bundle.root, destination);
|
|
103
|
+
if (bundle.files.includes(destination) || (yield* fs.exists(toAbsolute).pipe(Effect.orElseSucceed(() => false)))) return yield* new MoveError({
|
|
104
|
+
reason: "destination-exists",
|
|
105
|
+
detail: destination
|
|
106
|
+
});
|
|
107
|
+
const unloaded = bundle.files.filter((file) => file.endsWith(".md") && !ConceptId.isReservedFile(file) && !bundle.concepts.has(file.slice(0, -3)));
|
|
108
|
+
if (unloaded.length > 0) return yield* new MoveError({
|
|
109
|
+
reason: "unloaded-files",
|
|
110
|
+
detail: unloaded.join("\n")
|
|
111
|
+
});
|
|
112
|
+
const found = [...bundle.concepts.values()].map((concept) => ({
|
|
113
|
+
concept,
|
|
114
|
+
...referencesOf(concept, options.config)
|
|
115
|
+
}));
|
|
116
|
+
const unlocatable = found.flatMap(({ concept, unlocatableRefs }) => unlocatableRefs.filter((missing) => concept.path === source.path || rewriteReference(missing.raw, {
|
|
117
|
+
bundle,
|
|
118
|
+
fromOld: concept.path,
|
|
119
|
+
fromNew: concept.path,
|
|
120
|
+
movedFrom: source.path,
|
|
121
|
+
movedTo: destination,
|
|
122
|
+
...missing.field === void 0 ? {} : { field: missing.field }
|
|
123
|
+
}) !== void 0).map((missing) => missing.entry));
|
|
124
|
+
for (const { concept, references } of found) for (const reference of references) {
|
|
125
|
+
const next = rewriteReference(reference.raw, {
|
|
126
|
+
bundle,
|
|
127
|
+
fromOld: concept.path,
|
|
128
|
+
fromNew: concept.path === source.path ? destination : concept.path,
|
|
129
|
+
movedFrom: source.path,
|
|
130
|
+
movedTo: destination,
|
|
131
|
+
...reference.field === void 0 ? {} : { field: reference.field },
|
|
132
|
+
...reference.angle === true ? { angle: true } : {}
|
|
133
|
+
});
|
|
134
|
+
if (next !== void 0 && /\s/.test(next) && reference.angle !== true) unlocatable.push(`${concept.path}: ${reference.raw}`);
|
|
135
|
+
}
|
|
136
|
+
if (unlocatable.length > 0) return yield* new MoveError({
|
|
137
|
+
reason: "unrewritable",
|
|
138
|
+
detail: unlocatable.join("\n")
|
|
139
|
+
});
|
|
140
|
+
const signals = yield* Trust.signals(bundle, options.config, options.now);
|
|
141
|
+
const edits = [];
|
|
142
|
+
const drifts = [];
|
|
143
|
+
for (const { concept, references } of found) {
|
|
144
|
+
const isMoved = concept.path === source.path;
|
|
145
|
+
const fromNew = isMoved ? destination : concept.path;
|
|
146
|
+
const pending = [];
|
|
147
|
+
for (const reference of references) {
|
|
148
|
+
const next = rewriteReference(reference.raw, {
|
|
149
|
+
bundle,
|
|
150
|
+
fromOld: concept.path,
|
|
151
|
+
fromNew,
|
|
152
|
+
movedFrom: source.path,
|
|
153
|
+
movedTo: destination,
|
|
154
|
+
...reference.field === void 0 ? {} : { field: reference.field },
|
|
155
|
+
...reference.angle === true ? { angle: true } : {}
|
|
156
|
+
});
|
|
157
|
+
if (next !== void 0) pending.push(MarkdownEdit.make({
|
|
158
|
+
offset: reference.offset,
|
|
159
|
+
length: reference.length,
|
|
160
|
+
content: next
|
|
161
|
+
}));
|
|
162
|
+
}
|
|
163
|
+
if (pending.length === 0 && !isMoved) continue;
|
|
164
|
+
const text = pending.length === 0 ? concept.document.source : MarkdownEdit.applyAll(concept.document.source, pending);
|
|
165
|
+
edits.push({
|
|
166
|
+
path: fromNew,
|
|
167
|
+
count: pending.length,
|
|
168
|
+
text
|
|
169
|
+
});
|
|
170
|
+
if (pending.length > 0 && signals.get(concept.path.slice(0, -3))?.integrity === "current") drifts.push(concept.path.slice(0, -3));
|
|
171
|
+
}
|
|
172
|
+
edits.sort((a, b) => a.path < b.path ? -1 : a.path > b.path ? 1 : 0);
|
|
173
|
+
drifts.sort();
|
|
174
|
+
return {
|
|
175
|
+
from: source.path,
|
|
176
|
+
to: destination,
|
|
177
|
+
fromPath: path.join(bundle.root, source.path),
|
|
178
|
+
toPath: toAbsolute,
|
|
179
|
+
edits,
|
|
180
|
+
drifts
|
|
181
|
+
};
|
|
182
|
+
});
|
|
183
|
+
/**
|
|
184
|
+
* Plan the move, then (unless `dryRun`) write it: every referrer atomically, the moved file's new
|
|
185
|
+
* text over its old path, then a rename to the destination (its directory created first). Nothing is
|
|
186
|
+
* written until the whole plan succeeded; the writes themselves are not transactional (the rename is last).
|
|
187
|
+
*
|
|
188
|
+
* @public
|
|
189
|
+
*/
|
|
190
|
+
const runMove = Effect.fn("okfit/move/run")(function* (options) {
|
|
191
|
+
const fs = yield* FileSystem.FileSystem;
|
|
192
|
+
const path = yield* Path.Path;
|
|
193
|
+
const plan = yield* planMove(options);
|
|
194
|
+
if (!options.dryRun) {
|
|
195
|
+
yield* fs.makeDirectory(path.dirname(plan.toPath), { recursive: true });
|
|
196
|
+
for (const edit of plan.edits) {
|
|
197
|
+
if (edit.path === plan.to) continue;
|
|
198
|
+
yield* writeAtomic(path.join(options.bundleRoot, edit.path), edit.text);
|
|
199
|
+
}
|
|
200
|
+
const moved = plan.edits.find((edit) => edit.path === plan.to);
|
|
201
|
+
if (moved !== void 0) yield* writeAtomic(plan.fromPath, moved.text);
|
|
202
|
+
yield* fs.rename(plan.fromPath, plan.toPath);
|
|
203
|
+
}
|
|
204
|
+
return {
|
|
205
|
+
plan,
|
|
206
|
+
bundleRoot: options.bundleRoot,
|
|
207
|
+
dryRun: options.dryRun
|
|
208
|
+
};
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
//#endregion
|
|
212
|
+
export { planMove, runMove };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@okfit/engine",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.16.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "The okfit engine: platform layer, config discovery, and the validate, verify, sync, init and context programs shared by the okfit CLI and MCP server.",
|
|
6
6
|
"keywords": [
|
|
@@ -35,7 +35,6 @@
|
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
37
|
"@effect/platform-node": "^4.0.3",
|
|
38
|
-
"@effect/platform-node-shared": "^4.0.3",
|
|
39
38
|
"@effected/app": "^0.22.0",
|
|
40
39
|
"@effected/config-file": "^0.15.0",
|
|
41
40
|
"@effected/engine": "^0.5.0",
|
|
@@ -49,8 +48,8 @@
|
|
|
49
48
|
"@effected/walker": "^0.16.0",
|
|
50
49
|
"@effected/xdg": "^0.10.0",
|
|
51
50
|
"@effected/yaml": "^0.20.0",
|
|
52
|
-
"@okfit/core": "0.
|
|
53
|
-
"@okfit/profiles": "0.10.
|
|
51
|
+
"@okfit/core": "0.11.0",
|
|
52
|
+
"@okfit/profiles": "0.10.1",
|
|
54
53
|
"effect": "^4.0.3"
|
|
55
54
|
},
|
|
56
55
|
"engines": {
|
package/render/move.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { DistributionField } from "./distribution.js";
|
|
2
|
+
import { ENGINE_VERSION } from "../version.js";
|
|
3
|
+
import { Schema } from "effect";
|
|
4
|
+
|
|
5
|
+
//#region src/render/move.ts
|
|
6
|
+
/**
|
|
7
|
+
* `okfit mv`'s success envelope, schema 1, snake_case. `from`/`to` are bundle-relative paths, `path`
|
|
8
|
+
* the display form of the destination; `edits` lists every file that changes (post-move path) with
|
|
9
|
+
* its edit count, and `drifts` the ids whose current human verification the move invalidates.
|
|
10
|
+
*
|
|
11
|
+
* @public
|
|
12
|
+
*/
|
|
13
|
+
const MoveEnvelope = Schema.Struct({
|
|
14
|
+
schema: Schema.Literal(1),
|
|
15
|
+
okfit_version: Schema.String,
|
|
16
|
+
engine_version: Schema.String,
|
|
17
|
+
distribution: DistributionField,
|
|
18
|
+
from: Schema.String,
|
|
19
|
+
to: Schema.String,
|
|
20
|
+
path: Schema.String,
|
|
21
|
+
dry_run: Schema.Boolean,
|
|
22
|
+
edits: Schema.Array(Schema.Struct({
|
|
23
|
+
path: Schema.String,
|
|
24
|
+
count: Schema.Number
|
|
25
|
+
})),
|
|
26
|
+
drifts: Schema.Array(Schema.String),
|
|
27
|
+
exit_code: Schema.Literal(0)
|
|
28
|
+
});
|
|
29
|
+
/**
|
|
30
|
+
* `path` is already the display form, as for the other envelope builders.
|
|
31
|
+
*
|
|
32
|
+
* @public
|
|
33
|
+
*/
|
|
34
|
+
const moveEnvelope = (input) => ({
|
|
35
|
+
schema: 1,
|
|
36
|
+
okfit_version: input.okfitVersion,
|
|
37
|
+
engine_version: ENGINE_VERSION,
|
|
38
|
+
distribution: input.distribution ?? null,
|
|
39
|
+
from: input.from,
|
|
40
|
+
to: input.to,
|
|
41
|
+
path: input.path,
|
|
42
|
+
dry_run: input.dryRun,
|
|
43
|
+
edits: input.edits.map(({ path, count }) => ({
|
|
44
|
+
path,
|
|
45
|
+
count
|
|
46
|
+
})),
|
|
47
|
+
drifts: input.drifts,
|
|
48
|
+
exit_code: 0
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
//#endregion
|
|
52
|
+
export { MoveEnvelope, moveEnvelope };
|
package/sync/index.js
CHANGED
|
@@ -41,8 +41,9 @@ const writeOrCompare = Effect.fn("okfit/sync/index/writeOrCompare")(function* (t
|
|
|
41
41
|
});
|
|
42
42
|
/**
|
|
43
43
|
* Contract §7. Directories: the bundle root plus every directory in
|
|
44
|
-
* `
|
|
45
|
-
*
|
|
44
|
+
* `Derive.indexDirectories` -- each directory holding a concept and its
|
|
45
|
+
* ancestors, which a parent index links to (S-20, amended: an ancestor with
|
|
46
|
+
* no concept of its own still gets its index). A directory with an
|
|
46
47
|
* `index.md` but no concepts is left untouched (design §4: "nothing to
|
|
47
48
|
* derive") because such a directory is never a member of
|
|
48
49
|
* `bundle.directories` in the first place (`Bundle.load` only records a
|
|
@@ -60,7 +61,7 @@ const syncIndex = Effect.fn("okfit/sync/syncIndex")(function* (bundle, dryRun, p
|
|
|
60
61
|
subdirectories: childDirectoriesOf(bundle, "")
|
|
61
62
|
});
|
|
62
63
|
yield* writeOrCompare(path.join(bundle.root, "index.md"), "index.md", rootRendered, dryRun, written, unchanged, pending);
|
|
63
|
-
for (const dir of bundle
|
|
64
|
+
for (const dir of Derive.indexDirectories(bundle)) {
|
|
64
65
|
if (dir === "") continue;
|
|
65
66
|
const rendered = Derive.synthesizeIndex(bundle, dir);
|
|
66
67
|
yield* writeOrCompare(path.join(bundle.root, dir, "index.md"), `${dir}/index.md`, rendered, dryRun, written, unchanged, pending);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
//#region src/targets/inside.ts
|
|
2
|
+
/**
|
|
3
|
+
* The posix path of `absolute` relative to `root` (`""` for the root itself), or `undefined` when it
|
|
4
|
+
* lies outside. Only a `..` segment escapes: a file named `..draft.md` is inside.
|
|
5
|
+
*
|
|
6
|
+
* @internal
|
|
7
|
+
*/
|
|
8
|
+
const relativeInside = (path, root, absolute) => {
|
|
9
|
+
const relative = path.relative(root, absolute);
|
|
10
|
+
if (relative === ".." || relative.startsWith(`..${path.sep}`) || path.isAbsolute(relative)) return void 0;
|
|
11
|
+
return relative.split(path.sep).join("/");
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
//#endregion
|
|
15
|
+
export { relativeInside };
|