@wirecat/cli-core 0.725.0 → 0.727.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/CHANGELOG.md +36 -0
- package/dist/crm/manifest/writer.js +10 -6
- package/dist/crm/writes.d.ts +7 -2
- package/dist/crm/writes.js +15 -8
- package/dist/documents/file-mirror-model.d.ts +16 -4
- package/dist/services/files/conflicts.js +24 -10
- package/dist/services/files/mirror.d.ts +2 -0
- package/dist/services/files/mirror.js +44 -9
- package/dist/services/files/versions.d.ts +5 -4
- package/dist/services/files/versions.js +10 -7
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,42 @@ Notable changes to `@wirecat/cli-core` (`@wirecat/cli-messaging` up to 0.261.0,
|
|
|
5
5
|
minor version may break callers, and says how under "Changed — may break callers". `pnpm docs:check` checks the
|
|
6
6
|
shape of this file.
|
|
7
7
|
|
|
8
|
+
## 0.727.0 — 23.11.2026
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
|
|
12
|
+
- **A file mirror works with a remote that rewrites the text it is sent.** A `FileTarget` with `rewrites: true` (Notion,
|
|
13
|
+
which keeps a page as blocks and gives back its own Markdown) no longer turns every upload into a conflict. When an
|
|
14
|
+
upload reads back with other bytes, the mirror keeps the local file's version, writes the remote's form into the
|
|
15
|
+
local file while it is unchanged, and logs it. Both sides then hold the same bytes, so the next sync compares and
|
|
16
|
+
merges against one base. `mirrors conflicts resolve` does the same with the final content. The result lists such
|
|
17
|
+
files in `rewritten` ([DOCUMENTS.md](docs/dev/DOCUMENTS.md)). Without `rewrites`, nothing changes (#3232).
|
|
18
|
+
|
|
19
|
+
### Changed — may break callers
|
|
20
|
+
|
|
21
|
+
- **A file mirror keeps earlier versions for 365 days, not 30.** `fileVersions(dir, now, keepForDays)` takes the
|
|
22
|
+
period, so a CLI passes the profile's `keep_versions_for_days` (`keepVersionsForDays(settings)`), as documents
|
|
23
|
+
history does. Without it the default is 365 days, the owner's rule for every sync. A mirror's version store grows
|
|
24
|
+
for longer: yandex-disk-cli users keep a year of replaced files unless they set a shorter period.
|
|
25
|
+
- **`FileTarget.exists` answers false only for a real deletion.** A provider that cannot tell a deletion from lost
|
|
26
|
+
access throws instead, and the file is left alone as unknown. yandex-disk-cli's target already answers that way.
|
|
27
|
+
It also gets the file's id from the last sync, so a provider that keeps ids across a move can tell a file moved out
|
|
28
|
+
of the folder from a deleted one.
|
|
29
|
+
|
|
30
|
+
## 0.726.0 — 23.11.2026
|
|
31
|
+
|
|
32
|
+
### Added
|
|
33
|
+
|
|
34
|
+
- `CrmWrites.ownedByDefault`: a new item of these kinds is the sign-in's user's when no `--owner` is given, as Attio
|
|
35
|
+
needs for a deal. With no user known, the create is refused before anything is sent.
|
|
36
|
+
- `CrmWrites.self` may be a function, asked only when a write needs the sign-in's user, so a CLI whose sign-in does
|
|
37
|
+
not name that user still checks `--owner` against it.
|
|
38
|
+
|
|
39
|
+
### Fixed
|
|
40
|
+
|
|
41
|
+
- A manifest write that merges a name from its parts sends a cleared part as an empty string beside the given ones,
|
|
42
|
+
as Attio's name needs all three parts: `--name Madonna` and a lone `--first-name` are no longer refused.
|
|
43
|
+
|
|
8
44
|
## 0.725.0 — 23.11.2026
|
|
9
45
|
|
|
10
46
|
### Added
|
|
@@ -59,11 +59,14 @@ const sharedOf = (one) => {
|
|
|
59
59
|
by.set(rule.to, [...(by.get(rule.to) ?? []), field]);
|
|
60
60
|
return [...by.values()].filter((fields) => fields.length > 1);
|
|
61
61
|
};
|
|
62
|
-
/**
|
|
62
|
+
/**
|
|
63
|
+
* A merge of the entries rules that share a key give: each object's keys beside the others'. A part cleared beside a
|
|
64
|
+
* part given is sent empty, not left out: Attio refuses a name without all three of its parts.
|
|
65
|
+
*/
|
|
63
66
|
const merged = (entries, list) => {
|
|
64
|
-
|
|
65
|
-
if (given.length === 0)
|
|
67
|
+
if (entries.every(({ value }) => value === CLEAR))
|
|
66
68
|
return CLEAR;
|
|
69
|
+
const given = entries.map(({ value, blank }) => (value === CLEAR ? blank : value));
|
|
67
70
|
if (given.length === 1)
|
|
68
71
|
return given[0];
|
|
69
72
|
const first = (one) => (list && Array.isArray(one) ? one[0] : one);
|
|
@@ -175,7 +178,7 @@ export const crmWriterFromManifest = (input, options) => {
|
|
|
175
178
|
const fields = written;
|
|
176
179
|
const given = kind === "person" ? namesOf(one, fields) : fields;
|
|
177
180
|
const parts = new Map();
|
|
178
|
-
const add = (to, list,
|
|
181
|
+
const add = (to, list, value, blank = value) => parts.set(to, { list, entries: [...(parts.get(to)?.entries ?? []), { value, blank }] });
|
|
179
182
|
for (const [field, value] of Object.entries(given)) {
|
|
180
183
|
if (value === undefined || field === "custom")
|
|
181
184
|
continue;
|
|
@@ -194,7 +197,7 @@ export const crmWriterFromManifest = (input, options) => {
|
|
|
194
197
|
throw refused(`${service} keeps a ${kind}'s amount in ${currency}; give that ${field} or none`, field);
|
|
195
198
|
continue;
|
|
196
199
|
}
|
|
197
|
-
add(rule.to, rule.list === true, encode(kind, field, rule, value));
|
|
200
|
+
add(rule.to, rule.list === true, encode(kind, field, rule, value), value === null ? encode(kind, field, rule, "") : undefined);
|
|
198
201
|
}
|
|
199
202
|
const custom = Object.entries(given.custom ?? {});
|
|
200
203
|
if (custom.length && !one.custom)
|
|
@@ -285,13 +288,14 @@ export const crmWriterFromManifest = (input, options) => {
|
|
|
285
288
|
* `api` keys of each operation it sends as `alsoChecks`. The writes' guard asks before each one and journals it.
|
|
286
289
|
*/
|
|
287
290
|
export const crmManifestWrites = (input, options) => {
|
|
288
|
-
const { manifest: api, service, source, app, settings, self, env, warn } = options;
|
|
291
|
+
const { manifest: api, service, source, app, settings, self, ownedByDefault, env, warn } = options;
|
|
289
292
|
return {
|
|
290
293
|
writer: crmWriterFromManifest(input, { api, service, send: serviceApiSend(options), source }),
|
|
291
294
|
alsoChecks: crmManifestWriteKeys(input, api, options.bodyPermissions),
|
|
292
295
|
app,
|
|
293
296
|
settings,
|
|
294
297
|
...(self === undefined ? {} : { self }),
|
|
298
|
+
...(ownedByDefault === undefined ? {} : { ownedByDefault }),
|
|
295
299
|
...(env === undefined ? {} : { env }),
|
|
296
300
|
...(warn === undefined ? {} : { warn }),
|
|
297
301
|
};
|
package/dist/crm/writes.d.ts
CHANGED
|
@@ -12,8 +12,13 @@ import { type CrmVerb, type CrmWriter } from "./writer.js";
|
|
|
12
12
|
/** What a CLI hands the CRM writes for one profile. */
|
|
13
13
|
export interface CrmWrites extends ProfileWrites {
|
|
14
14
|
writer: CrmWriter;
|
|
15
|
-
/**
|
|
16
|
-
|
|
15
|
+
/**
|
|
16
|
+
* The CRM user the sign-in acts as, or how to ask the CRM for it when a write needs it. An owner other than it
|
|
17
|
+
* checks `crm.assign-others`; left out, or answered empty, every owner does.
|
|
18
|
+
*/
|
|
19
|
+
self?: string | (() => Promise<string | undefined>);
|
|
20
|
+
/** The kinds a new item of which is the sign-in's user's when no --owner is given: Attio refuses a deal with none. */
|
|
21
|
+
ownedByDefault?: readonly CrmKind[];
|
|
17
22
|
/**
|
|
18
23
|
* The CLI's own keys a write also checks, such as Bitrix24's `api.crm.deal.update`; pass the same function to
|
|
19
24
|
* `crmAlsoChecks` so `--dry-run` names them.
|
package/dist/crm/writes.js
CHANGED
|
@@ -238,13 +238,14 @@ export const crmCustomValues = (fields, pairs) => {
|
|
|
238
238
|
/** `--field` pairs by the field definitions the last sync stored. */
|
|
239
239
|
const customOf = async (context, kind, pairs) => pairs?.length ? crmCustomValues(await context.store.crm.fields(context.account, kind), pairs) : undefined;
|
|
240
240
|
/** Every key the write checks: its own, and each action its fields also take (WC-PERM-22). */
|
|
241
|
-
const
|
|
241
|
+
const selfOf = async ({ writes: { self } }) => (typeof self === "function" ? await self() : self) || undefined;
|
|
242
|
+
const keysOf = async (context, kind, verb, { stage, owner }) => {
|
|
242
243
|
const extra = [];
|
|
243
244
|
if (stage && verb !== "move")
|
|
244
245
|
extra.push(keyOf(kind, "move"));
|
|
245
246
|
if (stage && stage.outcome !== "open")
|
|
246
247
|
extra.push(keyOf(kind, "close"));
|
|
247
|
-
if (owner != null && owner !== context
|
|
248
|
+
if (owner != null && owner !== (await selfOf(context)))
|
|
248
249
|
extra.push(CRM_ASSIGN_OTHERS_KEY);
|
|
249
250
|
return { key: keyOf(kind, verb), extra: [...extra, ...(context.writes.alsoChecks?.(kind, verb) ?? [])] };
|
|
250
251
|
};
|
|
@@ -314,8 +315,14 @@ export const createCrmItem = async (context, kind, input, options = {}) => {
|
|
|
314
315
|
const custom = await customOf(context, kind, input.custom);
|
|
315
316
|
if (custom)
|
|
316
317
|
fields.custom = custom;
|
|
318
|
+
if (fields.owner === undefined && context.writes.ownedByDefault?.includes(kind)) {
|
|
319
|
+
const self = await selfOf(context);
|
|
320
|
+
if (self === undefined)
|
|
321
|
+
throw invalid(`a new ${kind} needs --owner: the sign-in names no CRM user`, "--owner");
|
|
322
|
+
fields.owner = self;
|
|
323
|
+
}
|
|
317
324
|
const operationId = operationOf(context, options.operationId);
|
|
318
|
-
const item = await written(context, keysOf(context, kind, "create", { stage, owner: fields.owner }), operationId, () => writer.create(kind, fields, { operationId }));
|
|
325
|
+
const item = await written(context, await keysOf(context, kind, "create", { stage, owner: fields.owner }), operationId, () => writer.create(kind, fields, { operationId }));
|
|
319
326
|
return { operationId, item: await kept(context, kind, item, operationId) };
|
|
320
327
|
};
|
|
321
328
|
export const updateCrmItem = async (context, kind, reference, input, options = {}) => {
|
|
@@ -334,7 +341,7 @@ export const updateCrmItem = async (context, kind, reference, input, options = {
|
|
|
334
341
|
if (Object.keys(fields).length === 0)
|
|
335
342
|
throw invalid("nothing to change: name a field");
|
|
336
343
|
const operationId = operationOf(context, options.operationId);
|
|
337
|
-
const item = await written(context, keysOf(context, kind, "update", { stage, owner: fields.owner }), operationId, () => writer.update(kind, copy.externalId, fields, { operationId }));
|
|
344
|
+
const item = await written(context, await keysOf(context, kind, "update", { stage, owner: fields.owner }), operationId, () => writer.update(kind, copy.externalId, fields, { operationId }));
|
|
338
345
|
return { operationId, item: await kept(context, kind, item, operationId) };
|
|
339
346
|
};
|
|
340
347
|
export const moveCrmItem = async (context, kind, reference, stageName, options = {}) => {
|
|
@@ -343,7 +350,7 @@ export const moveCrmItem = async (context, kind, reference, stageName, options =
|
|
|
343
350
|
const copy = await copyOf(context, kind, reference);
|
|
344
351
|
const stage = stageOf(await pipelineOf(context, kind, copy.pipeline), stageName);
|
|
345
352
|
const operationId = operationOf(context, options.operationId);
|
|
346
|
-
const item = await written(context, keysOf(context, kind, "move", { stage }), operationId, () => writer.move(kind, copy.externalId, stage.externalId, { operationId }));
|
|
353
|
+
const item = await written(context, await keysOf(context, kind, "move", { stage }), operationId, () => writer.move(kind, copy.externalId, stage.externalId, { operationId }));
|
|
347
354
|
return { operationId, item: await kept(context, kind, item, operationId) };
|
|
348
355
|
};
|
|
349
356
|
export const closeCrmItem = async (context, kind, reference, outcome, options = {}) => {
|
|
@@ -356,7 +363,7 @@ export const closeCrmItem = async (context, kind, reference, outcome, options =
|
|
|
356
363
|
if (reason !== undefined && (reason === "" || reason.length > 2000))
|
|
357
364
|
throw invalid("--reason is 1 to 2000 characters", "--reason");
|
|
358
365
|
const operationId = operationOf(context, options.operationId);
|
|
359
|
-
const item = await written(context, keysOf(context, kind, "close", {}), operationId, () => writer.close(kind, copy.externalId, outcome, { operationId, ...(reason ? { reason } : {}) }));
|
|
366
|
+
const item = await written(context, await keysOf(context, kind, "close", {}), operationId, () => writer.close(kind, copy.externalId, outcome, { operationId, ...(reason ? { reason } : {}) }));
|
|
360
367
|
return { operationId, item: await kept(context, kind, item, operationId) };
|
|
361
368
|
};
|
|
362
369
|
export const removeCrmItem = async (context, kind, reference, options = {}) => {
|
|
@@ -364,7 +371,7 @@ export const removeCrmItem = async (context, kind, reference, options = {}) => {
|
|
|
364
371
|
supported(writer, kind, "remove");
|
|
365
372
|
const copy = await copyOf(context, kind, reference);
|
|
366
373
|
const operationId = operationOf(context, options.operationId);
|
|
367
|
-
await written(context, keysOf(context, kind, "remove", {}), operationId, () => writer.remove(kind, copy.externalId, { operationId }));
|
|
374
|
+
await written(context, await keysOf(context, kind, "remove", {}), operationId, () => writer.remove(kind, copy.externalId, { operationId }));
|
|
368
375
|
const removed = await context.store.crm.remove(context.account, kind, [copy.externalId]);
|
|
369
376
|
return { operationId, ref: copy.ref, removed };
|
|
370
377
|
};
|
|
@@ -450,7 +457,7 @@ const writeRecord = async (context, kind, verb, fields, operationId, act) => {
|
|
|
450
457
|
if (!context.source)
|
|
451
458
|
throw new CliError("configuration_error", "a contact or company write needs the CRM source to tell your clients");
|
|
452
459
|
const rule = foreignContacts(context.source(), undefined);
|
|
453
|
-
const record = await written(context, keysOf(context, kind, verb, { owner: fields.owner }), operationId, act);
|
|
460
|
+
const record = await written(context, await keysOf(context, kind, verb, { owner: fields.owner }), operationId, act);
|
|
454
461
|
const stored = await rule.own(kind, record);
|
|
455
462
|
let removed;
|
|
456
463
|
try {
|
|
@@ -43,13 +43,19 @@ export interface FileTarget {
|
|
|
43
43
|
next?: string;
|
|
44
44
|
} | undefined>;
|
|
45
45
|
/**
|
|
46
|
-
* Whether anything is at `path` now, read directly rather than from a listing; false only when the provider says
|
|
47
|
-
*
|
|
46
|
+
* Whether anything is at `path` now, read directly rather than from a listing; false only when the provider says the
|
|
47
|
+
* file was deleted. A file missing from a listing is deleted only once this says so. An answer that cannot tell a
|
|
48
|
+
* deletion from lost access (a 404 that may mean "no longer shared") throws, and the file is left alone. `id` is
|
|
49
|
+
* the file's id at the last sync: a provider that keeps a file's id across a move asks by it, so a file moved out of
|
|
50
|
+
* the folder is still there, not deleted.
|
|
48
51
|
*/
|
|
49
|
-
exists(path: string, signal?: AbortSignal): Promise<boolean>;
|
|
52
|
+
exists(path: string, signal?: AbortSignal, id?: string): Promise<boolean>;
|
|
50
53
|
/** Streams the file into `into`, a new local file. */
|
|
51
54
|
download(path: string, into: string, signal?: AbortSignal): Promise<void>;
|
|
52
|
-
/**
|
|
55
|
+
/**
|
|
56
|
+
* Streams the local file `from` to `path` and returns the file as the remote now has it. With `rewrites`, its hash
|
|
57
|
+
* is of the remote's own form of the text, the bytes `download` gives.
|
|
58
|
+
*/
|
|
53
59
|
upload(path: string, from: string, expect: Expect, signal?: AbortSignal): Promise<RemoteFile>;
|
|
54
60
|
/** Moves the file to the remote's trash, where the owner can restore it. */
|
|
55
61
|
trash(path: string, expect: Exclude<Expect, {
|
|
@@ -57,6 +63,12 @@ export interface FileTarget {
|
|
|
57
63
|
}>, signal?: AbortSignal): Promise<void>;
|
|
58
64
|
/** Creates the folder and its parents; a folder that exists is fine. */
|
|
59
65
|
ensureFolder(path: string, signal?: AbortSignal): Promise<void>;
|
|
66
|
+
/**
|
|
67
|
+
* The remote keeps its own form of the text it is sent (Notion's Markdown), so an upload may read back with another
|
|
68
|
+
* hash. The mirror then writes that form into the local file, the earlier one kept, and both sides hold the same
|
|
69
|
+
* bytes again. Without it, such an upload is a conflict.
|
|
70
|
+
*/
|
|
71
|
+
rewrites?: boolean;
|
|
60
72
|
/** A value that moves with every change on the remote, when the provider has one: a watch reads it before listing. */
|
|
61
73
|
revision?(signal?: AbortSignal): Promise<string>;
|
|
62
74
|
}
|
|
@@ -106,25 +106,33 @@ export const resolveFileConflict = async (options) => {
|
|
|
106
106
|
if (parent !== ".")
|
|
107
107
|
await write("folder", "remote", parent, () => target.ensureFolder(remotePath(parent), signal));
|
|
108
108
|
const sent = await write("absent" in expect ? "create" : "update", "remote", at, () => target.upload(remotePath(at), from, expect, signal));
|
|
109
|
-
if (sent.sha256
|
|
109
|
+
if (sent.sha256 === undefined || sent.sha256 === hash)
|
|
110
|
+
return { sent, settled: hash };
|
|
111
|
+
if (!target.rewrites)
|
|
110
112
|
throw new CliError("conflict", "the file changed during the upload", { reason: "upload.mismatch" });
|
|
111
|
-
|
|
113
|
+
// The remote keeps its own form of the text: the local side takes that form, so both hold the same bytes.
|
|
114
|
+
const form = await versions.keep((temp) => target.download(remotePath(at), temp, signal));
|
|
115
|
+
if (form !== sent.sha256)
|
|
116
|
+
throw changed(`the ${options.remote} file`, "mirror.remote-changed");
|
|
117
|
+
return { sent, settled: form };
|
|
112
118
|
};
|
|
113
119
|
const expectRemote = found.remote
|
|
114
120
|
? { sha256: found.remote, ...(found.remoteFile?.md5 ? { md5: found.remoteFile.md5 } : {}) }
|
|
115
121
|
: { absent: true };
|
|
116
122
|
let sent = final === found.remote ? found.remoteFile : undefined;
|
|
123
|
+
/** What both sides hold once resolved: `final`, or the remote's own form of it. */
|
|
124
|
+
let settled = final;
|
|
117
125
|
if (final !== found.remote) {
|
|
118
126
|
if (final === undefined) {
|
|
119
127
|
await write("delete", "remote", path, () => target.trash(remotePath(path), expectRemote, signal));
|
|
120
128
|
}
|
|
121
129
|
else
|
|
122
|
-
sent = await send(path, "file" in choice ? choice.file : absolute, final, expectRemote);
|
|
130
|
+
({ sent, settled } = await send(path, "file" in choice ? choice.file : absolute, final, expectRemote));
|
|
123
131
|
}
|
|
124
|
-
if (
|
|
125
|
-
const placed =
|
|
132
|
+
if (settled !== found.local) {
|
|
133
|
+
const placed = settled === undefined
|
|
126
134
|
? await write("delete", "local", path, () => trashLocal(root, path, found.local, date))
|
|
127
|
-
: await write(found.local ? "update" : "create", "local", path, () => versions.write(
|
|
135
|
+
: await write(found.local ? "update" : "create", "local", path, () => versions.write(settled, absolute, found.local));
|
|
128
136
|
if (!placed)
|
|
129
137
|
throw changed("the local file", "mirror.local-changed");
|
|
130
138
|
}
|
|
@@ -143,16 +151,20 @@ export const resolveFileConflict = async (options) => {
|
|
|
143
151
|
syncedAt: new Date(now()).toISOString(),
|
|
144
152
|
};
|
|
145
153
|
};
|
|
146
|
-
if (
|
|
154
|
+
if (settled === undefined)
|
|
147
155
|
delete state.entries[path];
|
|
148
156
|
else if (sent)
|
|
149
|
-
await entry(path, sent,
|
|
157
|
+
await entry(path, sent, settled);
|
|
150
158
|
if (keep === "both" && found.local && found.remote) {
|
|
151
159
|
const name = copyName(path, options.remote, date, (at) => existsSync(join(root, at)) || at in state.entries);
|
|
152
160
|
await assertNoLink(root, name);
|
|
153
161
|
if (!(await write("create", "local", name, () => versions.write(found.remote, join(root, name)))))
|
|
154
162
|
throw changed("the local folder", "mirror.local-changed");
|
|
155
|
-
|
|
163
|
+
const copied = await send(name, join(root, name), found.remote, { absent: true });
|
|
164
|
+
if (copied.settled !== found.remote &&
|
|
165
|
+
!(await write("update", "local", name, () => versions.write(copied.settled, join(root, name), found.remote))))
|
|
166
|
+
throw changed("the local folder", "mirror.local-changed");
|
|
167
|
+
await entry(name, copied.sent, copied.settled);
|
|
156
168
|
copy = name;
|
|
157
169
|
}
|
|
158
170
|
if (state.conflicts) {
|
|
@@ -175,7 +187,9 @@ export const resolveFileConflict = async (options) => {
|
|
|
175
187
|
...(final ? { new: final } : {}),
|
|
176
188
|
resolution,
|
|
177
189
|
});
|
|
178
|
-
|
|
190
|
+
if (final && settled && settled !== final)
|
|
191
|
+
versions.log({ path, event: "changed", direction: "remote-to-local", old: final, from: "local", new: settled });
|
|
192
|
+
return { path, resolution, sha256: settled ?? null, ...(copy ? { copy } : {}) };
|
|
179
193
|
}
|
|
180
194
|
finally {
|
|
181
195
|
await held.release();
|
|
@@ -89,6 +89,8 @@ export interface FileMirrorResult {
|
|
|
89
89
|
downloaded: string[];
|
|
90
90
|
/** Text files both sides changed, merged and written to both. */
|
|
91
91
|
merged: string[];
|
|
92
|
+
/** Local files given the remote's own form of what was just uploaded, for a target that `rewrites`. */
|
|
93
|
+
rewritten?: string[];
|
|
92
94
|
trashed: {
|
|
93
95
|
path: string;
|
|
94
96
|
side: Side;
|
|
@@ -287,7 +287,7 @@ export const syncFileMirror = async (options) => {
|
|
|
287
287
|
const absent = steps.filter((step) => step.kind === "trash-local");
|
|
288
288
|
for (let at = 0; at < absent.length; at += CHECKS)
|
|
289
289
|
await Promise.all(absent.slice(at, at + CHECKS).map(async (step) => {
|
|
290
|
-
const why = await target.exists(remotePath(step.path), signal).then((there) => (there ? `missing from the ${options.remote} listing, but still there` : undefined), (error) => {
|
|
290
|
+
const why = await target.exists(remotePath(step.path), signal, step.base?.id).then((there) => (there ? `missing from the ${options.remote} listing, but still there` : undefined), (error) => {
|
|
291
291
|
if (signal?.aborted)
|
|
292
292
|
throw error;
|
|
293
293
|
return `the ${options.remote} could not say whether it was deleted: ${options.errorOf(error).message}`;
|
|
@@ -484,21 +484,56 @@ export const syncFileMirror = async (options) => {
|
|
|
484
484
|
folders.add(up);
|
|
485
485
|
}
|
|
486
486
|
const sent = await write(created ? "create" : "update", "remote", path, () => target.upload(remotePath(path), from.absolute, expect, signal));
|
|
487
|
-
|
|
487
|
+
const rewritten = sent.sha256 !== undefined && sent.sha256 !== from.hash;
|
|
488
|
+
if (rewritten && !target.rewrites)
|
|
488
489
|
throw new CliError("conflict", "the local file changed during the upload", { reason: "upload.mismatch" });
|
|
490
|
+
if (!merged) {
|
|
491
|
+
versions.log({
|
|
492
|
+
path,
|
|
493
|
+
event: created ? "created" : "changed",
|
|
494
|
+
direction: "local-to-remote",
|
|
495
|
+
...("sha256" in expect ? { old: expect.sha256, from: "remote" } : {}),
|
|
496
|
+
new: from.hash,
|
|
497
|
+
size: from.size,
|
|
498
|
+
});
|
|
499
|
+
result.uploaded.push(path);
|
|
500
|
+
}
|
|
501
|
+
if (rewritten)
|
|
502
|
+
return takeRewrite(path, sent, from);
|
|
489
503
|
record(path, sent, from, from.hash);
|
|
490
504
|
await keepBase(path, from.absolute, from.hash, from.size);
|
|
491
|
-
|
|
505
|
+
};
|
|
506
|
+
/**
|
|
507
|
+
* Puts the remote's own form of an upload into the local file, so both sides hold the same bytes and the next
|
|
508
|
+
* merge has one base. The local file keeps its version first; one saved meanwhile stays, sent by the next sync.
|
|
509
|
+
*/
|
|
510
|
+
const takeRewrite = async (path, sent, from) => {
|
|
511
|
+
const form = sent.sha256;
|
|
512
|
+
const kept = await versions.keep((temp) => target.download(remotePath(path), temp, signal));
|
|
513
|
+
if (kept !== form)
|
|
514
|
+
throw new CliError("conflict", `the ${options.remote} file changed right after the upload`, {
|
|
515
|
+
reason: "mirror.remote-changed",
|
|
516
|
+
});
|
|
517
|
+
const absolute = join(root, path);
|
|
518
|
+
await assertNoLink(root, path);
|
|
519
|
+
const old = await versions.keep(absolute);
|
|
520
|
+
const placed = old === from.hash && (await write("update", "local", path, () => versions.write(form, absolute, from.hash)));
|
|
521
|
+
if (!placed) {
|
|
522
|
+
// A local size no file has: the file is hashed again and sent, never taken for the remote's form.
|
|
523
|
+
record(path, sent, { size: -1, mtimeMs: -1 }, form);
|
|
492
524
|
return;
|
|
525
|
+
}
|
|
526
|
+
record(path, sent, await stat(absolute), form);
|
|
493
527
|
versions.log({
|
|
494
528
|
path,
|
|
495
|
-
event:
|
|
496
|
-
direction: "
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
529
|
+
event: "changed",
|
|
530
|
+
direction: "remote-to-local",
|
|
531
|
+
old,
|
|
532
|
+
from: "local",
|
|
533
|
+
new: form,
|
|
534
|
+
size: sent.size,
|
|
500
535
|
});
|
|
501
|
-
result.
|
|
536
|
+
result.rewritten = [...(result.rewritten ?? []), path];
|
|
502
537
|
};
|
|
503
538
|
/**
|
|
504
539
|
* False when the local file changed (or appeared) during the sync, so nothing was written. Without `recorded`, the
|
|
@@ -75,11 +75,12 @@ export interface FileVersions {
|
|
|
75
75
|
/**
|
|
76
76
|
* A mirror's local version store: `<dir>/objects/<sha256>` holds each kept content once, `<dir>/log.jsonl` is
|
|
77
77
|
* append-only. The store lives under the profile's state folder, never inside the mirrored folder. Retention: a
|
|
78
|
-
* content stays while an event newer than
|
|
79
|
-
*
|
|
80
|
-
*
|
|
78
|
+
* content stays while an event newer than `keepForDays` (the profile's `keep_versions_for_days`, 365 by default) names
|
|
79
|
+
* it; the last content of a path whose newest event is a deletion, and every content a merge, a conflict or a
|
|
80
|
+
* resolution names, stay for good. A content no event names stays as long after it was last kept, so a crash between
|
|
81
|
+
* keeping a file and logging the change loses nothing.
|
|
81
82
|
*/
|
|
82
|
-
export declare const fileVersions: (dir: string, now?: () => number) => FileVersions;
|
|
83
|
+
export declare const fileVersions: (dir: string, now?: () => number, keepForDays?: number) => FileVersions;
|
|
83
84
|
/**
|
|
84
85
|
* Puts a kept version of `path` back into the mirrored folder; the file there now is kept first. The next sync sends
|
|
85
86
|
* it like any local change. False when the store no longer has the version or the file changed meanwhile. Runs under
|
|
@@ -7,7 +7,8 @@ import { pipeline } from "node:stream/promises";
|
|
|
7
7
|
import { CliError } from "@wirecat/cli-base";
|
|
8
8
|
import { heldCheck } from "../documents/mirrors.js";
|
|
9
9
|
import { assertNoLink, hashFile, replaceLocal, safePath } from "../documents/push.js";
|
|
10
|
-
|
|
10
|
+
import { KEEP_VERSIONS_FOR_DAYS } from "../documents/sync.js";
|
|
11
|
+
const DAY_MS = 24 * 60 * 60 * 1000;
|
|
11
12
|
/** Errors of the local file system: a writer that fails with one could not write the store, whatever it was reading. */
|
|
12
13
|
const LOCAL_FAILURES = new Set(["ENOSPC", "EDQUOT", "EROFS", "EACCES", "EPERM", "EIO", "EFBIG", "EMFILE", "ENFILE"]);
|
|
13
14
|
const notKept = (error) => {
|
|
@@ -50,11 +51,13 @@ const rolesOf = (event) => {
|
|
|
50
51
|
/**
|
|
51
52
|
* A mirror's local version store: `<dir>/objects/<sha256>` holds each kept content once, `<dir>/log.jsonl` is
|
|
52
53
|
* append-only. The store lives under the profile's state folder, never inside the mirrored folder. Retention: a
|
|
53
|
-
* content stays while an event newer than
|
|
54
|
-
*
|
|
55
|
-
*
|
|
54
|
+
* content stays while an event newer than `keepForDays` (the profile's `keep_versions_for_days`, 365 by default) names
|
|
55
|
+
* it; the last content of a path whose newest event is a deletion, and every content a merge, a conflict or a
|
|
56
|
+
* resolution names, stay for good. A content no event names stays as long after it was last kept, so a crash between
|
|
57
|
+
* keeping a file and logging the change loses nothing.
|
|
56
58
|
*/
|
|
57
|
-
export const fileVersions = (dir, now = Date.now) => {
|
|
59
|
+
export const fileVersions = (dir, now = Date.now, keepForDays = KEEP_VERSIONS_FOR_DAYS) => {
|
|
60
|
+
const retainMs = keepForDays * DAY_MS;
|
|
58
61
|
const objects = join(dir, "objects");
|
|
59
62
|
const logPath = join(dir, "log.jsonl");
|
|
60
63
|
const events = () => {
|
|
@@ -140,7 +143,7 @@ export const fileVersions = (dir, now = Date.now) => {
|
|
|
140
143
|
const last = new Map();
|
|
141
144
|
for (const event of all) {
|
|
142
145
|
last.set(event.path, event);
|
|
143
|
-
if (Date.parse(event.at) > now() -
|
|
146
|
+
if (Date.parse(event.at) > now() - retainMs)
|
|
144
147
|
for (const hash of [event.old, event.new])
|
|
145
148
|
if (hash)
|
|
146
149
|
keep.add(hash);
|
|
@@ -166,7 +169,7 @@ export const fileVersions = (dir, now = Date.now) => {
|
|
|
166
169
|
return undefined;
|
|
167
170
|
throw error;
|
|
168
171
|
});
|
|
169
|
-
if (kept && kept.mtimeMs <= now() -
|
|
172
|
+
if (kept && kept.mtimeMs <= now() - retainMs)
|
|
170
173
|
await rm(object, { force: true });
|
|
171
174
|
}
|
|
172
175
|
},
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wirecat/cli-core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.727.0",
|
|
4
4
|
"description": "The messenger-neutral half of a messaging CLI: domain model, message locators, rendering, name resolution, the SQLite seam, the command skeleton and shared read commands, the send guard, run records and the message store",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "Viacheslav Ptsarev",
|