@gmickel/gno 1.31.0 → 1.32.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 +5 -4
- package/assets/skill/SKILL.md +23 -0
- package/browser-extension/artifacts/{gno-browser-clipper-v1.31.0.zip → gno-browser-clipper-v1.32.0.zip} +0 -0
- package/browser-extension/artifacts/gno-browser-clipper-v1.32.0.zip.sha256 +1 -0
- package/browser-extension/dist/manifest.json +1 -1
- package/package.json +1 -1
- package/spec/cli.md +19 -0
- package/spec/db/schema.sql +55 -0
- package/spec/mcp.md +114 -11
- package/spec/output-schemas/file-refactor-apply-result.schema.json +305 -0
- package/spec/output-schemas/file-refactor-preview.schema.json +393 -0
- package/src/core/document-capabilities.ts +13 -0
- package/src/core/file-ops.ts +129 -1
- package/src/core/file-refactor-adapter.ts +329 -0
- package/src/core/file-refactor-apply-edits.ts +61 -0
- package/src/core/file-refactor-apply-fs.ts +512 -0
- package/src/core/file-refactor-apply-safety.ts +340 -0
- package/src/core/file-refactor-apply-validate.ts +401 -0
- package/src/core/file-refactor-contract.ts +486 -0
- package/src/core/file-refactor-destination.ts +123 -0
- package/src/core/file-refactor-from-snapshot.ts +148 -0
- package/src/core/file-refactor-journal-port.ts +150 -0
- package/src/core/file-refactor-journal.ts +347 -0
- package/src/core/file-refactor-paths.ts +60 -0
- package/src/core/file-refactor-plan-classify.ts +208 -0
- package/src/core/file-refactor-plan-validate.ts +169 -0
- package/src/core/file-refactor-planner-types.ts +62 -0
- package/src/core/file-refactor-planner.ts +423 -0
- package/src/core/file-refactor-resolve.ts +280 -0
- package/src/core/file-refactor-service.ts +468 -0
- package/src/core/file-refactors.ts +84 -56
- package/src/core/link-destination-parse.ts +275 -0
- package/src/core/link-inventory-markdown.ts +454 -0
- package/src/core/link-inventory-opaque.ts +244 -0
- package/src/core/link-inventory-types.ts +47 -0
- package/src/core/link-inventory.ts +182 -0
- package/src/core/link-relevance.ts +150 -0
- package/src/mcp/tools/index.ts +18 -17
- package/src/mcp/tools/workspace-write.ts +215 -97
- package/src/sdk/client.ts +167 -115
- package/src/sdk/index.ts +7 -0
- package/src/sdk/types.ts +32 -2
- package/src/serve/file-refactor-http.ts +239 -0
- package/src/serve/public/components/RefactorImpactPreview.tsx +227 -0
- package/src/serve/public/globals.built.css +1 -1
- package/src/serve/public/pages/DocView.tsx +176 -41
- package/src/serve/routes/api.ts +191 -104
- package/src/store/migrations/026-file-refactor-recovery-journal.ts +72 -0
- package/src/store/migrations/index.ts +2 -0
- package/src/store/sqlite/adapter.ts +452 -0
- package/src/store/sqlite/file-refactor-journal-store.ts +275 -0
- package/src/store/types.ts +84 -0
- package/browser-extension/artifacts/gno-browser-clipper-v1.31.0.zip.sha256 +0 -1
|
@@ -0,0 +1,393 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "http://json-schema.org/draft-07/schema#",
|
|
3
|
+
"$id": "gno://schemas/file-refactor-preview@1.0",
|
|
4
|
+
"title": "GNO File Refactor Preview Plan",
|
|
5
|
+
"description": "Transport-neutral, versioned preview plan for reference-safe rename and same-collection move. Destination-only edit spans; filesystem atomicity is separate from post-commit index convergence.",
|
|
6
|
+
"type": "object",
|
|
7
|
+
"additionalProperties": false,
|
|
8
|
+
"required": [
|
|
9
|
+
"schemaVersion",
|
|
10
|
+
"operation",
|
|
11
|
+
"conflictPolicy",
|
|
12
|
+
"source",
|
|
13
|
+
"target",
|
|
14
|
+
"affectedDocuments",
|
|
15
|
+
"examinedReferences",
|
|
16
|
+
"preconditions",
|
|
17
|
+
"planDigest",
|
|
18
|
+
"safety",
|
|
19
|
+
"canApply",
|
|
20
|
+
"mutationBoundary"
|
|
21
|
+
],
|
|
22
|
+
"properties": {
|
|
23
|
+
"schemaVersion": {
|
|
24
|
+
"const": "1.0"
|
|
25
|
+
},
|
|
26
|
+
"operation": {
|
|
27
|
+
"type": "string",
|
|
28
|
+
"enum": ["rename", "move"]
|
|
29
|
+
},
|
|
30
|
+
"conflictPolicy": {
|
|
31
|
+
"type": "string",
|
|
32
|
+
"enum": ["fail"]
|
|
33
|
+
},
|
|
34
|
+
"source": {
|
|
35
|
+
"$ref": "#/definitions/documentRef"
|
|
36
|
+
},
|
|
37
|
+
"target": {
|
|
38
|
+
"$ref": "#/definitions/documentRef"
|
|
39
|
+
},
|
|
40
|
+
"affectedDocuments": {
|
|
41
|
+
"type": "array",
|
|
42
|
+
"maxItems": 10000,
|
|
43
|
+
"items": {
|
|
44
|
+
"$ref": "#/definitions/affectedDocument"
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
"examinedReferences": {
|
|
48
|
+
"type": "array",
|
|
49
|
+
"maxItems": 100000,
|
|
50
|
+
"items": {
|
|
51
|
+
"$ref": "#/definitions/examinedReference"
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
"preconditions": {
|
|
55
|
+
"$ref": "#/definitions/preconditions"
|
|
56
|
+
},
|
|
57
|
+
"planDigest": {
|
|
58
|
+
"$ref": "#/definitions/sha256"
|
|
59
|
+
},
|
|
60
|
+
"safety": {
|
|
61
|
+
"$ref": "#/definitions/safetySummary"
|
|
62
|
+
},
|
|
63
|
+
"canApply": {
|
|
64
|
+
"type": "boolean"
|
|
65
|
+
},
|
|
66
|
+
"mutationBoundary": {
|
|
67
|
+
"$ref": "#/definitions/mutationBoundary"
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
"definitions": {
|
|
71
|
+
"sha256": {
|
|
72
|
+
"type": "string",
|
|
73
|
+
"pattern": "^[a-f0-9]{64}$"
|
|
74
|
+
},
|
|
75
|
+
"reasonCode": {
|
|
76
|
+
"type": "string",
|
|
77
|
+
"enum": [
|
|
78
|
+
"ambiguous_resolution",
|
|
79
|
+
"unsupported_syntax",
|
|
80
|
+
"malformed_syntax",
|
|
81
|
+
"stale_plan",
|
|
82
|
+
"capability_denied",
|
|
83
|
+
"occupied_target",
|
|
84
|
+
"sync_pending",
|
|
85
|
+
"cross_collection_unsupported",
|
|
86
|
+
"unsafe_target",
|
|
87
|
+
"external_destination",
|
|
88
|
+
"code_fence_context",
|
|
89
|
+
"inline_code_context",
|
|
90
|
+
"html_context",
|
|
91
|
+
"duplicate_basename_ambiguity",
|
|
92
|
+
"unicode_normalization_mismatch",
|
|
93
|
+
"destination_unchanged",
|
|
94
|
+
"read_only_document",
|
|
95
|
+
"relative_path_recalculated",
|
|
96
|
+
"reference_definition_site",
|
|
97
|
+
"filesystem_commit_failed",
|
|
98
|
+
"rollback_recovery_required"
|
|
99
|
+
]
|
|
100
|
+
},
|
|
101
|
+
"documentRef": {
|
|
102
|
+
"type": "object",
|
|
103
|
+
"additionalProperties": false,
|
|
104
|
+
"required": ["uri", "relPath", "collection"],
|
|
105
|
+
"properties": {
|
|
106
|
+
"uri": {
|
|
107
|
+
"type": "string",
|
|
108
|
+
"minLength": 1,
|
|
109
|
+
"maxLength": 1024
|
|
110
|
+
},
|
|
111
|
+
"relPath": {
|
|
112
|
+
"type": "string",
|
|
113
|
+
"minLength": 1,
|
|
114
|
+
"maxLength": 1024
|
|
115
|
+
},
|
|
116
|
+
"collection": {
|
|
117
|
+
"type": "string",
|
|
118
|
+
"minLength": 1,
|
|
119
|
+
"maxLength": 256
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
},
|
|
123
|
+
"destinationSpan": {
|
|
124
|
+
"type": "object",
|
|
125
|
+
"additionalProperties": false,
|
|
126
|
+
"required": [
|
|
127
|
+
"coordinateSpace",
|
|
128
|
+
"startOffset",
|
|
129
|
+
"endOffset",
|
|
130
|
+
"originalDestination",
|
|
131
|
+
"replacementDestination"
|
|
132
|
+
],
|
|
133
|
+
"properties": {
|
|
134
|
+
"coordinateSpace": {
|
|
135
|
+
"const": "utf16_code_units"
|
|
136
|
+
},
|
|
137
|
+
"startOffset": {
|
|
138
|
+
"type": "integer",
|
|
139
|
+
"minimum": 0,
|
|
140
|
+
"maximum": 9007199254740991
|
|
141
|
+
},
|
|
142
|
+
"endOffset": {
|
|
143
|
+
"type": "integer",
|
|
144
|
+
"minimum": 0,
|
|
145
|
+
"maximum": 9007199254740991
|
|
146
|
+
},
|
|
147
|
+
"originalDestination": {
|
|
148
|
+
"type": "string",
|
|
149
|
+
"maxLength": 4096
|
|
150
|
+
},
|
|
151
|
+
"replacementDestination": {
|
|
152
|
+
"type": "string",
|
|
153
|
+
"maxLength": 4096
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
},
|
|
157
|
+
"examinedReference": {
|
|
158
|
+
"type": "object",
|
|
159
|
+
"additionalProperties": false,
|
|
160
|
+
"required": ["documentUri", "documentRelPath", "kind", "classification"],
|
|
161
|
+
"properties": {
|
|
162
|
+
"documentUri": {
|
|
163
|
+
"type": "string",
|
|
164
|
+
"minLength": 1,
|
|
165
|
+
"maxLength": 1024
|
|
166
|
+
},
|
|
167
|
+
"documentRelPath": {
|
|
168
|
+
"type": "string",
|
|
169
|
+
"minLength": 1,
|
|
170
|
+
"maxLength": 1024
|
|
171
|
+
},
|
|
172
|
+
"kind": {
|
|
173
|
+
"type": "string",
|
|
174
|
+
"enum": ["wiki", "markdown", "markdown_definition", "opaque"]
|
|
175
|
+
},
|
|
176
|
+
"classification": {
|
|
177
|
+
"type": "string",
|
|
178
|
+
"enum": [
|
|
179
|
+
"rewriteable",
|
|
180
|
+
"unchanged",
|
|
181
|
+
"ambiguous",
|
|
182
|
+
"unsupported",
|
|
183
|
+
"malformed",
|
|
184
|
+
"invalid"
|
|
185
|
+
]
|
|
186
|
+
},
|
|
187
|
+
"reasonCode": {
|
|
188
|
+
"$ref": "#/definitions/reasonCode"
|
|
189
|
+
},
|
|
190
|
+
"originalDestination": {
|
|
191
|
+
"type": "string",
|
|
192
|
+
"maxLength": 4096
|
|
193
|
+
},
|
|
194
|
+
"proposedDestination": {
|
|
195
|
+
"type": "string",
|
|
196
|
+
"maxLength": 4096
|
|
197
|
+
},
|
|
198
|
+
"edit": {
|
|
199
|
+
"$ref": "#/definitions/destinationSpan"
|
|
200
|
+
},
|
|
201
|
+
"startLine": {
|
|
202
|
+
"type": "integer",
|
|
203
|
+
"minimum": 1,
|
|
204
|
+
"maximum": 9007199254740991
|
|
205
|
+
},
|
|
206
|
+
"startCol": {
|
|
207
|
+
"type": "integer",
|
|
208
|
+
"minimum": 1,
|
|
209
|
+
"maximum": 9007199254740991
|
|
210
|
+
},
|
|
211
|
+
"endLine": {
|
|
212
|
+
"type": "integer",
|
|
213
|
+
"minimum": 1,
|
|
214
|
+
"maximum": 9007199254740991
|
|
215
|
+
},
|
|
216
|
+
"endCol": {
|
|
217
|
+
"type": "integer",
|
|
218
|
+
"minimum": 1,
|
|
219
|
+
"maximum": 9007199254740991
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
},
|
|
223
|
+
"affectedDocument": {
|
|
224
|
+
"type": "object",
|
|
225
|
+
"additionalProperties": false,
|
|
226
|
+
"required": ["uri", "relPath", "contentFingerprint", "edits", "examined"],
|
|
227
|
+
"properties": {
|
|
228
|
+
"uri": {
|
|
229
|
+
"type": "string",
|
|
230
|
+
"minLength": 1,
|
|
231
|
+
"maxLength": 1024
|
|
232
|
+
},
|
|
233
|
+
"relPath": {
|
|
234
|
+
"type": "string",
|
|
235
|
+
"minLength": 1,
|
|
236
|
+
"maxLength": 1024
|
|
237
|
+
},
|
|
238
|
+
"contentFingerprint": {
|
|
239
|
+
"$ref": "#/definitions/sha256"
|
|
240
|
+
},
|
|
241
|
+
"edits": {
|
|
242
|
+
"type": "array",
|
|
243
|
+
"maxItems": 10000,
|
|
244
|
+
"items": {
|
|
245
|
+
"$ref": "#/definitions/destinationSpan"
|
|
246
|
+
}
|
|
247
|
+
},
|
|
248
|
+
"examined": {
|
|
249
|
+
"type": "array",
|
|
250
|
+
"maxItems": 100000,
|
|
251
|
+
"items": {
|
|
252
|
+
"$ref": "#/definitions/examinedReference"
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
},
|
|
257
|
+
"preconditions": {
|
|
258
|
+
"type": "object",
|
|
259
|
+
"additionalProperties": false,
|
|
260
|
+
"required": [
|
|
261
|
+
"sourceContentFingerprint",
|
|
262
|
+
"affectedContentFingerprints",
|
|
263
|
+
"targetPathFingerprint"
|
|
264
|
+
],
|
|
265
|
+
"properties": {
|
|
266
|
+
"sourceContentFingerprint": {
|
|
267
|
+
"$ref": "#/definitions/sha256"
|
|
268
|
+
},
|
|
269
|
+
"affectedContentFingerprints": {
|
|
270
|
+
"type": "array",
|
|
271
|
+
"maxItems": 10000,
|
|
272
|
+
"items": {
|
|
273
|
+
"type": "object",
|
|
274
|
+
"additionalProperties": false,
|
|
275
|
+
"required": ["uri", "fingerprint"],
|
|
276
|
+
"properties": {
|
|
277
|
+
"uri": {
|
|
278
|
+
"type": "string",
|
|
279
|
+
"minLength": 1,
|
|
280
|
+
"maxLength": 1024
|
|
281
|
+
},
|
|
282
|
+
"fingerprint": {
|
|
283
|
+
"$ref": "#/definitions/sha256"
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
},
|
|
288
|
+
"targetPathFingerprint": {
|
|
289
|
+
"$ref": "#/definitions/sha256"
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
},
|
|
293
|
+
"safetySummary": {
|
|
294
|
+
"type": "object",
|
|
295
|
+
"additionalProperties": false,
|
|
296
|
+
"required": [
|
|
297
|
+
"rewriteableCount",
|
|
298
|
+
"unchangedCount",
|
|
299
|
+
"ambiguousCount",
|
|
300
|
+
"unsupportedCount",
|
|
301
|
+
"malformedCount",
|
|
302
|
+
"invalidCount",
|
|
303
|
+
"blockingReasonCodes",
|
|
304
|
+
"warnings",
|
|
305
|
+
"backlinkCount",
|
|
306
|
+
"wikiLinkCount",
|
|
307
|
+
"markdownLinkCount"
|
|
308
|
+
],
|
|
309
|
+
"properties": {
|
|
310
|
+
"rewriteableCount": {
|
|
311
|
+
"type": "integer",
|
|
312
|
+
"minimum": 0,
|
|
313
|
+
"maximum": 9007199254740991
|
|
314
|
+
},
|
|
315
|
+
"unchangedCount": {
|
|
316
|
+
"type": "integer",
|
|
317
|
+
"minimum": 0,
|
|
318
|
+
"maximum": 9007199254740991
|
|
319
|
+
},
|
|
320
|
+
"ambiguousCount": {
|
|
321
|
+
"type": "integer",
|
|
322
|
+
"minimum": 0,
|
|
323
|
+
"maximum": 9007199254740991
|
|
324
|
+
},
|
|
325
|
+
"unsupportedCount": {
|
|
326
|
+
"type": "integer",
|
|
327
|
+
"minimum": 0,
|
|
328
|
+
"maximum": 9007199254740991
|
|
329
|
+
},
|
|
330
|
+
"malformedCount": {
|
|
331
|
+
"type": "integer",
|
|
332
|
+
"minimum": 0,
|
|
333
|
+
"maximum": 9007199254740991
|
|
334
|
+
},
|
|
335
|
+
"invalidCount": {
|
|
336
|
+
"type": "integer",
|
|
337
|
+
"minimum": 0,
|
|
338
|
+
"maximum": 9007199254740991
|
|
339
|
+
},
|
|
340
|
+
"blockingReasonCodes": {
|
|
341
|
+
"type": "array",
|
|
342
|
+
"maxItems": 64,
|
|
343
|
+
"items": {
|
|
344
|
+
"$ref": "#/definitions/reasonCode"
|
|
345
|
+
}
|
|
346
|
+
},
|
|
347
|
+
"warnings": {
|
|
348
|
+
"type": "array",
|
|
349
|
+
"maxItems": 64,
|
|
350
|
+
"items": {
|
|
351
|
+
"type": "string",
|
|
352
|
+
"maxLength": 512
|
|
353
|
+
}
|
|
354
|
+
},
|
|
355
|
+
"backlinkCount": {
|
|
356
|
+
"type": "integer",
|
|
357
|
+
"minimum": 0,
|
|
358
|
+
"maximum": 9007199254740991
|
|
359
|
+
},
|
|
360
|
+
"wikiLinkCount": {
|
|
361
|
+
"type": "integer",
|
|
362
|
+
"minimum": 0,
|
|
363
|
+
"maximum": 9007199254740991
|
|
364
|
+
},
|
|
365
|
+
"markdownLinkCount": {
|
|
366
|
+
"type": "integer",
|
|
367
|
+
"minimum": 0,
|
|
368
|
+
"maximum": 9007199254740991
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
},
|
|
372
|
+
"mutationBoundary": {
|
|
373
|
+
"type": "object",
|
|
374
|
+
"additionalProperties": false,
|
|
375
|
+
"required": [
|
|
376
|
+
"filesystemCommit",
|
|
377
|
+
"indexConvergence",
|
|
378
|
+
"syncFailureDoesNotRollbackFilesystem"
|
|
379
|
+
],
|
|
380
|
+
"properties": {
|
|
381
|
+
"filesystemCommit": {
|
|
382
|
+
"const": "atomic_all_or_rollback"
|
|
383
|
+
},
|
|
384
|
+
"indexConvergence": {
|
|
385
|
+
"const": "post_commit_separate"
|
|
386
|
+
},
|
|
387
|
+
"syncFailureDoesNotRollbackFilesystem": {
|
|
388
|
+
"const": true
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
}
|
|
@@ -24,6 +24,19 @@ function isTextLikeMime(mime: string): boolean {
|
|
|
24
24
|
return mime.startsWith("text/");
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
+
/**
|
|
28
|
+
* True when a document may carry markdown/wiki/HTML references in mirror text.
|
|
29
|
+
* Used by refactor inventory completeness to fail closed on missing mirrors.
|
|
30
|
+
* Non-text binaries (pdf/images/…) are omitted — they cannot hold those refs.
|
|
31
|
+
*/
|
|
32
|
+
export function isTextLikeReferenceDocument(
|
|
33
|
+
sourceExt: string,
|
|
34
|
+
sourceMime: string
|
|
35
|
+
): boolean {
|
|
36
|
+
const ext = sourceExt.toLowerCase();
|
|
37
|
+
return EDITABLE_EXTENSIONS.has(ext) || isTextLikeMime(sourceMime);
|
|
38
|
+
}
|
|
39
|
+
|
|
27
40
|
export function getDocumentCapabilities(input: {
|
|
28
41
|
sourceExt: string;
|
|
29
42
|
sourceMime: string;
|
package/src/core/file-ops.ts
CHANGED
|
@@ -27,13 +27,25 @@ export async function atomicWrite(
|
|
|
27
27
|
}
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
+
type AtomicCreateContent =
|
|
31
|
+
| string
|
|
32
|
+
| Blob
|
|
33
|
+
| ArrayBuffer
|
|
34
|
+
| Uint8Array
|
|
35
|
+
| ReturnType<typeof Bun.file>;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Exclusively create `path` via temp write + hard-link.
|
|
39
|
+
* EEXIST (including a pre-existing symlink) fails closed without following it.
|
|
40
|
+
*/
|
|
30
41
|
export async function atomicCreate(
|
|
31
42
|
path: string,
|
|
32
|
-
content:
|
|
43
|
+
content: AtomicCreateContent
|
|
33
44
|
): Promise<void> {
|
|
34
45
|
const tempPath = `${path}.tmp.${crypto.randomUUID()}`;
|
|
35
46
|
await Bun.write(tempPath, content);
|
|
36
47
|
try {
|
|
48
|
+
// node:fs/promises link — exclusive create; no Bun equivalent
|
|
37
49
|
await link(tempPath, path);
|
|
38
50
|
} catch (e) {
|
|
39
51
|
await unlink(tempPath).catch(() => {
|
|
@@ -78,6 +90,122 @@ export async function createFolderPath(path: string): Promise<void> {
|
|
|
78
90
|
await mkdir(path, { recursive: true });
|
|
79
91
|
}
|
|
80
92
|
|
|
93
|
+
/** Sibling staging/backup path beside a user file (never under a different root). */
|
|
94
|
+
export function siblingRefactorPath(
|
|
95
|
+
path: string,
|
|
96
|
+
kind: "stage" | "backup",
|
|
97
|
+
token: string
|
|
98
|
+
): string {
|
|
99
|
+
return `${path}.gno-rf-${kind}.${token}`;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Exclusively create a stage artifact. Pre-existing symlink/file at stagePath
|
|
104
|
+
* fails closed (EEXIST) and is never followed/overwritten.
|
|
105
|
+
*/
|
|
106
|
+
export async function writeStagedFileContent(
|
|
107
|
+
stagePath: string,
|
|
108
|
+
content: string
|
|
109
|
+
): Promise<void> {
|
|
110
|
+
await atomicCreate(stagePath, content);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Exclusively copy live bytes into a sibling backup path.
|
|
115
|
+
* Pre-existing symlink/file at backupPath fails closed (EEXIST).
|
|
116
|
+
*/
|
|
117
|
+
export async function backupFileToSibling(
|
|
118
|
+
sourcePath: string,
|
|
119
|
+
backupPath: string
|
|
120
|
+
): Promise<void> {
|
|
121
|
+
await atomicCreate(backupPath, Bun.file(sourcePath));
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Replace an existing path from a staged sibling via rename.
|
|
126
|
+
* Callers must verify live fingerprints before invoking.
|
|
127
|
+
*/
|
|
128
|
+
export async function commitStagedFileReplace(
|
|
129
|
+
stagePath: string,
|
|
130
|
+
targetPath: string
|
|
131
|
+
): Promise<void> {
|
|
132
|
+
await rename(stagePath, targetPath);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Create a new target from a staged sibling without overwrite.
|
|
137
|
+
* Uses hard-link create semantics so an occupied target fails closed.
|
|
138
|
+
*/
|
|
139
|
+
export async function commitStagedFileExclusive(
|
|
140
|
+
stagePath: string,
|
|
141
|
+
targetPath: string
|
|
142
|
+
): Promise<void> {
|
|
143
|
+
// node:fs/promises link — exclusive create; no Bun equivalent
|
|
144
|
+
await link(stagePath, targetPath);
|
|
145
|
+
await unlink(stagePath).catch(() => {
|
|
146
|
+
/* stage cleanup best-effort after durable target exists */
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** @deprecated Prefer commitStagedFileReplace / commitStagedFileExclusive. */
|
|
151
|
+
export async function commitStagedFile(
|
|
152
|
+
stagePath: string,
|
|
153
|
+
targetPath: string
|
|
154
|
+
): Promise<void> {
|
|
155
|
+
await commitStagedFileReplace(stagePath, targetPath);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Restore original bytes by replacing the target path itself (rename),
|
|
160
|
+
* never following a symlink that may have been swapped onto the target.
|
|
161
|
+
*/
|
|
162
|
+
export async function restoreFileFromBackup(
|
|
163
|
+
backupPath: string,
|
|
164
|
+
targetPath: string
|
|
165
|
+
): Promise<void> {
|
|
166
|
+
const tempPath = `${targetPath}.gno-rf-restore.${crypto.randomUUID()}`;
|
|
167
|
+
await Bun.write(tempPath, Bun.file(backupPath));
|
|
168
|
+
try {
|
|
169
|
+
// node:fs/promises rename — replaces the directory entry (symlink-safe)
|
|
170
|
+
await rename(tempPath, targetPath);
|
|
171
|
+
} catch (error) {
|
|
172
|
+
await unlink(tempPath).catch(() => {
|
|
173
|
+
/* ignore cleanup errors */
|
|
174
|
+
});
|
|
175
|
+
throw error;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function isEnoent(error: unknown): boolean {
|
|
180
|
+
return (
|
|
181
|
+
error instanceof Error &&
|
|
182
|
+
"code" in error &&
|
|
183
|
+
(error as NodeJS.ErrnoException).code === "ENOENT"
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** Best-effort cleanup removal; ignores all errors (never used for commit). */
|
|
188
|
+
export async function removePathIfExists(path: string): Promise<void> {
|
|
189
|
+
// node:fs/promises unlink — structure op, no Bun equivalent
|
|
190
|
+
await unlink(path).catch(() => {
|
|
191
|
+
/* ignore cleanup errors */
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Required removal: ignores only ENOENT. Permission/I/O failures throw so
|
|
197
|
+
* callers cannot report success with both source and target present.
|
|
198
|
+
*/
|
|
199
|
+
export async function removePathRequired(path: string): Promise<void> {
|
|
200
|
+
// node:fs/promises unlink — structure op, no Bun equivalent
|
|
201
|
+
try {
|
|
202
|
+
await unlink(path);
|
|
203
|
+
} catch (error) {
|
|
204
|
+
if (isEnoent(error)) return;
|
|
205
|
+
throw error;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
81
209
|
type TrashFileDeps = {
|
|
82
210
|
homeDir?: string;
|
|
83
211
|
platform?: ReturnType<typeof getPlatform>;
|