@better-fs-tools/write 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +442 -2
- package/dist/authorize/ask-before-write.d.ts +11 -0
- package/dist/authorize/ask-before-write.js +49 -0
- package/dist/authorize/index.d.ts +3 -0
- package/dist/authorize/index.js +3 -0
- package/dist/authorize/protect-paths.d.ts +12 -0
- package/dist/authorize/protect-paths.js +74 -0
- package/dist/authorize/write-authorizers.d.ts +9 -0
- package/dist/authorize/write-authorizers.js +48 -0
- package/dist/bundle/create-fs-tools.d.ts +71 -0
- package/dist/bundle/create-fs-tools.js +125 -0
- package/dist/codecs/index.d.ts +1 -0
- package/dist/codecs/index.js +1 -0
- package/dist/codecs/utf8.d.ts +7 -0
- package/dist/codecs/utf8.js +61 -0
- package/dist/contract/codec.d.ts +26 -0
- package/dist/contract/codec.js +0 -0
- package/dist/contract/context.d.ts +14 -0
- package/dist/contract/context.js +0 -0
- package/dist/contract/deps.d.ts +55 -0
- package/dist/contract/deps.js +0 -0
- package/dist/contract/extensions.d.ts +103 -0
- package/dist/contract/extensions.js +0 -0
- package/dist/contract/format.d.ts +15 -0
- package/dist/contract/format.js +0 -0
- package/dist/contract/input.d.ts +35 -0
- package/dist/contract/input.js +0 -0
- package/dist/contract/limits.d.ts +26 -0
- package/dist/contract/limits.js +0 -0
- package/dist/contract/locks.d.ts +14 -0
- package/dist/contract/locks.js +0 -0
- package/dist/contract/matcher.d.ts +30 -0
- package/dist/contract/matcher.js +0 -0
- package/dist/contract/messages.d.ts +211 -0
- package/dist/contract/messages.js +0 -0
- package/dist/contract/patch.d.ts +50 -0
- package/dist/contract/patch.js +0 -0
- package/dist/contract/preconditions.d.ts +9 -0
- package/dist/contract/preconditions.js +5 -0
- package/dist/contract/result.d.ts +102 -0
- package/dist/contract/result.js +0 -0
- package/dist/core/abort.d.ts +4 -0
- package/dist/core/abort.js +23 -0
- package/dist/core/authorize.d.ts +9 -0
- package/dist/core/authorize.js +87 -0
- package/dist/core/bytes.d.ts +3 -0
- package/dist/core/bytes.js +14 -0
- package/dist/core/commit-patch.d.ts +20 -0
- package/dist/core/commit-patch.js +224 -0
- package/dist/core/commit.d.ts +9 -0
- package/dist/core/commit.js +111 -0
- package/dist/core/create-tools.d.ts +23 -0
- package/dist/core/create-tools.js +54 -0
- package/dist/core/deps.d.ts +11 -0
- package/dist/core/deps.js +207 -0
- package/dist/core/diff.d.ts +10 -0
- package/dist/core/diff.js +221 -0
- package/dist/core/encode.d.ts +3 -0
- package/dist/core/encode.js +26 -0
- package/dist/core/extension-error.d.ts +8 -0
- package/dist/core/extension-error.js +17 -0
- package/dist/core/format.d.ts +4 -0
- package/dist/core/format.js +36 -0
- package/dist/core/guards.d.ts +3 -0
- package/dist/core/guards.js +45 -0
- package/dist/core/hints.d.ts +14 -0
- package/dist/core/hints.js +75 -0
- package/dist/core/hooks.d.ts +15 -0
- package/dist/core/hooks.js +115 -0
- package/dist/core/input.d.ts +20 -0
- package/dist/core/input.js +91 -0
- package/dist/core/limits.d.ts +10 -0
- package/dist/core/limits.js +48 -0
- package/dist/core/line-index.d.ts +10 -0
- package/dist/core/line-index.js +38 -0
- package/dist/core/load.d.ts +14 -0
- package/dist/core/load.js +221 -0
- package/dist/core/lock.d.ts +2 -0
- package/dist/core/lock.js +56 -0
- package/dist/core/match.d.ts +15 -0
- package/dist/core/match.js +66 -0
- package/dist/core/messages.d.ts +8 -0
- package/dist/core/messages.js +147 -0
- package/dist/core/outcomes.d.ts +21 -0
- package/dist/core/outcomes.js +127 -0
- package/dist/core/parse-patch.d.ts +3 -0
- package/dist/core/parse-patch.js +64 -0
- package/dist/core/patch-hunks.d.ts +25 -0
- package/dist/core/patch-hunks.js +177 -0
- package/dist/core/patch-pipeline.d.ts +4 -0
- package/dist/core/patch-pipeline.js +285 -0
- package/dist/core/pipeline.d.ts +22 -0
- package/dist/core/pipeline.js +153 -0
- package/dist/core/plan-edit.d.ts +13 -0
- package/dist/core/plan-edit.js +215 -0
- package/dist/core/plan-patch.d.ts +29 -0
- package/dist/core/plan-patch.js +169 -0
- package/dist/core/plan-write.d.ts +6 -0
- package/dist/core/plan-write.js +27 -0
- package/dist/core/planned.d.ts +35 -0
- package/dist/core/planned.js +36 -0
- package/dist/core/precondition.d.ts +10 -0
- package/dist/core/precondition.js +76 -0
- package/dist/core/record.d.ts +3 -0
- package/dist/core/record.js +58 -0
- package/dist/core/resolve.d.ts +3 -0
- package/dist/core/resolve.js +65 -0
- package/dist/core/scope.d.ts +33 -0
- package/dist/core/scope.js +109 -0
- package/dist/core/snippet.d.ts +4 -0
- package/dist/core/snippet.js +36 -0
- package/dist/core/splice.d.ts +10 -0
- package/dist/core/splice.js +19 -0
- package/dist/core/target.d.ts +10 -0
- package/dist/core/target.js +46 -0
- package/dist/formatters/default.d.ts +16 -0
- package/dist/formatters/default.js +122 -0
- package/dist/formatters/index.d.ts +2 -0
- package/dist/formatters/index.js +1 -0
- package/dist/guards/generated-file.d.ts +10 -0
- package/dist/guards/generated-file.js +39 -0
- package/dist/guards/index.d.ts +13 -0
- package/dist/guards/index.js +25 -0
- package/dist/guards/non-text.d.ts +3 -0
- package/dist/guards/non-text.js +46 -0
- package/dist/guards/omission.d.ts +9 -0
- package/dist/guards/omission.js +48 -0
- package/dist/guards/read-prefix.d.ts +12 -0
- package/dist/guards/read-prefix.js +87 -0
- package/dist/guards/shared.d.ts +11 -0
- package/dist/guards/shared.js +46 -0
- package/dist/guards/syntax.d.ts +10 -0
- package/dist/guards/syntax.js +54 -0
- package/dist/guards/truncation-notice.d.ts +8 -0
- package/dist/guards/truncation-notice.js +32 -0
- package/dist/hooks/executable-shebang.d.ts +9 -0
- package/dist/hooks/executable-shebang.js +46 -0
- package/dist/hooks/index.d.ts +2 -0
- package/dist/hooks/index.js +2 -0
- package/dist/hooks/verify-write.d.ts +9 -0
- package/dist/hooks/verify-write.js +63 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.js +15 -0
- package/dist/locks/index.d.ts +1 -0
- package/dist/locks/index.js +1 -0
- package/dist/locks/memory-locks.d.ts +9 -0
- package/dist/locks/memory-locks.js +94 -0
- package/dist/matchers/block-anchor.d.ts +9 -0
- package/dist/matchers/block-anchor.js +69 -0
- package/dist/matchers/escape.d.ts +6 -0
- package/dist/matchers/escape.js +32 -0
- package/dist/matchers/exact.d.ts +4 -0
- package/dist/matchers/exact.js +29 -0
- package/dist/matchers/fold.d.ts +11 -0
- package/dist/matchers/fold.js +144 -0
- package/dist/matchers/indentation.d.ts +3 -0
- package/dist/matchers/indentation.js +81 -0
- package/dist/matchers/index.d.ts +11 -0
- package/dist/matchers/index.js +18 -0
- package/dist/matchers/line-trimmed.d.ts +3 -0
- package/dist/matchers/line-trimmed.js +32 -0
- package/dist/matchers/lines.d.ts +24 -0
- package/dist/matchers/lines.js +74 -0
- package/dist/matchers/normalized.d.ts +7 -0
- package/dist/matchers/normalized.js +69 -0
- package/dist/patch/grammar.d.ts +5 -0
- package/dist/patch/grammar.js +24 -0
- package/dist/patch/index.d.ts +4 -0
- package/dist/patch/index.js +3 -0
- package/dist/patch/parse.d.ts +11 -0
- package/dist/patch/parse.js +209 -0
- package/dist/signature/contract.d.ts +8 -0
- package/dist/signature/contract.js +0 -0
- package/dist/signature/edit.d.ts +19 -0
- package/dist/signature/edit.js +158 -0
- package/dist/signature/index.d.ts +6 -0
- package/dist/signature/index.js +4 -0
- package/dist/signature/matching.d.ts +3 -0
- package/dist/signature/matching.js +25 -0
- package/dist/signature/messages.d.ts +4 -0
- package/dist/signature/messages.js +9 -0
- package/dist/signature/patch.d.ts +6 -0
- package/dist/signature/patch.js +55 -0
- package/dist/signature/schema.d.ts +19 -0
- package/dist/signature/schema.js +117 -0
- package/dist/signature/write.d.ts +9 -0
- package/dist/signature/write.js +52 -0
- package/dist/state/index.d.ts +2 -0
- package/dist/state/index.js +1 -0
- package/dist/state/invalidate.d.ts +29 -0
- package/dist/state/invalidate.js +37 -0
- package/docs/architecture.md +221 -0
- package/docs/hosts.md +95 -0
- package/docs/result-schema.md +268 -0
- package/package.json +54 -4
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import { checkBeforeCommit, fileChange, isMutatedFile } from "./commit.js";
|
|
2
|
+
import { isRecord } from "./input.js";
|
|
3
|
+
import { WriteStop, codeForReason, errorNote, failure, isBackendError } from "./outcomes.js";
|
|
4
|
+
import { messageOf } from "./outcomes.js";
|
|
5
|
+
import { ioFailure } from "./target.js";
|
|
6
|
+
export async function commitPatch(scope, fs, steps, files) {
|
|
7
|
+
scope.enter("commit");
|
|
8
|
+
scope.checkAbort();
|
|
9
|
+
scope.startCommit();
|
|
10
|
+
const staged = await stageAll(scope, fs, steps);
|
|
11
|
+
const journal = [];
|
|
12
|
+
for (const [index, step] of steps.entries()) {
|
|
13
|
+
const failed = await publishStep(scope, fs, step, staged[index] ?? null, journal);
|
|
14
|
+
if (failed === null)
|
|
15
|
+
continue;
|
|
16
|
+
await discardAll(staged);
|
|
17
|
+
throw await rollback(scope, fs, journal, failed, files);
|
|
18
|
+
}
|
|
19
|
+
return steps.map((step) => {
|
|
20
|
+
const entry = journal.find((item) => item.step === step && item.kind === "written");
|
|
21
|
+
return { step, file: entry?.kind === "written" ? entry.file : null };
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
async function stageAll(scope, fs, steps) {
|
|
25
|
+
const staged = steps.map(() => null);
|
|
26
|
+
if (typeof fs.stage !== "function")
|
|
27
|
+
return staged;
|
|
28
|
+
for (const [index, step] of steps.entries()) {
|
|
29
|
+
if (step.bytes === null)
|
|
30
|
+
continue;
|
|
31
|
+
const { target } = step.change.planned;
|
|
32
|
+
let outcome;
|
|
33
|
+
try {
|
|
34
|
+
outcome = await fs.stage(target.resolvedPath, step.bytes, writeOptions(step));
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
await discardAll(staged);
|
|
38
|
+
throw ioFailure(scope, target.requestedPath, messageOf(error));
|
|
39
|
+
}
|
|
40
|
+
if (isRecord(outcome) && outcome.ok === false && isBackendError(outcome.error)) {
|
|
41
|
+
await discardAll(staged);
|
|
42
|
+
throw scope.backendFailure(target.requestedPath, outcome.error);
|
|
43
|
+
}
|
|
44
|
+
if (!isRecord(outcome) || outcome.ok !== true || !isStaged(outcome.staged)) {
|
|
45
|
+
await discardAll(staged);
|
|
46
|
+
throw ioFailure(scope, target.requestedPath, "malformed stage outcome");
|
|
47
|
+
}
|
|
48
|
+
staged[index] = outcome.staged;
|
|
49
|
+
}
|
|
50
|
+
return staged;
|
|
51
|
+
}
|
|
52
|
+
async function publishStep(scope, fs, step, staged, journal) {
|
|
53
|
+
const { planned, source } = step.change;
|
|
54
|
+
const { target, precondition } = planned;
|
|
55
|
+
const bytes = step.bytes;
|
|
56
|
+
if (bytes !== null) {
|
|
57
|
+
const written = await attempt(scope, fs, target, precondition, () => staged === null ? fs.write(target.resolvedPath, bytes, writeOptions(step)) : staged.publish());
|
|
58
|
+
if (!written.ok)
|
|
59
|
+
return { target, code: written.code };
|
|
60
|
+
const created = precondition.kind === "absent";
|
|
61
|
+
journal.push({ kind: "written", step, file: written.file, created });
|
|
62
|
+
}
|
|
63
|
+
const removed = source ?? (planned.change.kind === "delete" ? target : null);
|
|
64
|
+
if (removed === null)
|
|
65
|
+
return null;
|
|
66
|
+
const loaded = planned.loaded;
|
|
67
|
+
const version = { kind: "version", version: loaded.version };
|
|
68
|
+
const outcome = await attemptRemove(scope, fs, removed, version);
|
|
69
|
+
if (!outcome.ok)
|
|
70
|
+
return { target: removed, code: outcome.code };
|
|
71
|
+
journal.push({ kind: "removed", step, target: removed, loaded });
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
async function rollback(scope, fs, journal, failed, files) {
|
|
75
|
+
const states = new Map();
|
|
76
|
+
states.set(failed.target.resolvedPath, { state: "unchanged", code: failed.code });
|
|
77
|
+
const left = [];
|
|
78
|
+
for (const entry of [...journal].reverse()) {
|
|
79
|
+
const outcome = await undo(scope, fs, entry);
|
|
80
|
+
const key = entry.kind === "written"
|
|
81
|
+
? entry.step.change.planned.target.resolvedPath
|
|
82
|
+
: entry.target.resolvedPath;
|
|
83
|
+
if (outcome.ok) {
|
|
84
|
+
states.set(key, { state: "restored" });
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
states.set(key, { state: "rollback-failed", code: outcome.code });
|
|
88
|
+
left.unshift(changeLeftAfterFailedRollback(scope, entry));
|
|
89
|
+
}
|
|
90
|
+
const rolledBack = left.length === 0;
|
|
91
|
+
const report = files.map((file) => ({
|
|
92
|
+
path: file.path,
|
|
93
|
+
...(states.get(file.resolvedPath) ?? { state: "unchanged" }),
|
|
94
|
+
}));
|
|
95
|
+
const { messages } = scope.deps;
|
|
96
|
+
const path = failed.target.requestedPath;
|
|
97
|
+
const code = rolledBack ? failed.code : "PARTIAL_COMMIT";
|
|
98
|
+
const message = messages.patchCommitFailed({
|
|
99
|
+
path,
|
|
100
|
+
code: failed.code,
|
|
101
|
+
rolledBack,
|
|
102
|
+
files: report,
|
|
103
|
+
});
|
|
104
|
+
const data = rolledBack
|
|
105
|
+
? { path, rolledBack }
|
|
106
|
+
: { path, cause: failed.code, rolledBack };
|
|
107
|
+
const base = failure(scope.tool, "commit", errorNote(code, message, data));
|
|
108
|
+
return new WriteStop({
|
|
109
|
+
...base,
|
|
110
|
+
changes: left,
|
|
111
|
+
commit: { rolledBack, files: report },
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
async function undo(scope, fs, entry) {
|
|
115
|
+
if (entry.kind === "removed") {
|
|
116
|
+
const { target, loaded } = entry;
|
|
117
|
+
const options = {
|
|
118
|
+
precondition: { kind: "absent" },
|
|
119
|
+
createParents: true,
|
|
120
|
+
...(loaded.mode === null ? {} : { mode: loaded.mode }),
|
|
121
|
+
};
|
|
122
|
+
return attempt(scope, fs, target, options.precondition, () => fs.write(target.resolvedPath, loaded.bytes, options));
|
|
123
|
+
}
|
|
124
|
+
const { planned } = entry.step.change;
|
|
125
|
+
const { target } = planned;
|
|
126
|
+
const after = entry.file.version === null
|
|
127
|
+
? { kind: "any" }
|
|
128
|
+
: { kind: "version", version: entry.file.version };
|
|
129
|
+
if (entry.created)
|
|
130
|
+
return attemptRemove(scope, fs, target, after);
|
|
131
|
+
const original = planned.loaded.bytes;
|
|
132
|
+
return attempt(scope, fs, target, after, () => fs.write(target.resolvedPath, original, { precondition: after, createParents: false }));
|
|
133
|
+
}
|
|
134
|
+
function attemptRemove(scope, fs, target, precondition) {
|
|
135
|
+
const remove = fs.remove;
|
|
136
|
+
if (typeof remove !== "function") {
|
|
137
|
+
return Promise.resolve({ ok: false, code: "UNSUPPORTED_BACKEND" });
|
|
138
|
+
}
|
|
139
|
+
return attempt(scope, fs, target, precondition, () => remove.call(fs, target.resolvedPath, { precondition }));
|
|
140
|
+
}
|
|
141
|
+
async function attempt(scope, fs, target, precondition, run) {
|
|
142
|
+
if (!fs.writeCapabilities.compareAndSwap && precondition.kind !== "any") {
|
|
143
|
+
try {
|
|
144
|
+
await checkBeforeCommit(scope, fs, target, precondition);
|
|
145
|
+
}
|
|
146
|
+
catch (error) {
|
|
147
|
+
if (error instanceof WriteStop)
|
|
148
|
+
return { ok: false, code: error.report.error.code };
|
|
149
|
+
return { ok: false, code: "IO_ERROR" };
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
let outcome;
|
|
153
|
+
try {
|
|
154
|
+
outcome = await run();
|
|
155
|
+
}
|
|
156
|
+
catch {
|
|
157
|
+
return { ok: false, code: "IO_ERROR" };
|
|
158
|
+
}
|
|
159
|
+
if (isRecord(outcome) && outcome.ok === false && isBackendError(outcome.error)) {
|
|
160
|
+
return { ok: false, code: codeForReason(outcome.error.reason) };
|
|
161
|
+
}
|
|
162
|
+
if (!isRecord(outcome) || outcome.ok !== true || !isMutatedFile(outcome.file)) {
|
|
163
|
+
return { ok: false, code: "IO_ERROR" };
|
|
164
|
+
}
|
|
165
|
+
return { ok: true, file: outcome.file };
|
|
166
|
+
}
|
|
167
|
+
function changeLeftAfterFailedRollback(scope, entry) {
|
|
168
|
+
const { planned } = entry.step.change;
|
|
169
|
+
if (entry.kind === "removed")
|
|
170
|
+
return deleteChange(planned, entry.target);
|
|
171
|
+
const change = fileChange(scope, planned, entry.step.bytes, entry.file);
|
|
172
|
+
// Its move source was restored or never removed, so the destination is a create.
|
|
173
|
+
return planned.change.kind === "move" ? { ...change, kind: "create", before: null } : change;
|
|
174
|
+
}
|
|
175
|
+
export function deleteChange(planned, target) {
|
|
176
|
+
const loaded = planned.loaded;
|
|
177
|
+
const isDelete = planned.change.kind === "delete";
|
|
178
|
+
return {
|
|
179
|
+
kind: "delete",
|
|
180
|
+
path: target.displayPath,
|
|
181
|
+
requestedPath: target.requestedPath,
|
|
182
|
+
resolvedPath: target.resolvedPath,
|
|
183
|
+
movedFrom: null,
|
|
184
|
+
before: {
|
|
185
|
+
contentId: loaded.contentId,
|
|
186
|
+
version: loaded.version,
|
|
187
|
+
bytes: loaded.bytes.byteLength,
|
|
188
|
+
},
|
|
189
|
+
after: null,
|
|
190
|
+
linesAdded: 0,
|
|
191
|
+
linesRemoved: isDelete ? planned.change.linesRemoved : lineCount(loaded.text),
|
|
192
|
+
diff: isDelete ? planned.change.diff : "",
|
|
193
|
+
diffTruncated: isDelete && planned.diffTruncated,
|
|
194
|
+
matches: [],
|
|
195
|
+
snippets: [],
|
|
196
|
+
userModified: false,
|
|
197
|
+
createdDirectories: [],
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
function lineCount(text) {
|
|
201
|
+
if (text === "")
|
|
202
|
+
return 0;
|
|
203
|
+
const breaks = text.split("\n").length - 1;
|
|
204
|
+
return text.endsWith("\n") ? breaks : breaks + 1;
|
|
205
|
+
}
|
|
206
|
+
function writeOptions(step) {
|
|
207
|
+
const { precondition, createParents } = step.change.planned;
|
|
208
|
+
return { precondition, createParents, ...(step.mode === null ? {} : { mode: step.mode }) };
|
|
209
|
+
}
|
|
210
|
+
async function discardAll(staged) {
|
|
211
|
+
for (const write of staged) {
|
|
212
|
+
if (write === null)
|
|
213
|
+
continue;
|
|
214
|
+
try {
|
|
215
|
+
await write.discard();
|
|
216
|
+
}
|
|
217
|
+
catch {
|
|
218
|
+
// A leftover staged file cannot change a target.
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
function isStaged(value) {
|
|
223
|
+
return (isRecord(value) && typeof value.publish === "function" && typeof value.discard === "function");
|
|
224
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { MutatedFile, Precondition, WritableFileSystem } from "@better-fs-tools/fs";
|
|
2
|
+
import type { FileChange } from "../contract/result.ts";
|
|
3
|
+
import type { Planned, ResolvedTarget } from "./planned.ts";
|
|
4
|
+
import type { MutationScope } from "./scope.ts";
|
|
5
|
+
export declare function commitOne<THost>(scope: MutationScope<THost>, fs: WritableFileSystem, planned: Planned, bytes: Uint8Array): Promise<MutatedFile>;
|
|
6
|
+
export declare function checkBeforeCommit<THost>(scope: MutationScope<THost>, fs: WritableFileSystem, target: ResolvedTarget, precondition: Precondition): Promise<void>;
|
|
7
|
+
export declare function addCapabilityNotes<THost>(scope: MutationScope<THost>, fs: WritableFileSystem, planned: Planned, file: MutatedFile): void;
|
|
8
|
+
export declare function fileChange<THost>(scope: MutationScope<THost>, planned: Planned, bytes: Uint8Array, file: MutatedFile): FileChange;
|
|
9
|
+
export declare function isMutatedFile(value: unknown): value is MutatedFile;
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { hashBytes } from "./bytes.js";
|
|
2
|
+
import { newFileMode } from "./hooks.js";
|
|
3
|
+
import { isRecord } from "./input.js";
|
|
4
|
+
import { isBackendError, messageOf } from "./outcomes.js";
|
|
5
|
+
import { ioFailure, statTarget } from "./target.js";
|
|
6
|
+
export async function commitOne(scope, fs, planned, bytes) {
|
|
7
|
+
scope.enter("commit");
|
|
8
|
+
scope.checkAbort();
|
|
9
|
+
const { target, precondition, createParents } = planned;
|
|
10
|
+
if (!fs.writeCapabilities.compareAndSwap) {
|
|
11
|
+
await checkBeforeCommit(scope, fs, target, precondition);
|
|
12
|
+
}
|
|
13
|
+
const mode = planned.loaded === null ? newFileMode(scope, planned.change) : null;
|
|
14
|
+
scope.startCommit();
|
|
15
|
+
let outcome;
|
|
16
|
+
try {
|
|
17
|
+
outcome = await fs.write(target.resolvedPath, bytes, {
|
|
18
|
+
precondition,
|
|
19
|
+
createParents,
|
|
20
|
+
...(mode === null ? {} : { mode }),
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
catch (error) {
|
|
24
|
+
throw ioFailure(scope, target.requestedPath, messageOf(error));
|
|
25
|
+
}
|
|
26
|
+
if (isRecord(outcome) && outcome.ok === false && isBackendError(outcome.error)) {
|
|
27
|
+
throw scope.backendFailure(target.requestedPath, outcome.error);
|
|
28
|
+
}
|
|
29
|
+
if (!isRecord(outcome) || outcome.ok !== true || !isMutatedFile(outcome.file)) {
|
|
30
|
+
throw ioFailure(scope, target.requestedPath, "malformed write outcome");
|
|
31
|
+
}
|
|
32
|
+
addCapabilityNotes(scope, fs, planned, outcome.file);
|
|
33
|
+
return outcome.file;
|
|
34
|
+
}
|
|
35
|
+
export async function checkBeforeCommit(scope, fs, target, precondition) {
|
|
36
|
+
const stat = await statTarget(scope, fs, { requestedPath: target.requestedPath, path: target.resolvedPath }, "commit");
|
|
37
|
+
const { messages } = scope.deps;
|
|
38
|
+
const path = target.requestedPath;
|
|
39
|
+
if (precondition.kind === "absent" && stat.exists) {
|
|
40
|
+
throw scope.stop("EXISTS", messages.exists({ tool: scope.tool, path }));
|
|
41
|
+
}
|
|
42
|
+
if (precondition.kind === "version" && (!stat.exists || stat.version !== precondition.version)) {
|
|
43
|
+
throw scope.stop("STALE", messages.stale({ tool: scope.tool, path }));
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
export function addCapabilityNotes(scope, fs, planned, file) {
|
|
47
|
+
const { messages } = scope.deps;
|
|
48
|
+
const capabilities = fs.writeCapabilities;
|
|
49
|
+
const backend = fs.id;
|
|
50
|
+
const add = (note) => {
|
|
51
|
+
if (!scope.notes.some((existing) => existing.code === note.code))
|
|
52
|
+
scope.notes.push(note);
|
|
53
|
+
};
|
|
54
|
+
if (!capabilities.atomic || !file.atomic) {
|
|
55
|
+
add({ code: "not-atomic", severity: "warning", message: messages.notAtomic({ backend }) });
|
|
56
|
+
}
|
|
57
|
+
if (!capabilities.compareAndSwap) {
|
|
58
|
+
add({
|
|
59
|
+
code: "no-compare-and-swap",
|
|
60
|
+
severity: "info",
|
|
61
|
+
message: messages.noCompareAndSwap({ backend }),
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
const replacesExistingFile = planned.precondition.kind !== "absent";
|
|
65
|
+
if (!capabilities.preserveMode && replacesExistingFile) {
|
|
66
|
+
add({ code: "mode-not-kept", severity: "info", message: messages.modeNotKept({ backend }) });
|
|
67
|
+
}
|
|
68
|
+
if (file.createdDirectories.length > 0) {
|
|
69
|
+
const paths = file.createdDirectories;
|
|
70
|
+
scope.notes.push({
|
|
71
|
+
code: "directories-created",
|
|
72
|
+
severity: "info",
|
|
73
|
+
message: messages.directoriesCreated({ paths }),
|
|
74
|
+
data: { paths: [...paths] },
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
export function fileChange(scope, planned, bytes, file) {
|
|
79
|
+
const { change, loaded, target } = planned;
|
|
80
|
+
const { digest } = scope.deps;
|
|
81
|
+
return {
|
|
82
|
+
kind: change.kind,
|
|
83
|
+
path: target.displayPath,
|
|
84
|
+
requestedPath: target.requestedPath,
|
|
85
|
+
resolvedPath: target.resolvedPath,
|
|
86
|
+
movedFrom: null,
|
|
87
|
+
before: loaded === null
|
|
88
|
+
? null
|
|
89
|
+
: { contentId: loaded.contentId, version: loaded.version, bytes: loaded.bytes.byteLength },
|
|
90
|
+
after: {
|
|
91
|
+
contentId: digest === null ? null : hashBytes(digest, bytes),
|
|
92
|
+
version: file.version,
|
|
93
|
+
bytes: bytes.byteLength,
|
|
94
|
+
},
|
|
95
|
+
linesAdded: change.linesAdded,
|
|
96
|
+
linesRemoved: change.linesRemoved,
|
|
97
|
+
diff: change.diff,
|
|
98
|
+
diffTruncated: planned.diffTruncated,
|
|
99
|
+
matches: planned.matches,
|
|
100
|
+
snippets: planned.snippets,
|
|
101
|
+
userModified: planned.userModified,
|
|
102
|
+
createdDirectories: [...file.createdDirectories],
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
export function isMutatedFile(value) {
|
|
106
|
+
return (isRecord(value) &&
|
|
107
|
+
typeof value.resolvedPath === "string" &&
|
|
108
|
+
(value.version === null || typeof value.version === "string") &&
|
|
109
|
+
Array.isArray(value.createdDirectories) &&
|
|
110
|
+
typeof value.atomic === "boolean");
|
|
111
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { ApplyPatchTool, EditTool, WriteTool } from "../contract/context.ts";
|
|
2
|
+
import type { StateNeedsDigest } from "@better-fs-tools/read";
|
|
3
|
+
import type { ApplyPatchToolDeps, EditToolDeps, WriteToolDeps } from "../contract/deps.ts";
|
|
4
|
+
/**
|
|
5
|
+
* Creates the edit tool. Takes the same dependencies as createWriteTool, plus
|
|
6
|
+
* a non-empty `matchers` list (default defaultEditMatchers()). Throws
|
|
7
|
+
* TypeError on invalid dependencies.
|
|
8
|
+
*/
|
|
9
|
+
export declare function createEditTool<THost = undefined>(deps: EditToolDeps<THost> & StateNeedsDigest): EditTool<THost>;
|
|
10
|
+
/**
|
|
11
|
+
* Creates the write tool. Throws TypeError on an unknown key, a missing fs,
|
|
12
|
+
* an empty classifier or codec list, state without digest, or a malformed
|
|
13
|
+
* limit, message, or policy. limits, messages, and preconditions merge with
|
|
14
|
+
* the defaults key by key. Every other dependency replaces its default.
|
|
15
|
+
*/
|
|
16
|
+
export declare function createWriteTool<THost = undefined>(deps: WriteToolDeps<THost> & StateNeedsDigest): WriteTool<THost>;
|
|
17
|
+
/**
|
|
18
|
+
* Creates the apply_patch tool. Takes the same dependencies as
|
|
19
|
+
* createWriteTool, plus a non-empty `matchers` list (default
|
|
20
|
+
* defaultPatchMatchers()) and a `patchParser` (default codexPatchParser()).
|
|
21
|
+
* Throws TypeError on invalid dependencies.
|
|
22
|
+
*/
|
|
23
|
+
export declare function createApplyPatchTool<THost = undefined>(deps: ApplyPatchToolDeps<THost> & StateNeedsDigest): ApplyPatchTool<THost>;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { resolveApplyPatchDependencies, resolveEditDependencies, resolveWriteDependencies, } from "./deps.js";
|
|
2
|
+
import { MissCounter } from "./hints.js";
|
|
3
|
+
import { isRecord } from "./input.js";
|
|
4
|
+
import { runApplyPatch } from "./patch-pipeline.js";
|
|
5
|
+
import { runEdit, runWrite } from "./pipeline.js";
|
|
6
|
+
/**
|
|
7
|
+
* Creates the edit tool. Takes the same dependencies as createWriteTool, plus
|
|
8
|
+
* a non-empty `matchers` list (default defaultEditMatchers()). Throws
|
|
9
|
+
* TypeError on invalid dependencies.
|
|
10
|
+
*/
|
|
11
|
+
export function createEditTool(deps) {
|
|
12
|
+
const resolved = resolveEditDependencies(deps);
|
|
13
|
+
const misses = new MissCounter();
|
|
14
|
+
const edit = async (input, ctx) => {
|
|
15
|
+
if (ctx !== undefined && !isRecord(ctx))
|
|
16
|
+
throw new TypeError("edit context must be an object");
|
|
17
|
+
const call = ctx ?? {};
|
|
18
|
+
return runEdit(resolved, input, call, misses);
|
|
19
|
+
};
|
|
20
|
+
return edit;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Creates the write tool. Throws TypeError on an unknown key, a missing fs,
|
|
24
|
+
* an empty classifier or codec list, state without digest, or a malformed
|
|
25
|
+
* limit, message, or policy. limits, messages, and preconditions merge with
|
|
26
|
+
* the defaults key by key. Every other dependency replaces its default.
|
|
27
|
+
*/
|
|
28
|
+
export function createWriteTool(deps) {
|
|
29
|
+
const resolved = resolveWriteDependencies(deps, "write");
|
|
30
|
+
const write = async (input, ctx) => {
|
|
31
|
+
if (ctx !== undefined && !isRecord(ctx))
|
|
32
|
+
throw new TypeError("write context must be an object");
|
|
33
|
+
const call = ctx ?? {};
|
|
34
|
+
return runWrite(resolved, input, call);
|
|
35
|
+
};
|
|
36
|
+
return write;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Creates the apply_patch tool. Takes the same dependencies as
|
|
40
|
+
* createWriteTool, plus a non-empty `matchers` list (default
|
|
41
|
+
* defaultPatchMatchers()) and a `patchParser` (default codexPatchParser()).
|
|
42
|
+
* Throws TypeError on invalid dependencies.
|
|
43
|
+
*/
|
|
44
|
+
export function createApplyPatchTool(deps) {
|
|
45
|
+
const resolved = resolveApplyPatchDependencies(deps);
|
|
46
|
+
const applyPatch = async (input, ctx) => {
|
|
47
|
+
if (ctx !== undefined && !isRecord(ctx)) {
|
|
48
|
+
throw new TypeError("apply_patch context must be an object");
|
|
49
|
+
}
|
|
50
|
+
const call = ctx ?? {};
|
|
51
|
+
return runApplyPatch(resolved, input, call);
|
|
52
|
+
};
|
|
53
|
+
return applyPatch;
|
|
54
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { WritableFileSystem } from "@better-fs-tools/fs";
|
|
2
|
+
import type { ReadStateStore } from "@better-fs-tools/read";
|
|
3
|
+
import type { ApplyPatchDependencies, ApplyPatchToolDeps, EditDependencies, EditToolDeps, WriteDependencies, WriteToolDeps } from "../contract/deps.ts";
|
|
4
|
+
import type { PreconditionPolicy } from "../contract/preconditions.ts";
|
|
5
|
+
export declare function resolveWriteDependencies<THost>(deps: WriteToolDeps<THost>, tool: string): WriteDependencies<THost>;
|
|
6
|
+
export declare function resolveEditDependencies<THost>(deps: EditToolDeps<THost>): EditDependencies<THost>;
|
|
7
|
+
export declare function resolveApplyPatchDependencies<THost>(deps: ApplyPatchToolDeps<THost>): ApplyPatchDependencies<THost>;
|
|
8
|
+
export declare function resolvePreconditions(overrides?: Partial<PreconditionPolicy>): Readonly<PreconditionPolicy>;
|
|
9
|
+
export declare function isWritable(value: unknown): value is WritableFileSystem;
|
|
10
|
+
export declare function isFileSystem(value: unknown): boolean;
|
|
11
|
+
export declare function isStateStore(value: unknown): value is ReadStateStore;
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import { isWritableFileSystem } from "@better-fs-tools/fs";
|
|
2
|
+
import { defaultClassifiers } from "@better-fs-tools/read";
|
|
3
|
+
import { utf8Codec } from "../codecs/utf8.js";
|
|
4
|
+
import { defaultPreconditions } from "../contract/preconditions.js";
|
|
5
|
+
import { defaultWriteFormatter } from "../formatters/default.js";
|
|
6
|
+
import { defaultGuards } from "../guards/index.js";
|
|
7
|
+
import { memoryLocks } from "../locks/memory-locks.js";
|
|
8
|
+
import { defaultEditMatchers, defaultPatchMatchers } from "../matchers/index.js";
|
|
9
|
+
import { codexPatchParser } from "../patch/parse.js";
|
|
10
|
+
import { isRecord } from "./input.js";
|
|
11
|
+
import { resolveWriteLimits } from "./limits.js";
|
|
12
|
+
import { resolveWriteMessages } from "./messages.js";
|
|
13
|
+
const KNOWN = new Set([
|
|
14
|
+
"fs",
|
|
15
|
+
"limits",
|
|
16
|
+
"messages",
|
|
17
|
+
"resolve",
|
|
18
|
+
"authorize",
|
|
19
|
+
"preconditions",
|
|
20
|
+
"state",
|
|
21
|
+
"digest",
|
|
22
|
+
"clock",
|
|
23
|
+
"locks",
|
|
24
|
+
"classifiers",
|
|
25
|
+
"codecs",
|
|
26
|
+
"guards",
|
|
27
|
+
"hooks",
|
|
28
|
+
"formatter",
|
|
29
|
+
]);
|
|
30
|
+
export function resolveWriteDependencies(deps, tool) {
|
|
31
|
+
if (!isRecord(deps))
|
|
32
|
+
throw new TypeError(`${tool} tool dependencies must be an object`);
|
|
33
|
+
for (const key of Object.keys(deps)) {
|
|
34
|
+
if (!KNOWN.has(key))
|
|
35
|
+
throw new TypeError(`Unknown ${tool} tool dependency: ${key}`);
|
|
36
|
+
}
|
|
37
|
+
const { fs } = deps;
|
|
38
|
+
if (typeof fs !== "function" && !isWritable(fs)) {
|
|
39
|
+
throw new TypeError(isFileSystem(fs)
|
|
40
|
+
? "fs has no write methods: a write tool needs a WritableFileSystem"
|
|
41
|
+
: "fs must be a WritableFileSystem or a function that returns one");
|
|
42
|
+
}
|
|
43
|
+
const resolve = deps.resolve ?? null;
|
|
44
|
+
if (resolve !== null && (!isRecord(resolve) || typeof resolve.resolve !== "function")) {
|
|
45
|
+
throw new TypeError("resolve must be a path resolver or null");
|
|
46
|
+
}
|
|
47
|
+
const authorize = deps.authorize ?? null;
|
|
48
|
+
if (authorize !== null && (!isRecord(authorize) || typeof authorize.authorize !== "function")) {
|
|
49
|
+
throw new TypeError("authorize must be an authorizer or null");
|
|
50
|
+
}
|
|
51
|
+
const state = deps.state ?? null;
|
|
52
|
+
if (state !== null && typeof state !== "function" && !isStateStore(state)) {
|
|
53
|
+
throw new TypeError("state must be a store, a function that returns one, or null");
|
|
54
|
+
}
|
|
55
|
+
const digest = deps.digest ?? null;
|
|
56
|
+
if (digest !== null && !isDigest(digest)) {
|
|
57
|
+
throw new TypeError("digest must have an id, create, and hash, or be null");
|
|
58
|
+
}
|
|
59
|
+
if (state !== null && digest === null) {
|
|
60
|
+
throw new TypeError("state needs a digest: records name the digest that made them");
|
|
61
|
+
}
|
|
62
|
+
const clock = deps.clock ?? (() => new Date());
|
|
63
|
+
if (typeof clock !== "function")
|
|
64
|
+
throw new TypeError("clock must be a function");
|
|
65
|
+
const locks = deps.locks ?? memoryLocks();
|
|
66
|
+
if (!isLockManager(locks))
|
|
67
|
+
throw new TypeError("locks must be a lock manager");
|
|
68
|
+
const classifiers = deps.classifiers ?? defaultClassifiers();
|
|
69
|
+
if (!Array.isArray(classifiers) || classifiers.length === 0) {
|
|
70
|
+
throw new TypeError("classifiers must be a non-empty array");
|
|
71
|
+
}
|
|
72
|
+
const codecs = deps.codecs ?? [utf8Codec()];
|
|
73
|
+
if (!Array.isArray(codecs) || codecs.length === 0 || !codecs.every(isCodec)) {
|
|
74
|
+
throw new TypeError("codecs must be a non-empty array of codecs");
|
|
75
|
+
}
|
|
76
|
+
const guards = deps.guards ?? defaultGuards();
|
|
77
|
+
if (!Array.isArray(guards) || !guards.every(isGuard)) {
|
|
78
|
+
throw new TypeError("guards must be an array of guards with an id and check");
|
|
79
|
+
}
|
|
80
|
+
const hooks = deps.hooks ?? [];
|
|
81
|
+
if (!Array.isArray(hooks) || !hooks.every(isHook)) {
|
|
82
|
+
throw new TypeError("hooks must be an array of hooks with an id and afterWrite");
|
|
83
|
+
}
|
|
84
|
+
const formatter = deps.formatter ?? defaultWriteFormatter();
|
|
85
|
+
if (!isRecord(formatter) || typeof formatter.format !== "function") {
|
|
86
|
+
throw new TypeError("formatter must have a format function");
|
|
87
|
+
}
|
|
88
|
+
return Object.freeze({
|
|
89
|
+
fs,
|
|
90
|
+
limits: resolveWriteLimits(deps.limits),
|
|
91
|
+
messages: resolveWriteMessages(deps.messages),
|
|
92
|
+
resolve,
|
|
93
|
+
authorize,
|
|
94
|
+
preconditions: resolvePreconditions(deps.preconditions),
|
|
95
|
+
state,
|
|
96
|
+
digest,
|
|
97
|
+
clock,
|
|
98
|
+
locks,
|
|
99
|
+
classifiers: Object.freeze([...classifiers]),
|
|
100
|
+
codecs: Object.freeze([...codecs]),
|
|
101
|
+
guards: Object.freeze([...guards]),
|
|
102
|
+
hooks: Object.freeze([...hooks]),
|
|
103
|
+
formatter,
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
export function resolveEditDependencies(deps) {
|
|
107
|
+
if (!isRecord(deps))
|
|
108
|
+
throw new TypeError("edit tool dependencies must be an object");
|
|
109
|
+
const { matchers = defaultEditMatchers(), ...shared } = deps;
|
|
110
|
+
if (!Array.isArray(matchers) || matchers.length === 0 || !matchers.every(isMatcher)) {
|
|
111
|
+
throw new TypeError("matchers must be a non-empty array of matchers");
|
|
112
|
+
}
|
|
113
|
+
return Object.freeze({
|
|
114
|
+
...resolveWriteDependencies(shared, "edit"),
|
|
115
|
+
matchers: Object.freeze([...matchers]),
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
export function resolveApplyPatchDependencies(deps) {
|
|
119
|
+
if (!isRecord(deps))
|
|
120
|
+
throw new TypeError("apply_patch tool dependencies must be an object");
|
|
121
|
+
const { matchers = defaultPatchMatchers(), patchParser = codexPatchParser(), ...shared } = deps;
|
|
122
|
+
if (!Array.isArray(matchers) || matchers.length === 0 || !matchers.every(isMatcher)) {
|
|
123
|
+
throw new TypeError("matchers must be a non-empty array of matchers");
|
|
124
|
+
}
|
|
125
|
+
if (!isPatchParser(patchParser)) {
|
|
126
|
+
throw new TypeError("patchParser must have an id and a parse function");
|
|
127
|
+
}
|
|
128
|
+
return Object.freeze({
|
|
129
|
+
...resolveWriteDependencies(shared, "apply_patch"),
|
|
130
|
+
matchers: Object.freeze([...matchers]),
|
|
131
|
+
patchParser,
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
const POLICY_VALUES = {
|
|
135
|
+
requireRead: ["existing", "off"],
|
|
136
|
+
partialRead: ["edit-only", "always", "never"],
|
|
137
|
+
onStale: ["rematch", "reject"],
|
|
138
|
+
};
|
|
139
|
+
export function resolvePreconditions(overrides = {}) {
|
|
140
|
+
if (!isRecord(overrides))
|
|
141
|
+
throw new TypeError("preconditions must be an object");
|
|
142
|
+
const resolved = { ...defaultPreconditions };
|
|
143
|
+
for (const [key, value] of Object.entries(overrides)) {
|
|
144
|
+
const allowed = POLICY_VALUES[key];
|
|
145
|
+
if (!Object.hasOwn(POLICY_VALUES, key) || allowed === undefined) {
|
|
146
|
+
throw new TypeError(`Unknown precondition policy key: ${key}`);
|
|
147
|
+
}
|
|
148
|
+
if (value === undefined)
|
|
149
|
+
continue;
|
|
150
|
+
if (typeof value !== "string" || !allowed.includes(value)) {
|
|
151
|
+
throw new TypeError(`preconditions.${key} must be one of ${allowed.join(", ")}`);
|
|
152
|
+
}
|
|
153
|
+
resolved[key] = value;
|
|
154
|
+
}
|
|
155
|
+
return Object.freeze(resolved);
|
|
156
|
+
}
|
|
157
|
+
export function isWritable(value) {
|
|
158
|
+
return (isRecord(value) &&
|
|
159
|
+
typeof value.open === "function" &&
|
|
160
|
+
isWritableFileSystem(value));
|
|
161
|
+
}
|
|
162
|
+
export function isFileSystem(value) {
|
|
163
|
+
return isRecord(value) && typeof value.open === "function";
|
|
164
|
+
}
|
|
165
|
+
export function isStateStore(value) {
|
|
166
|
+
return (isRecord(value) &&
|
|
167
|
+
typeof value.get === "function" &&
|
|
168
|
+
typeof value.put === "function" &&
|
|
169
|
+
typeof value.delete === "function");
|
|
170
|
+
}
|
|
171
|
+
function isDigest(value) {
|
|
172
|
+
return (isRecord(value) &&
|
|
173
|
+
typeof value.id === "string" &&
|
|
174
|
+
typeof value.create === "function" &&
|
|
175
|
+
typeof value.hash === "function");
|
|
176
|
+
}
|
|
177
|
+
function isLockManager(value) {
|
|
178
|
+
return isRecord(value) && typeof value.id === "string" && typeof value.acquire === "function";
|
|
179
|
+
}
|
|
180
|
+
function isCodec(value) {
|
|
181
|
+
return (isRecord(value) &&
|
|
182
|
+
typeof value.id === "string" &&
|
|
183
|
+
typeof value.accepts === "function" &&
|
|
184
|
+
typeof value.decode === "function" &&
|
|
185
|
+
typeof value.encode === "function" &&
|
|
186
|
+
isRecord(value.newFileStyle));
|
|
187
|
+
}
|
|
188
|
+
function isMatcher(value) {
|
|
189
|
+
return (isRecord(value) &&
|
|
190
|
+
typeof value.id === "string" &&
|
|
191
|
+
typeof value.fuzzy === "boolean" &&
|
|
192
|
+
typeof value.describe === "string" &&
|
|
193
|
+
typeof value.find === "function" &&
|
|
194
|
+
(value.adapt === undefined || typeof value.adapt === "function"));
|
|
195
|
+
}
|
|
196
|
+
function isPatchParser(value) {
|
|
197
|
+
return isRecord(value) && typeof value.id === "string" && typeof value.parse === "function";
|
|
198
|
+
}
|
|
199
|
+
function isGuard(value) {
|
|
200
|
+
return isRecord(value) && typeof value.id === "string" && typeof value.check === "function";
|
|
201
|
+
}
|
|
202
|
+
function isHook(value) {
|
|
203
|
+
return (isRecord(value) &&
|
|
204
|
+
typeof value.id === "string" &&
|
|
205
|
+
typeof value.afterWrite === "function" &&
|
|
206
|
+
(value.newFileMode === undefined || typeof value.newFileMode === "function"));
|
|
207
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export interface LineDiff {
|
|
2
|
+
readonly linesAdded: number;
|
|
3
|
+
readonly linesRemoved: number;
|
|
4
|
+
readonly text: string;
|
|
5
|
+
readonly truncated: boolean;
|
|
6
|
+
/** One-based inclusive line ranges in the new text. A pure removal gives the line after it. */
|
|
7
|
+
readonly changed: readonly (readonly [number, number])[];
|
|
8
|
+
}
|
|
9
|
+
export declare function unifiedDiff(before: string | null, after: string | null, path: string, maxLines: number, beforePath?: string): LineDiff;
|
|
10
|
+
export declare function tokens(text: string): string[];
|