@criogaid/pi-hashline-edit 0.3.1 → 0.5.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 +166 -61
- package/package.json +8 -3
- package/src/core/apply.test.ts +459 -313
- package/src/core/apply.ts +251 -215
- package/src/core/errors.ts +9 -0
- package/src/core/hash.test.ts +61 -23
- package/src/core/hash.ts +49 -20
- package/src/core/lines.test.ts +46 -27
- package/src/core/lines.ts +36 -28
- package/src/core/ranges.test.ts +73 -19
- package/src/core/ranges.ts +15 -15
- package/src/core/replace.ts +109 -0
- package/src/core/text.test.ts +3 -3
- package/src/core/text.ts +26 -9
- package/src/core/types.ts +56 -41
- package/src/index.ts +23 -19
- package/src/integration/config-registration.test.ts +206 -0
- package/src/integration/grep-rg.test.ts +879 -251
- package/src/integration/large-text.test.ts +63 -0
- package/src/integration/replace-package.test.ts +41 -0
- package/src/pi/action-fusion.test.ts +612 -207
- package/src/pi/action-fusion.ts +442 -254
- package/src/pi/anchor-format.test.ts +53 -21
- package/src/pi/anchor-format.ts +48 -20
- package/src/pi/budgets.ts +25 -0
- package/src/pi/config.test.ts +178 -35
- package/src/pi/config.ts +96 -37
- package/src/pi/directory-sync.test.ts +55 -0
- package/src/pi/e2e-scenarios.test.ts +360 -0
- package/src/pi/edit-tool.ts +307 -279
- package/src/pi/error-text.ts +26 -0
- package/src/pi/execute.test.ts +1718 -713
- package/src/pi/failure-context.test.ts +110 -83
- package/src/pi/failure-context.ts +232 -73
- package/src/pi/failure-path.test.ts +180 -66
- package/src/pi/file-commit.test.ts +446 -233
- package/src/pi/file-commit.ts +348 -203
- package/src/pi/fusion-card.test.ts +118 -83
- package/src/pi/fusion-card.ts +203 -87
- package/src/pi/grep-output.ts +225 -0
- package/src/pi/grep-render.ts +59 -0
- package/src/pi/grep-scope.ts +139 -0
- package/src/pi/grep-search.ts +231 -0
- package/src/pi/grep-tool.ts +215 -774
- package/src/pi/grep.test.ts +825 -376
- package/src/pi/mutation-outcome.testing.ts +22 -0
- package/src/pi/mutation-result.test.ts +86 -0
- package/src/pi/mutation-result.ts +118 -63
- package/src/pi/mutation-runner.ts +151 -0
- package/src/pi/path.ts +20 -12
- package/src/pi/pi.test.ts +100 -52
- package/src/pi/read-tool.ts +245 -134
- package/src/pi/registration.test.ts +271 -114
- package/src/pi/render.test.ts +227 -84
- package/src/pi/render.ts +86 -116
- package/src/pi/replace-regex.ts +54 -0
- package/src/pi/replace-tool.ts +247 -241
- package/src/pi/replace-worker.mjs +4 -0
- package/src/pi/replace-worker.ts +10 -0
- package/src/pi/replace.test.ts +778 -390
- package/src/pi/result-safety.test.ts +821 -344
- package/src/pi/rg-line-ranges.test.ts +57 -31
- package/src/pi/rg-line-ranges.ts +45 -82
- package/src/pi/rg-process.test.ts +113 -0
- package/src/pi/{rg-line-filter.ts → rg-process.ts} +72 -125
- package/src/pi/rg-text-view.ts +102 -48
- package/src/pi/schema.ts +19 -0
- package/src/pi/text-stream.test.ts +118 -0
- package/src/pi/text-stream.ts +136 -0
- package/src/pi/tool-call.testing.ts +37 -0
- package/src/pi/write-tool.ts +114 -52
- package/src/pi/write.test.ts +250 -97
- package/src/core/index.ts +0 -14
- package/src/pi/rg-line-filter.test.ts +0 -85
- package/src/pi/state.ts +0 -26
package/src/pi/file-commit.ts
CHANGED
|
@@ -1,249 +1,394 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
chmod,
|
|
3
|
+
lstat,
|
|
4
|
+
mkdir,
|
|
5
|
+
mkdtemp,
|
|
6
|
+
open,
|
|
7
|
+
readFile,
|
|
8
|
+
realpath,
|
|
9
|
+
rename,
|
|
10
|
+
rm,
|
|
11
|
+
stat,
|
|
12
|
+
link,
|
|
13
|
+
} from "node:fs/promises";
|
|
2
14
|
import { dirname, join } from "node:path";
|
|
3
15
|
import { createHash } from "node:crypto";
|
|
4
|
-
import { decodeEditableText } from "../core/text.ts";
|
|
16
|
+
import { decodeEditableText, unwritableTextReason } from "../core/text.ts";
|
|
17
|
+
import { errorMessage } from "../core/errors.ts";
|
|
18
|
+
import { OPERATION_ABORTED, throwIfCancelled } from "./error-text.ts";
|
|
5
19
|
|
|
6
20
|
export type PublicationStatus = "NOT_PUBLISHED" | "PUBLISHED" | "UNKNOWN";
|
|
7
21
|
export type CommitMode = "create" | "overwrite";
|
|
8
22
|
|
|
9
23
|
export interface CommitOptions {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
24
|
+
mode?: CommitMode;
|
|
25
|
+
expectedRevision?: string;
|
|
26
|
+
signal?: AbortSignal;
|
|
27
|
+
/** Caller-supplied revision of the current file, skipping the readFile + SHA-256 in inspectTarget. */
|
|
28
|
+
knownBeforeRevision?: string;
|
|
13
29
|
}
|
|
14
30
|
|
|
15
31
|
export interface MutationVersions {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
32
|
+
baseRevision?: string;
|
|
33
|
+
publishedRevision: string;
|
|
34
|
+
observedRevision: string;
|
|
19
35
|
}
|
|
20
36
|
|
|
21
37
|
export interface CommitResult extends MutationVersions {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
revision: string;
|
|
25
|
-
publication: "NOT_PUBLISHED" | "PUBLISHED";
|
|
38
|
+
created: boolean;
|
|
39
|
+
publication: "NOT_PUBLISHED" | "PUBLISHED";
|
|
26
40
|
}
|
|
27
41
|
|
|
28
42
|
export class FileMutationError extends Error {
|
|
29
|
-
|
|
30
|
-
|
|
43
|
+
readonly stage: "prepare" | "commit" | "post_process";
|
|
44
|
+
readonly publication: PublicationStatus;
|
|
31
45
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
46
|
+
constructor(
|
|
47
|
+
stage: "prepare" | "commit" | "post_process",
|
|
48
|
+
publication: PublicationStatus,
|
|
49
|
+
message: string,
|
|
50
|
+
options?: { cause?: unknown },
|
|
51
|
+
) {
|
|
52
|
+
super(message, options);
|
|
53
|
+
this.name = "FileMutationError";
|
|
54
|
+
this.stage = stage;
|
|
55
|
+
this.publication = publication;
|
|
56
|
+
}
|
|
38
57
|
}
|
|
39
58
|
|
|
40
59
|
export function byteRevision(content: string | Uint8Array): string {
|
|
41
|
-
|
|
60
|
+
return createHash("sha256").update(content).digest("hex");
|
|
42
61
|
}
|
|
43
62
|
|
|
44
63
|
export async function fileRevision(path: string): Promise<string> {
|
|
45
|
-
|
|
64
|
+
return byteRevision(await readFile(path));
|
|
46
65
|
}
|
|
47
66
|
|
|
48
67
|
/** Decode and bind a mutation snapshot to the exact bytes read. */
|
|
49
68
|
export async function readEditableSnapshot(path: string, displayPath: string) {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
69
|
+
try {
|
|
70
|
+
const bytes = await readFile(path);
|
|
71
|
+
return { text: decodeEditableText(bytes), baseRevision: byteRevision(bytes) };
|
|
72
|
+
} catch (error) {
|
|
73
|
+
throw new Error(`Error reading ${displayPath}: ${errorMessage(error)}`);
|
|
74
|
+
}
|
|
56
75
|
}
|
|
57
76
|
|
|
58
77
|
/** Publish a read-modify-write result against its source revision. Callers own the queue. */
|
|
59
|
-
export async function commitReplacement(
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
78
|
+
export async function commitReplacement(
|
|
79
|
+
path: string,
|
|
80
|
+
displayPath: string,
|
|
81
|
+
text: string,
|
|
82
|
+
baseRevision: string,
|
|
83
|
+
signal?: AbortSignal,
|
|
84
|
+
) {
|
|
85
|
+
try {
|
|
86
|
+
return await commitFile(path, text, {
|
|
87
|
+
mode: "overwrite",
|
|
88
|
+
expectedRevision: baseRevision,
|
|
89
|
+
knownBeforeRevision: baseRevision,
|
|
90
|
+
signal,
|
|
91
|
+
});
|
|
92
|
+
} catch (error) {
|
|
93
|
+
if (error instanceof FileMutationError) throw error;
|
|
94
|
+
throw new FileMutationError(
|
|
95
|
+
"commit",
|
|
96
|
+
"UNKNOWN",
|
|
97
|
+
`Error writing ${displayPath}: ${errorMessage(error)}`,
|
|
98
|
+
{ cause: error },
|
|
99
|
+
);
|
|
100
|
+
}
|
|
66
101
|
}
|
|
67
102
|
|
|
68
103
|
function prepareError(message: string, cause?: unknown): FileMutationError {
|
|
69
|
-
|
|
104
|
+
return new FileMutationError("prepare", "NOT_PUBLISHED", message, { cause });
|
|
70
105
|
}
|
|
71
106
|
|
|
72
107
|
function errorCode(error: unknown): string | undefined {
|
|
73
|
-
|
|
108
|
+
return typeof error === "object" &&
|
|
109
|
+
error !== null &&
|
|
110
|
+
"code" in error &&
|
|
111
|
+
typeof error.code === "string"
|
|
112
|
+
? error.code
|
|
113
|
+
: undefined;
|
|
74
114
|
}
|
|
75
115
|
|
|
76
116
|
interface TargetInfo {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
async function inspectTarget(path: string): Promise<TargetInfo> {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
117
|
+
existed: boolean;
|
|
118
|
+
publishPath: string;
|
|
119
|
+
beforeRevision?: string;
|
|
120
|
+
modeBits?: number;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
async function inspectTarget(path: string, knownBeforeRevision?: string): Promise<TargetInfo> {
|
|
124
|
+
let entry;
|
|
125
|
+
try {
|
|
126
|
+
entry = await lstat(path);
|
|
127
|
+
} catch (error) {
|
|
128
|
+
if (errorCode(error) === "ENOENT") return { existed: false, publishPath: path };
|
|
129
|
+
throw prepareError(`unable to inspect target: ${errorMessage(error)}`, error);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
let publishPath = path;
|
|
133
|
+
if (entry.isSymbolicLink()) {
|
|
134
|
+
try {
|
|
135
|
+
publishPath = await realpath(path);
|
|
136
|
+
} catch (error) {
|
|
137
|
+
throw prepareError(`target symlink cannot be resolved: ${errorMessage(error)}`, error);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
let target;
|
|
142
|
+
try {
|
|
143
|
+
target = await stat(publishPath);
|
|
144
|
+
} catch (error) {
|
|
145
|
+
throw prepareError(`unable to inspect target: ${errorMessage(error)}`, error);
|
|
146
|
+
}
|
|
147
|
+
if (!target.isFile()) throw prepareError("target is not a regular file");
|
|
148
|
+
if (target.nlink > 1)
|
|
149
|
+
throw prepareError("target has multiple hard links; refusing to split the link set");
|
|
150
|
+
|
|
151
|
+
let beforeRevision: string;
|
|
152
|
+
if (knownBeforeRevision !== undefined) {
|
|
153
|
+
beforeRevision = knownBeforeRevision;
|
|
154
|
+
} else {
|
|
155
|
+
try {
|
|
156
|
+
beforeRevision = await fileRevision(publishPath);
|
|
157
|
+
} catch (error) {
|
|
158
|
+
throw prepareError(`unable to read target revision: ${errorMessage(error)}`, error);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return {
|
|
162
|
+
existed: true,
|
|
163
|
+
publishPath,
|
|
164
|
+
beforeRevision,
|
|
165
|
+
modeBits: target.mode,
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
async function writeAndSyncTemp(
|
|
170
|
+
tempPath: string,
|
|
171
|
+
content: Buffer,
|
|
172
|
+
modeBits: number | undefined,
|
|
173
|
+
signal: AbortSignal | undefined,
|
|
174
|
+
): Promise<void> {
|
|
175
|
+
throwIfCancelled(signal);
|
|
176
|
+
const handle = await open(tempPath, "w", modeBits === undefined ? 0o600 : modeBits & 0o7777);
|
|
177
|
+
try {
|
|
178
|
+
await handle.writeFile(content);
|
|
179
|
+
await handle.sync();
|
|
180
|
+
} finally {
|
|
181
|
+
await handle.close();
|
|
182
|
+
}
|
|
183
|
+
if (modeBits !== undefined) await chmod(tempPath, modeBits & 0o7777);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
async function publishCreate(
|
|
187
|
+
tempPath: string,
|
|
188
|
+
targetPath: string,
|
|
189
|
+
signal: AbortSignal | undefined,
|
|
190
|
+
): Promise<void> {
|
|
191
|
+
// link() creates an unclobberable directory entry on the same filesystem, avoiding create/replace races.
|
|
192
|
+
throwIfCancelled(signal);
|
|
193
|
+
try {
|
|
194
|
+
await link(tempPath, targetPath);
|
|
195
|
+
} catch (error) {
|
|
196
|
+
if (errorCode(error) === "EEXIST" || errorCode(error) === "ENOTEMPTY") {
|
|
197
|
+
throw new FileMutationError(
|
|
198
|
+
"commit",
|
|
199
|
+
"NOT_PUBLISHED",
|
|
200
|
+
"target appeared during create; refusing to overwrite it",
|
|
201
|
+
{ cause: error },
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
throw new FileMutationError(
|
|
205
|
+
"commit",
|
|
206
|
+
"UNKNOWN",
|
|
207
|
+
`unable to publish new target: ${errorMessage(error)}`,
|
|
208
|
+
{ cause: error },
|
|
209
|
+
);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
async function publishReplace(
|
|
214
|
+
tempPath: string,
|
|
215
|
+
targetPath: string,
|
|
216
|
+
signal: AbortSignal | undefined,
|
|
217
|
+
): Promise<void> {
|
|
218
|
+
// rename() replaces the directory entry atomically without deleting the old file first; readers observe old or new.
|
|
219
|
+
throwIfCancelled(signal);
|
|
220
|
+
try {
|
|
221
|
+
await rename(tempPath, targetPath);
|
|
222
|
+
} catch (error) {
|
|
223
|
+
throw new FileMutationError(
|
|
224
|
+
"commit",
|
|
225
|
+
"UNKNOWN",
|
|
226
|
+
`unable to publish replacement: ${errorMessage(error)}`,
|
|
227
|
+
{ cause: error },
|
|
228
|
+
);
|
|
229
|
+
}
|
|
157
230
|
}
|
|
158
231
|
|
|
159
232
|
async function syncDirectory(path: string): Promise<void> {
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
233
|
+
if (process.platform === "win32") return;
|
|
234
|
+
const handle = await open(path, "r");
|
|
235
|
+
try {
|
|
236
|
+
await handle.sync();
|
|
237
|
+
} catch (error) {
|
|
238
|
+
// Some filesystems do not support directory fsync; preserve real I/O failures.
|
|
239
|
+
if (errorCode(error) !== "EINVAL" && errorCode(error) !== "ENOTSUP") throw error;
|
|
240
|
+
} finally {
|
|
241
|
+
await handle.close();
|
|
242
|
+
}
|
|
167
243
|
}
|
|
168
244
|
|
|
169
245
|
/** Validate the target, skip identical content, otherwise sync and publish. Callers own the file queue. */
|
|
170
|
-
export async function commitFile(
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
246
|
+
export async function commitFile(
|
|
247
|
+
path: string,
|
|
248
|
+
content: string,
|
|
249
|
+
options: CommitOptions = {},
|
|
250
|
+
): Promise<CommitResult> {
|
|
251
|
+
// Tools reject such input in prepareArguments, but a transformation can still produce it
|
|
252
|
+
// (a regex without `u` can split a surrogate pair), so publication keeps the final check.
|
|
253
|
+
const unwritable = unwritableTextReason(content);
|
|
254
|
+
if (unwritable) throw prepareError(unwritable);
|
|
255
|
+
const bytes = Buffer.from(content, "utf8");
|
|
256
|
+
const target = await inspectTarget(path, options.knownBeforeRevision);
|
|
257
|
+
const mode = options.mode ?? (target.existed ? "overwrite" : "create");
|
|
258
|
+
if (mode === "create" && target.existed)
|
|
259
|
+
throw prepareError("target already exists; use mode=overwrite");
|
|
260
|
+
if (mode === "overwrite" && !target.existed)
|
|
261
|
+
throw prepareError("target does not exist; use mode=create or omit mode");
|
|
262
|
+
if (mode === "create" && options.expectedRevision !== undefined)
|
|
263
|
+
throw prepareError("expectedRevision cannot be combined with mode=create");
|
|
264
|
+
if (
|
|
265
|
+
options.expectedRevision !== undefined &&
|
|
266
|
+
(!target.beforeRevision || target.beforeRevision !== options.expectedRevision)
|
|
267
|
+
) {
|
|
268
|
+
throw prepareError("expectedRevision does not match the current file");
|
|
269
|
+
}
|
|
270
|
+
try {
|
|
271
|
+
throwIfCancelled(options.signal);
|
|
272
|
+
} catch (error) {
|
|
273
|
+
throw prepareError(`${OPERATION_ABORTED} before publication`, error);
|
|
274
|
+
}
|
|
275
|
+
const publishedRevision = byteRevision(bytes);
|
|
276
|
+
// Mode, revision, target safety, and cancellation checks still apply to no-ops.
|
|
277
|
+
if (target.beforeRevision === publishedRevision) {
|
|
278
|
+
let currentRevision = target.beforeRevision;
|
|
279
|
+
if (options.knownBeforeRevision !== undefined) {
|
|
280
|
+
try {
|
|
281
|
+
throwIfCancelled(options.signal);
|
|
282
|
+
currentRevision = await fileRevision(target.publishPath);
|
|
283
|
+
throwIfCancelled(options.signal);
|
|
284
|
+
} catch (error) {
|
|
285
|
+
if (options.signal?.aborted)
|
|
286
|
+
throw prepareError(`${OPERATION_ABORTED} before publication`, error);
|
|
287
|
+
throw prepareError(`unable to read target revision: ${errorMessage(error)}`, error);
|
|
288
|
+
}
|
|
289
|
+
if (options.expectedRevision !== undefined && currentRevision !== options.expectedRevision) {
|
|
290
|
+
throw prepareError("expectedRevision does not match the current file");
|
|
291
|
+
}
|
|
292
|
+
if (currentRevision !== publishedRevision) {
|
|
293
|
+
target.beforeRevision = currentRevision;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
if (target.beforeRevision === publishedRevision) {
|
|
297
|
+
return {
|
|
298
|
+
created: false,
|
|
299
|
+
baseRevision: target.beforeRevision,
|
|
300
|
+
publishedRevision,
|
|
301
|
+
observedRevision: currentRevision,
|
|
302
|
+
publication: "NOT_PUBLISHED",
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
const publishPath = target.publishPath;
|
|
308
|
+
const publishDirectory = dirname(publishPath);
|
|
309
|
+
let tempDir: string;
|
|
310
|
+
try {
|
|
311
|
+
await mkdir(publishDirectory, { recursive: true });
|
|
312
|
+
tempDir = await mkdtemp(join(publishDirectory, ".hashline-commit-"));
|
|
313
|
+
} catch (error) {
|
|
314
|
+
throw new FileMutationError(
|
|
315
|
+
"commit",
|
|
316
|
+
"NOT_PUBLISHED",
|
|
317
|
+
`unable to prepare temporary publication area: ${errorMessage(error)}`,
|
|
318
|
+
{ cause: error },
|
|
319
|
+
);
|
|
320
|
+
}
|
|
321
|
+
const tempPath = join(tempDir, "content");
|
|
322
|
+
let published = false;
|
|
323
|
+
let failure: unknown;
|
|
324
|
+
try {
|
|
325
|
+
await writeAndSyncTemp(tempPath, bytes, target.modeBits, options.signal);
|
|
326
|
+
if (mode === "overwrite" && options.expectedRevision !== undefined) {
|
|
327
|
+
let currentRevision: string;
|
|
328
|
+
try {
|
|
329
|
+
currentRevision = await fileRevision(publishPath);
|
|
330
|
+
} catch (error) {
|
|
331
|
+
throw prepareError(`unable to recheck target revision: ${errorMessage(error)}`, error);
|
|
332
|
+
}
|
|
333
|
+
if (currentRevision !== options.expectedRevision)
|
|
334
|
+
throw prepareError("expectedRevision changed before publication");
|
|
335
|
+
}
|
|
336
|
+
if (mode === "create") await publishCreate(tempPath, publishPath, options.signal);
|
|
337
|
+
else await publishReplace(tempPath, publishPath, options.signal);
|
|
338
|
+
published = true;
|
|
339
|
+
await syncDirectory(publishDirectory);
|
|
340
|
+
try {
|
|
341
|
+
const observedRevision = await fileRevision(publishPath);
|
|
342
|
+
return {
|
|
343
|
+
created: mode === "create",
|
|
344
|
+
baseRevision: target.beforeRevision,
|
|
345
|
+
publishedRevision,
|
|
346
|
+
observedRevision,
|
|
347
|
+
publication: "PUBLISHED",
|
|
348
|
+
};
|
|
349
|
+
} catch (error) {
|
|
350
|
+
throw new FileMutationError(
|
|
351
|
+
"post_process",
|
|
352
|
+
"PUBLISHED",
|
|
353
|
+
`target was published but final revision could not be read: ${errorMessage(error)}`,
|
|
354
|
+
{ cause: error },
|
|
355
|
+
);
|
|
356
|
+
}
|
|
357
|
+
} catch (error) {
|
|
358
|
+
failure = error;
|
|
359
|
+
if (error instanceof FileMutationError) throw error;
|
|
360
|
+
if (published)
|
|
361
|
+
throw new FileMutationError(
|
|
362
|
+
"post_process",
|
|
363
|
+
"PUBLISHED",
|
|
364
|
+
`target was published but post-publication processing failed: ${errorMessage(error)}`,
|
|
365
|
+
{ cause: error },
|
|
366
|
+
);
|
|
367
|
+
throw new FileMutationError(
|
|
368
|
+
"commit",
|
|
369
|
+
"NOT_PUBLISHED",
|
|
370
|
+
`unable to prepare or publish target: ${errorMessage(error)}`,
|
|
371
|
+
{ cause: error },
|
|
372
|
+
);
|
|
373
|
+
} finally {
|
|
374
|
+
try {
|
|
375
|
+
await rm(tempDir, { recursive: true, force: true });
|
|
376
|
+
} catch (error) {
|
|
377
|
+
if (!published) {
|
|
378
|
+
if (failure !== undefined) throw failure;
|
|
379
|
+
throw error;
|
|
380
|
+
}
|
|
381
|
+
throw new FileMutationError(
|
|
382
|
+
"post_process",
|
|
383
|
+
"PUBLISHED",
|
|
384
|
+
`target was published but temporary cleanup failed: ${errorMessage(error)}`,
|
|
385
|
+
{
|
|
386
|
+
cause:
|
|
387
|
+
failure === undefined
|
|
388
|
+
? error
|
|
389
|
+
: new AggregateError([failure, error], "post-publication cleanup also failed"),
|
|
390
|
+
},
|
|
391
|
+
);
|
|
392
|
+
}
|
|
393
|
+
}
|
|
249
394
|
}
|