micloud-cli 0.0.0-stage → 0.5.1
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 +661 -0
- package/README.en.md +118 -0
- package/README.md +117 -2
- package/dist/cli.js +59 -0
- package/dist/commands/account.js +76 -0
- package/dist/core/account.js +22 -0
- package/dist/core/auth.js +338 -0
- package/dist/core/config.js +81 -0
- package/dist/core/http.js +172 -0
- package/dist/core/lock.js +90 -0
- package/dist/core/output.js +137 -0
- package/dist/core/template.js +39 -0
- package/dist/core/utils.js +205 -0
- package/dist/note/client.js +298 -0
- package/dist/note/commands/create.js +44 -0
- package/dist/note/commands/delete.js +32 -0
- package/dist/note/commands/export.js +20 -0
- package/dist/note/commands/folder.js +101 -0
- package/dist/note/commands/get.js +35 -0
- package/dist/note/commands/list.js +57 -0
- package/dist/note/commands/move.js +37 -0
- package/dist/note/commands/pin.js +41 -0
- package/dist/note/commands/search.js +45 -0
- package/dist/note/commands/shared.js +37 -0
- package/dist/note/commands/sync.js +217 -0
- package/dist/note/commands/update.js +61 -0
- package/dist/note/commands/upload-image.js +27 -0
- package/dist/note/config.js +166 -0
- package/dist/note/converter.js +664 -0
- package/dist/note/extra-info.js +23 -0
- package/dist/note/images.js +100 -0
- package/dist/note/register.js +172 -0
- package/dist/note/sync-diff.js +143 -0
- package/dist/note/sync.js +748 -0
- package/dist/note/text-diff.js +71 -0
- package/dist/note/types.js +2 -0
- package/dist/recording/client.js +115 -0
- package/dist/recording/commands/config.js +73 -0
- package/dist/recording/commands/delete.js +171 -0
- package/dist/recording/commands/list.js +161 -0
- package/dist/recording/commands/shared.js +47 -0
- package/dist/recording/commands/transfer.js +192 -0
- package/dist/recording/commands/trash.js +140 -0
- package/dist/recording/config.js +104 -0
- package/dist/recording/dates.js +61 -0
- package/dist/recording/download.js +308 -0
- package/dist/recording/progress.js +77 -0
- package/dist/recording/pull.js +433 -0
- package/dist/recording/recording.js +294 -0
- package/dist/recording/register.js +202 -0
- package/dist/recording/template.js +44 -0
- package/dist/recording/types.js +2 -0
- package/package.json +53 -4
|
@@ -0,0 +1,748 @@
|
|
|
1
|
+
import { readFile, writeFile, rm, readdir } from "node:fs/promises";
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
|
+
import { createRequire } from "node:module";
|
|
4
|
+
import { basename, dirname, join, relative, sep, resolve as resolvePath } from "node:path";
|
|
5
|
+
import { parseNoteEntry, xmlToMarkdown, markdownToXml, extractSnippet, getNoteFilePath, buildImageMap, } from "./converter.js";
|
|
6
|
+
import { uploadLocalImages, withAttachments } from "./images.js";
|
|
7
|
+
import { buildExtraInfoString } from "./extra-info.js";
|
|
8
|
+
import { randomDelay, ensureFileDir, fileExists, ensureDir, sanitizeFileName, getCacheDir, } from "../core/utils.js";
|
|
9
|
+
import { logInfo } from "../core/output.js";
|
|
10
|
+
import { loadUserConfig, loadState, saveState, toOutputRel, toOutputAbs, } from "./config.js";
|
|
11
|
+
import { classify, decide } from "./sync-diff.js";
|
|
12
|
+
const SAVE_INTERVAL = 10;
|
|
13
|
+
const TOOL_VERSION = createRequire(import.meta.url)("../../package.json").version;
|
|
14
|
+
export function computeHash(content) {
|
|
15
|
+
return createHash("sha256").update(content, "utf-8").digest("hex");
|
|
16
|
+
}
|
|
17
|
+
// ============================================================
|
|
18
|
+
// 落盘路径:同名笔记去重
|
|
19
|
+
// ============================================================
|
|
20
|
+
/** 路径比较键:macOS / Windows 默认文件系统大小写不敏感 */
|
|
21
|
+
function pathKey(p) {
|
|
22
|
+
const abs = resolvePath(p);
|
|
23
|
+
return process.platform === "darwin" || process.platform === "win32"
|
|
24
|
+
? abs.toLowerCase()
|
|
25
|
+
: abs;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* 笔记文件里附件引用的前缀:附件统一存放在 `<output>/assets/`,
|
|
29
|
+
* 子目录(云端文件夹)里的笔记需要 `../assets/` 才能被 Markdown 查看器正确解析。
|
|
30
|
+
*/
|
|
31
|
+
export function assetPrefixFor(filePath, outputDir) {
|
|
32
|
+
const rel = relative(dirname(filePath), join(outputDir, "assets"));
|
|
33
|
+
return `${rel.split(sep).join("/")}/`;
|
|
34
|
+
}
|
|
35
|
+
/** 下载笔记附件到 `<output>/assets/`(已存在的跳过),返回下载失败的文件名 */
|
|
36
|
+
async function downloadAttachments(client, files, outputDir) {
|
|
37
|
+
const failed = [];
|
|
38
|
+
for (const f of files) {
|
|
39
|
+
if (!(await client.downloadFile(f.fileId, join(outputDir, "assets", f.name)))) {
|
|
40
|
+
failed.push(f.name);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return failed;
|
|
44
|
+
}
|
|
45
|
+
function attachmentError(failed) {
|
|
46
|
+
return `附件下载失败:${failed.join("、")}(正文已写入)`;
|
|
47
|
+
}
|
|
48
|
+
/** 同名冲突时的备用路径:`<name>_<id>.md`(id 唯一,结果稳定) */
|
|
49
|
+
export function withIdSuffix(filePath, id) {
|
|
50
|
+
const base = filePath.endsWith(".md") ? filePath.slice(0, -3) : filePath;
|
|
51
|
+
return `${base}_${id}.md`;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* 为一批笔记分配互不冲突的落盘路径(export 用,无状态故需确定性):
|
|
55
|
+
* 同一路径下创建最早的笔记保留原名,其余加 id 后缀。
|
|
56
|
+
*/
|
|
57
|
+
export function assignUniquePaths(items) {
|
|
58
|
+
const groups = new Map();
|
|
59
|
+
for (const it of items) {
|
|
60
|
+
const key = pathKey(it.desired);
|
|
61
|
+
const group = groups.get(key);
|
|
62
|
+
if (group)
|
|
63
|
+
group.push(it);
|
|
64
|
+
else
|
|
65
|
+
groups.set(key, [it]);
|
|
66
|
+
}
|
|
67
|
+
const result = new Map();
|
|
68
|
+
for (const group of groups.values()) {
|
|
69
|
+
group.sort((a, b) => (a.createDate ?? 0) - (b.createDate ?? 0) || a.id.localeCompare(b.id));
|
|
70
|
+
group.forEach((it, i) => {
|
|
71
|
+
result.set(it.id, i === 0 ? it.desired : withIdSuffix(it.desired, it.id));
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
return result;
|
|
75
|
+
}
|
|
76
|
+
/** 某个导出目录对应的增量缓存文件路径 */
|
|
77
|
+
export function getExportCachePath(outputDir) {
|
|
78
|
+
const key = createHash("sha1").update(resolvePath(outputDir)).digest("hex").slice(0, 16);
|
|
79
|
+
return join(getCacheDir(), "export", `${key}.json`);
|
|
80
|
+
}
|
|
81
|
+
async function loadExportCache(cachePath, template) {
|
|
82
|
+
try {
|
|
83
|
+
const cache = JSON.parse(await readFile(cachePath, "utf-8"));
|
|
84
|
+
if (cache.version !== TOOL_VERSION || cache.template !== template)
|
|
85
|
+
return {};
|
|
86
|
+
return cache.notes ?? {};
|
|
87
|
+
}
|
|
88
|
+
catch {
|
|
89
|
+
return {};
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* 导出全部笔记为本地 Markdown(单向、只读云端)。
|
|
94
|
+
*
|
|
95
|
+
* - 拉取云端全部笔记 → 转 Markdown → 写本地(按文件夹组织)
|
|
96
|
+
* - 下载附件到 assets/
|
|
97
|
+
* - 增量:云端未变且本地文件未被改动的笔记直接跳过(见 ExportCacheEntry);force=true 时全量重来
|
|
98
|
+
* - 同名笔记不互相覆盖:创建最早的保留原名,其余加 `_<id>` 后缀
|
|
99
|
+
* - 不删除任何云端笔记;不因本地缺失而回写云端
|
|
100
|
+
*/
|
|
101
|
+
export async function exportNotes(client, outputDir, force = false, quiet = false, cachePath = getExportCachePath(outputDir)) {
|
|
102
|
+
const log = (msg) => {
|
|
103
|
+
if (!quiet)
|
|
104
|
+
logInfo(msg);
|
|
105
|
+
};
|
|
106
|
+
log("📂 开始导出...");
|
|
107
|
+
const template = (await loadUserConfig()).fileNameTemplate ?? null;
|
|
108
|
+
const { entries, folders } = await client.getAllNotes(200, (count) => {
|
|
109
|
+
if (!quiet)
|
|
110
|
+
process.stderr.write(`\r📋 已获取 ${count} 条笔记...`);
|
|
111
|
+
});
|
|
112
|
+
if (!quiet)
|
|
113
|
+
process.stderr.write(`\r📋 共获取 ${entries.length} 条笔记\n`);
|
|
114
|
+
await ensureDir(outputDir);
|
|
115
|
+
await ensureDir(join(outputDir, "assets"));
|
|
116
|
+
const cache = force ? {} : await loadExportCache(cachePath, template);
|
|
117
|
+
const nextCache = {};
|
|
118
|
+
const rel = (p) => toOutputRel(p, outputDir);
|
|
119
|
+
const abs = (p) => toOutputAbs(p, outputDir);
|
|
120
|
+
let written = 0;
|
|
121
|
+
let skipped = 0;
|
|
122
|
+
let emptyCount = 0;
|
|
123
|
+
const errors = [];
|
|
124
|
+
const recordError = (id, err) => {
|
|
125
|
+
const message = err.message;
|
|
126
|
+
errors.push({ id, error: message });
|
|
127
|
+
if (!quiet)
|
|
128
|
+
process.stderr.write(`\n❌ 导出失败 [${id}]: ${message}\n`);
|
|
129
|
+
};
|
|
130
|
+
// 先拉齐全部详情再分配路径:同名去重需要看到整批笔记才能确定性地决定谁保留原名
|
|
131
|
+
const fresh = [];
|
|
132
|
+
const cached = [];
|
|
133
|
+
for (let i = 0; i < entries.length; i++) {
|
|
134
|
+
const entry = entries[i];
|
|
135
|
+
const id = String(entry.id);
|
|
136
|
+
if (!quiet) {
|
|
137
|
+
const pct = (((i + 1) / entries.length) * 100).toFixed(1);
|
|
138
|
+
process.stderr.write(`\r⏳ 导出中... ${i + 1}/${entries.length} (${pct}%)`);
|
|
139
|
+
}
|
|
140
|
+
const folderId = String(entry.folderId ?? "0");
|
|
141
|
+
const key = {
|
|
142
|
+
modifyDate: entry.modifyDate,
|
|
143
|
+
folderId,
|
|
144
|
+
folderName: folders[folderId]?.subject ?? "",
|
|
145
|
+
};
|
|
146
|
+
try {
|
|
147
|
+
const hit = cache[id];
|
|
148
|
+
if (hit &&
|
|
149
|
+
hit.modifyDate === key.modifyDate &&
|
|
150
|
+
hit.folderId === key.folderId &&
|
|
151
|
+
hit.folderName === key.folderName) {
|
|
152
|
+
if (hit.empty) {
|
|
153
|
+
emptyCount++;
|
|
154
|
+
nextCache[id] = hit;
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
const file = hit.filePath ? abs(hit.filePath) : undefined;
|
|
158
|
+
if (file &&
|
|
159
|
+
hit.desired &&
|
|
160
|
+
(await fileExists(file)) &&
|
|
161
|
+
computeHash(await readFile(file, "utf-8")) === hit.hash) {
|
|
162
|
+
cached.push({ id, entry: hit });
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
const note = parseNoteEntry(await client.getNote(id));
|
|
167
|
+
await randomDelay(300);
|
|
168
|
+
const desired = getNoteFilePath(note, folders, outputDir, template ?? undefined);
|
|
169
|
+
const markdown = xmlToMarkdown(note.content, note.files, assetPrefixFor(desired, outputDir));
|
|
170
|
+
if (!markdown.trim()) {
|
|
171
|
+
emptyCount++;
|
|
172
|
+
nextCache[id] = { ...key, empty: true };
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
fresh.push({ note, markdown, key: { ...key, createDate: note.createDate, desired: rel(desired) } });
|
|
176
|
+
}
|
|
177
|
+
catch (err) {
|
|
178
|
+
recordError(id, err);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
const paths = assignUniquePaths([
|
|
182
|
+
...fresh.map(({ note, key }) => ({ id: note.id, createDate: key.createDate, desired: abs(key.desired) })),
|
|
183
|
+
...cached.map(({ id, entry }) => ({ id, createDate: entry.createDate, desired: abs(entry.desired) })),
|
|
184
|
+
]);
|
|
185
|
+
// 命中缓存的笔记:内容已确认与上次写出的一致,仅在去重结果变化时挪到新路径
|
|
186
|
+
for (const { id, entry } of cached) {
|
|
187
|
+
try {
|
|
188
|
+
const target = paths.get(id);
|
|
189
|
+
const file = abs(entry.filePath);
|
|
190
|
+
if (pathKey(target) === pathKey(file)) {
|
|
191
|
+
skipped++;
|
|
192
|
+
}
|
|
193
|
+
else {
|
|
194
|
+
// 去重分配结果变了(同名笔记集合变化):挪到新路径。
|
|
195
|
+
// 旧文件内容与缓存校验过的哈希一致、是我们上次写出的产物,可安全删除,
|
|
196
|
+
// 留在原地反而会被后续 sync 当成「本地新增」重复上传。
|
|
197
|
+
await ensureFileDir(target);
|
|
198
|
+
await writeFile(target, await readFile(file, "utf-8"), "utf-8");
|
|
199
|
+
await rm(file, { force: true });
|
|
200
|
+
written++;
|
|
201
|
+
}
|
|
202
|
+
nextCache[id] = { ...entry, filePath: rel(target) };
|
|
203
|
+
}
|
|
204
|
+
catch (err) {
|
|
205
|
+
recordError(id, err);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
for (const { note, markdown, key } of fresh) {
|
|
209
|
+
try {
|
|
210
|
+
const filePath = paths.get(note.id);
|
|
211
|
+
const unchanged = !force &&
|
|
212
|
+
(await fileExists(filePath)) &&
|
|
213
|
+
computeHash(await readFile(filePath, "utf-8")) === computeHash(markdown);
|
|
214
|
+
if (unchanged) {
|
|
215
|
+
skipped++;
|
|
216
|
+
}
|
|
217
|
+
else {
|
|
218
|
+
await ensureFileDir(filePath);
|
|
219
|
+
await writeFile(filePath, markdown, "utf-8");
|
|
220
|
+
written++;
|
|
221
|
+
}
|
|
222
|
+
// 正文未变也补齐附件:上次下载失败的借此重试(已存在的文件不会重复下载)。
|
|
223
|
+
// 有失败则不写入缓存,下次导出会重新检查这条笔记。
|
|
224
|
+
const failedFiles = await downloadAttachments(client, note.files, outputDir);
|
|
225
|
+
if (failedFiles.length > 0)
|
|
226
|
+
throw new Error(attachmentError(failedFiles));
|
|
227
|
+
nextCache[note.id] = { ...key, filePath: rel(filePath), hash: computeHash(markdown) };
|
|
228
|
+
}
|
|
229
|
+
catch (err) {
|
|
230
|
+
recordError(note.id, err);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
const nextFile = { version: TOOL_VERSION, template, notes: nextCache };
|
|
234
|
+
await ensureFileDir(cachePath);
|
|
235
|
+
await writeFile(cachePath, JSON.stringify(nextFile), "utf-8");
|
|
236
|
+
if (!quiet) {
|
|
237
|
+
process.stderr.write("\n");
|
|
238
|
+
log(`━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
239
|
+
📊 导出完成
|
|
240
|
+
总笔记数: ${entries.length}
|
|
241
|
+
写入: ${written}
|
|
242
|
+
未修改: ${skipped}
|
|
243
|
+
空笔记: ${emptyCount}
|
|
244
|
+
失败: ${errors.length}
|
|
245
|
+
输出目录: ${outputDir}
|
|
246
|
+
━━━━━━━━━━━━━━━━━━━━━━━━━━━━`);
|
|
247
|
+
}
|
|
248
|
+
return {
|
|
249
|
+
total: entries.length,
|
|
250
|
+
written,
|
|
251
|
+
skipped,
|
|
252
|
+
empty: emptyCount,
|
|
253
|
+
failed: errors.length,
|
|
254
|
+
errors,
|
|
255
|
+
outputDir,
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
/** 各动作计数全为 0 */
|
|
259
|
+
export function emptyApplied() {
|
|
260
|
+
return {
|
|
261
|
+
skip: 0,
|
|
262
|
+
"update-local": 0,
|
|
263
|
+
"update-remote": 0,
|
|
264
|
+
"create-local": 0,
|
|
265
|
+
"create-remote": 0,
|
|
266
|
+
"delete-local": 0,
|
|
267
|
+
"delete-remote": 0,
|
|
268
|
+
"drop-state": 0,
|
|
269
|
+
conflict: 0,
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* 构建同步计划:对每条笔记做 3-way diff 并按 mode 决策动作。
|
|
274
|
+
* 不触碰云端与本地文件;只在内存中修正 state(认领、去重),由调用方决定是否落盘。
|
|
275
|
+
*
|
|
276
|
+
* @returns stateChanged 为 true 时即使 plan 为空也应保存 state
|
|
277
|
+
*/
|
|
278
|
+
export async function buildSyncPlan(client, outputDir, mode, quiet = false) {
|
|
279
|
+
const state = await loadState(outputDir);
|
|
280
|
+
const fileNameTemplate = (await loadUserConfig()).fileNameTemplate;
|
|
281
|
+
let stateChanged = await dedupeStatePaths(state, outputDir);
|
|
282
|
+
const { entries, folders } = await client.getAllNotes(200, (count) => {
|
|
283
|
+
if (!quiet)
|
|
284
|
+
process.stderr.write(`\r📋 已获取 ${count} 条笔记...`);
|
|
285
|
+
});
|
|
286
|
+
if (!quiet)
|
|
287
|
+
process.stderr.write("\r");
|
|
288
|
+
const remoteById = new Map();
|
|
289
|
+
for (const e of entries)
|
|
290
|
+
remoteById.set(String(e.id), e);
|
|
291
|
+
// 已被某条笔记占用的本地路径:state 记录的 + 本次认领的
|
|
292
|
+
const trackedPaths = new Set();
|
|
293
|
+
for (const n of Object.values(state.notes)) {
|
|
294
|
+
if (n.filePath)
|
|
295
|
+
trackedPaths.add(pathKey(toOutputAbs(n.filePath, outputDir)));
|
|
296
|
+
}
|
|
297
|
+
// 收集所有涉及的 id:云端的 ∪ 状态里记录过的
|
|
298
|
+
const allIds = new Set([
|
|
299
|
+
...remoteById.keys(),
|
|
300
|
+
...Object.keys(state.notes),
|
|
301
|
+
]);
|
|
302
|
+
const plan = [];
|
|
303
|
+
for (const id of allIds) {
|
|
304
|
+
const remote = remoteById.get(id);
|
|
305
|
+
const base = state.notes[id];
|
|
306
|
+
// 先读本地(不依赖网络)。state 里 filePath 为相对 output 的相对路径,读写时转绝对。
|
|
307
|
+
let localMarkdown;
|
|
308
|
+
let localHash;
|
|
309
|
+
let filePath = base?.filePath ? toOutputAbs(base.filePath, outputDir) : null;
|
|
310
|
+
if (filePath && (await fileExists(filePath))) {
|
|
311
|
+
localMarkdown = await readFile(filePath, "utf-8");
|
|
312
|
+
localHash = computeHash(localMarkdown);
|
|
313
|
+
}
|
|
314
|
+
// 效率优化:云端 modifyDate 未变 且 本地哈希未变 → 必然 in-sync,跳过拉详情
|
|
315
|
+
const remoteUnchanged = remote !== undefined &&
|
|
316
|
+
base?.remoteModify !== undefined &&
|
|
317
|
+
remote.modifyDate === base.remoteModify;
|
|
318
|
+
const localUnchanged = localHash !== undefined && localHash === base?.localHash;
|
|
319
|
+
if (remoteUnchanged && localUnchanged) {
|
|
320
|
+
continue; // in-sync,无需动作
|
|
321
|
+
}
|
|
322
|
+
// 计算 remote markdown / hash(仅在可能变化时拉详情)
|
|
323
|
+
let remoteMarkdown;
|
|
324
|
+
let remoteHash;
|
|
325
|
+
let remoteDetail;
|
|
326
|
+
let remoteNote;
|
|
327
|
+
let desired;
|
|
328
|
+
if (remote) {
|
|
329
|
+
remoteDetail = await client.getNote(id);
|
|
330
|
+
remoteNote = parseNoteEntry(remoteDetail);
|
|
331
|
+
desired = getNoteFilePath(remoteNote, folders, outputDir, fileNameTemplate);
|
|
332
|
+
remoteMarkdown = xmlToMarkdown(remoteNote.content, remoteNote.files, assetPrefixFor(desired, outputDir));
|
|
333
|
+
remoteHash = computeHash(remoteMarkdown);
|
|
334
|
+
}
|
|
335
|
+
// 旧版本在子目录笔记里把附件写成 `assets/`(正确应为 `../assets/`)。
|
|
336
|
+
// 本地内容若只差这个前缀,不能当成云端改动,否则会误报冲突。
|
|
337
|
+
const legacyHash = () => remoteNote && desired && assetPrefixFor(desired, outputDir) !== "assets/"
|
|
338
|
+
? computeHash(xmlToMarkdown(remoteNote.content, remoteNote.files, "assets/"))
|
|
339
|
+
: undefined;
|
|
340
|
+
// 认领:尚无基线的云端笔记,若其落盘位置已有未被跟踪的本地文件(如先 export 过),
|
|
341
|
+
// 视为同一笔记参与 3-way 比较,而不是把它当成「本地新增」再上传一份重复笔记。
|
|
342
|
+
let baseHash = base?.baseHash;
|
|
343
|
+
if (!base && remoteNote && desired) {
|
|
344
|
+
const key = pathKey(desired);
|
|
345
|
+
if (!trackedPaths.has(key) && (await fileExists(desired))) {
|
|
346
|
+
trackedPaths.add(key);
|
|
347
|
+
filePath = desired;
|
|
348
|
+
localMarkdown = await readFile(desired, "utf-8");
|
|
349
|
+
localHash = computeHash(localMarkdown);
|
|
350
|
+
// 与云端只差旧前缀:以本地为基线,按「仅云端变化」刷新文件
|
|
351
|
+
if (localHash !== remoteHash && legacyHash() === localHash) {
|
|
352
|
+
baseHash = localHash;
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
let scenario = classify({ baseHash, remoteHash, localHash });
|
|
357
|
+
if (scenario === "both-changed" && baseHash && legacyHash() === baseHash) {
|
|
358
|
+
scenario = "local-changed"; // 云端内容其实没变,只是引用前缀换了写法
|
|
359
|
+
}
|
|
360
|
+
const action = decide(scenario, mode);
|
|
361
|
+
if (scenario === "in-sync") {
|
|
362
|
+
if (!base && remoteDetail && remoteNote && filePath && remoteHash) {
|
|
363
|
+
state.notes[id] = {
|
|
364
|
+
id,
|
|
365
|
+
subject: remoteNote.subject,
|
|
366
|
+
filePath: toOutputRel(filePath, outputDir),
|
|
367
|
+
folderId: remoteNote.folderId,
|
|
368
|
+
baseHash: remoteHash,
|
|
369
|
+
localHash: remoteHash,
|
|
370
|
+
remoteModify: remoteDetail.modifyDate,
|
|
371
|
+
};
|
|
372
|
+
stateChanged = true;
|
|
373
|
+
}
|
|
374
|
+
continue;
|
|
375
|
+
}
|
|
376
|
+
plan.push({
|
|
377
|
+
id,
|
|
378
|
+
subject: remoteNote?.subject || base?.subject || id,
|
|
379
|
+
scenario,
|
|
380
|
+
action,
|
|
381
|
+
remote: remoteDetail ?? remote,
|
|
382
|
+
filePath,
|
|
383
|
+
localMarkdown,
|
|
384
|
+
remoteMarkdown,
|
|
385
|
+
});
|
|
386
|
+
}
|
|
387
|
+
// 检测「本地新增文件」(#6):输出目录下未被状态记录、也未被认领的 .md 文件。
|
|
388
|
+
const localFiles = await scanMarkdownFiles(outputDir);
|
|
389
|
+
for (const fp of localFiles) {
|
|
390
|
+
if (trackedPaths.has(pathKey(fp)))
|
|
391
|
+
continue;
|
|
392
|
+
const localMarkdown = await readFile(fp, "utf-8");
|
|
393
|
+
if (!localMarkdown.trim())
|
|
394
|
+
continue; // 空文件忽略
|
|
395
|
+
// 无 base、无 remote、有 local → classify 为 local-new
|
|
396
|
+
const scenario = classify({ localHash: computeHash(localMarkdown) });
|
|
397
|
+
const action = decide(scenario, mode);
|
|
398
|
+
// 用文件路径作为计划项的临时标识(尚无云端 id)
|
|
399
|
+
plan.push({
|
|
400
|
+
id: `local:${fp}`,
|
|
401
|
+
subject: titleFromPath(fp, `local:${fp}`),
|
|
402
|
+
scenario,
|
|
403
|
+
action,
|
|
404
|
+
filePath: fp,
|
|
405
|
+
localMarkdown,
|
|
406
|
+
});
|
|
407
|
+
}
|
|
408
|
+
return { plan, state, folders, stateChanged };
|
|
409
|
+
}
|
|
410
|
+
/**
|
|
411
|
+
* 修复多条笔记记录指向同一本地文件的状态(旧版本同名笔记互相覆盖所致):
|
|
412
|
+
* 仅保留基线与文件当前内容一致的那条,其余移出状态,按云端新增重新落盘(届时会加 id 后缀)。
|
|
413
|
+
* 都对不上时全部移出,文件作为未跟踪文件参与认领/冲突判定,不擅自归属。
|
|
414
|
+
*/
|
|
415
|
+
async function dedupeStatePaths(state, outputDir) {
|
|
416
|
+
const groups = new Map();
|
|
417
|
+
for (const n of Object.values(state.notes)) {
|
|
418
|
+
if (!n.filePath)
|
|
419
|
+
continue;
|
|
420
|
+
const key = pathKey(toOutputAbs(n.filePath, outputDir));
|
|
421
|
+
const group = groups.get(key);
|
|
422
|
+
if (group)
|
|
423
|
+
group.push(n);
|
|
424
|
+
else
|
|
425
|
+
groups.set(key, [n]);
|
|
426
|
+
}
|
|
427
|
+
let changed = false;
|
|
428
|
+
for (const group of groups.values()) {
|
|
429
|
+
if (group.length < 2)
|
|
430
|
+
continue;
|
|
431
|
+
const abs = toOutputAbs(group[0].filePath, outputDir);
|
|
432
|
+
const hash = (await fileExists(abs))
|
|
433
|
+
? computeHash(await readFile(abs, "utf-8"))
|
|
434
|
+
: undefined;
|
|
435
|
+
const owner = hash ? group.find((n) => n.baseHash === hash) : undefined;
|
|
436
|
+
for (const n of group) {
|
|
437
|
+
if (n !== owner)
|
|
438
|
+
delete state.notes[n.id];
|
|
439
|
+
}
|
|
440
|
+
changed = true;
|
|
441
|
+
}
|
|
442
|
+
return changed;
|
|
443
|
+
}
|
|
444
|
+
/**
|
|
445
|
+
* 执行同步计划。
|
|
446
|
+
* @param resolveConflict 遇到 action==="conflict" 时调用;不提供则跳过并计入冲突列表
|
|
447
|
+
*/
|
|
448
|
+
export async function executeSyncPlan(client, outputDir, mode, plan, state, folders, opts = {}) {
|
|
449
|
+
const { dryRun = false, quiet = false, resolveConflict } = opts;
|
|
450
|
+
const fileNameTemplate = (await loadUserConfig()).fileNameTemplate;
|
|
451
|
+
const applied = emptyApplied();
|
|
452
|
+
const conflicts = [];
|
|
453
|
+
const errors = [];
|
|
454
|
+
let processed = 0;
|
|
455
|
+
for (const item of plan) {
|
|
456
|
+
let action = item.action;
|
|
457
|
+
if (action === "conflict") {
|
|
458
|
+
if (resolveConflict) {
|
|
459
|
+
action = await resolveConflict(item);
|
|
460
|
+
}
|
|
461
|
+
else {
|
|
462
|
+
conflicts.push(item);
|
|
463
|
+
applied.conflict++;
|
|
464
|
+
continue;
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
if (dryRun) {
|
|
468
|
+
applied[action]++;
|
|
469
|
+
continue;
|
|
470
|
+
}
|
|
471
|
+
try {
|
|
472
|
+
const warning = await applyAction(client, outputDir, action, item, state, folders, fileNameTemplate);
|
|
473
|
+
if (warning)
|
|
474
|
+
errors.push({ id: item.id, subject: item.subject, action, error: warning });
|
|
475
|
+
applied[action]++;
|
|
476
|
+
processed++;
|
|
477
|
+
if (processed % SAVE_INTERVAL === 0) {
|
|
478
|
+
state.lastSync = Date.now();
|
|
479
|
+
await saveState(outputDir, state);
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
catch (err) {
|
|
483
|
+
const message = err.message;
|
|
484
|
+
errors.push({ id: item.id, subject: item.subject, action, error: message });
|
|
485
|
+
if (!quiet) {
|
|
486
|
+
process.stderr.write(`\n❌ 处理失败 [${item.id}] ${action}: ${message}\n`);
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
if (!dryRun) {
|
|
491
|
+
state.lastSync = Date.now();
|
|
492
|
+
await saveState(outputDir, state);
|
|
493
|
+
}
|
|
494
|
+
return {
|
|
495
|
+
total: plan.length,
|
|
496
|
+
applied,
|
|
497
|
+
conflicts,
|
|
498
|
+
errors,
|
|
499
|
+
outputDir,
|
|
500
|
+
mode,
|
|
501
|
+
dryRun,
|
|
502
|
+
};
|
|
503
|
+
}
|
|
504
|
+
/**
|
|
505
|
+
* 计划生成后本地文件又被改动(如交互询问期间用户编辑了文件)时中止该条,
|
|
506
|
+
* 避免用过期的计划覆盖/删除新内容。
|
|
507
|
+
*/
|
|
508
|
+
async function assertLocalUnchanged(item) {
|
|
509
|
+
const current = item.filePath && (await fileExists(item.filePath))
|
|
510
|
+
? await readFile(item.filePath, "utf-8")
|
|
511
|
+
: undefined;
|
|
512
|
+
if (current !== item.localMarkdown) {
|
|
513
|
+
throw new Error("本地文件在生成同步计划后又被修改,已跳过,请重新运行 sync");
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
/** 选一个不会覆盖其他笔记文件的落盘路径 */
|
|
517
|
+
async function claimFilePath(desired, item, state, outputDir) {
|
|
518
|
+
const key = pathKey(desired);
|
|
519
|
+
if (item.filePath && pathKey(item.filePath) === key)
|
|
520
|
+
return desired;
|
|
521
|
+
const heldByOther = Object.values(state.notes).some((n) => n.id !== item.id &&
|
|
522
|
+
n.filePath !== null &&
|
|
523
|
+
pathKey(toOutputAbs(n.filePath, outputDir)) === key);
|
|
524
|
+
if (!heldByOther && !(await fileExists(desired)))
|
|
525
|
+
return desired;
|
|
526
|
+
return withIdSuffix(desired, item.id);
|
|
527
|
+
}
|
|
528
|
+
/**
|
|
529
|
+
* 本地文件所在一级子目录 → 云端文件夹 id。
|
|
530
|
+
* 优先沿用同目录下已同步笔记所在的文件夹(云端文件夹改名后本地目录名会过时),
|
|
531
|
+
* 其次按名称匹配,都没有才新建。
|
|
532
|
+
*/
|
|
533
|
+
async function resolveFolderId(client, filePath, outputDir, folders, state) {
|
|
534
|
+
if (!filePath)
|
|
535
|
+
return "0";
|
|
536
|
+
const parts = relative(outputDir, filePath).split(sep);
|
|
537
|
+
if (parts.length < 2)
|
|
538
|
+
return "0";
|
|
539
|
+
const dirName = parts[0];
|
|
540
|
+
const dirKey = pathKey(join(outputDir, dirName));
|
|
541
|
+
for (const n of Object.values(state.notes)) {
|
|
542
|
+
if (!n.folderId || !n.filePath || !folders[n.folderId])
|
|
543
|
+
continue;
|
|
544
|
+
if (pathKey(dirname(toOutputAbs(n.filePath, outputDir))) === dirKey)
|
|
545
|
+
return n.folderId;
|
|
546
|
+
}
|
|
547
|
+
for (const [id, folder] of Object.entries(folders)) {
|
|
548
|
+
if (sanitizeFileName(folder.subject || "") === dirName)
|
|
549
|
+
return id;
|
|
550
|
+
}
|
|
551
|
+
const created = await client.createFolder(dirName);
|
|
552
|
+
const id = String(created.id);
|
|
553
|
+
folders[id] = created;
|
|
554
|
+
return id;
|
|
555
|
+
}
|
|
556
|
+
/** 执行单个动作并更新状态;动作已生效但有需要报告的问题时返回说明 */
|
|
557
|
+
async function applyAction(client, outputDir, action, item, state, folders, fileNameTemplate) {
|
|
558
|
+
const id = item.id;
|
|
559
|
+
switch (action) {
|
|
560
|
+
case "skip":
|
|
561
|
+
return;
|
|
562
|
+
case "update-local":
|
|
563
|
+
case "create-local": {
|
|
564
|
+
// 下行:用云端覆盖/创建本地
|
|
565
|
+
if (!item.remote || item.remoteMarkdown === undefined)
|
|
566
|
+
return;
|
|
567
|
+
await assertLocalUnchanged(item);
|
|
568
|
+
const note = parseNoteEntry(item.remote);
|
|
569
|
+
const desired = getNoteFilePath(note, folders, outputDir, fileNameTemplate);
|
|
570
|
+
const filePath = await claimFilePath(desired, item, state, outputDir);
|
|
571
|
+
await ensureFileDir(filePath);
|
|
572
|
+
await writeFile(filePath, item.remoteMarkdown, "utf-8");
|
|
573
|
+
state.notes[id] = {
|
|
574
|
+
id,
|
|
575
|
+
subject: note.subject,
|
|
576
|
+
filePath: toOutputRel(filePath, outputDir),
|
|
577
|
+
folderId: note.folderId,
|
|
578
|
+
baseHash: computeHash(item.remoteMarkdown),
|
|
579
|
+
localHash: computeHash(item.remoteMarkdown),
|
|
580
|
+
remoteModify: item.remote.modifyDate,
|
|
581
|
+
};
|
|
582
|
+
// 路径变化(改名/换文件夹)时清理旧文件
|
|
583
|
+
if (item.filePath &&
|
|
584
|
+
pathKey(item.filePath) !== pathKey(filePath) &&
|
|
585
|
+
(await fileExists(item.filePath))) {
|
|
586
|
+
await rm(item.filePath, { force: true });
|
|
587
|
+
}
|
|
588
|
+
// 附件失败不回滚正文:一个坏附件不应让整篇笔记永远同步不下来
|
|
589
|
+
const failedFiles = await downloadAttachments(client, note.files, outputDir);
|
|
590
|
+
return failedFiles.length > 0 ? attachmentError(failedFiles) : undefined;
|
|
591
|
+
}
|
|
592
|
+
case "update-remote":
|
|
593
|
+
case "create-remote": {
|
|
594
|
+
// 上行:用本地内容更新/创建云端。item.filePath 为绝对路径,存状态前转相对 output。
|
|
595
|
+
if (item.localMarkdown === undefined)
|
|
596
|
+
return;
|
|
597
|
+
await assertLocalUnchanged(item);
|
|
598
|
+
const relPath = item.filePath ? toOutputRel(item.filePath, outputDir) : null;
|
|
599
|
+
const prefix = item.filePath ? assetPrefixFor(item.filePath, outputDir) : "assets/";
|
|
600
|
+
const baseDirs = item.filePath ? [dirname(item.filePath), outputDir] : [outputDir];
|
|
601
|
+
const now = Date.now();
|
|
602
|
+
if (action === "create-remote") {
|
|
603
|
+
const { imageMap, uploaded } = await uploadLocalImages(client, item.localMarkdown, new Map(), baseDirs);
|
|
604
|
+
const xml = markdownToXml(item.localMarkdown, imageMap);
|
|
605
|
+
const folderId = await resolveFolderId(client, item.filePath, outputDir, folders, state);
|
|
606
|
+
const title = item.filePath
|
|
607
|
+
? titleFromPath(item.filePath, id)
|
|
608
|
+
: firstLine(item.localMarkdown);
|
|
609
|
+
const created = await client.createNote({
|
|
610
|
+
colorId: 0,
|
|
611
|
+
folderId,
|
|
612
|
+
createDate: now,
|
|
613
|
+
modifyDate: now,
|
|
614
|
+
content: xml,
|
|
615
|
+
alertDate: 0,
|
|
616
|
+
setting: withAttachments(undefined, uploaded),
|
|
617
|
+
extraInfo: buildExtraInfoString(title),
|
|
618
|
+
snippet: extractSnippet(xml),
|
|
619
|
+
});
|
|
620
|
+
const newId = String(created.id);
|
|
621
|
+
// 云端已删后重建会换新 id,旧记录必须移除,否则两条记录指向同一文件
|
|
622
|
+
delete state.notes[id];
|
|
623
|
+
state.notes[newId] = {
|
|
624
|
+
id: newId,
|
|
625
|
+
subject: title,
|
|
626
|
+
filePath: relPath,
|
|
627
|
+
folderId,
|
|
628
|
+
baseHash: computeHash(item.localMarkdown),
|
|
629
|
+
localHash: computeHash(item.localMarkdown),
|
|
630
|
+
remoteModify: created.modifyDate,
|
|
631
|
+
};
|
|
632
|
+
}
|
|
633
|
+
else {
|
|
634
|
+
// update-remote:先取最新 entry 拿 tag,改 content 再提交
|
|
635
|
+
const current = await client.getNote(id);
|
|
636
|
+
// 计划生成后云端又被改过:直接提交会用新 tag 覆盖掉这次改动
|
|
637
|
+
if (item.remote?.modifyDate !== undefined &&
|
|
638
|
+
current.modifyDate !== item.remote.modifyDate) {
|
|
639
|
+
throw new Error("云端笔记在生成同步计划后又被修改,已跳过,请重新运行 sync");
|
|
640
|
+
}
|
|
641
|
+
const currentNote = parseNoteEntry(current);
|
|
642
|
+
const { imageMap, uploaded } = await uploadLocalImages(client, item.localMarkdown, buildImageMap(currentNote.files, prefix, "assets/"), baseDirs);
|
|
643
|
+
const xml = markdownToXml(item.localMarkdown, imageMap);
|
|
644
|
+
const entry = {
|
|
645
|
+
id,
|
|
646
|
+
tag: current.tag,
|
|
647
|
+
status: current.status,
|
|
648
|
+
createDate: current.createDate ?? now,
|
|
649
|
+
modifyDate: now,
|
|
650
|
+
colorId: current.colorId ?? 0,
|
|
651
|
+
content: xml,
|
|
652
|
+
setting: withAttachments(current.setting, uploaded),
|
|
653
|
+
folderId: String(current.folderId ?? "0"),
|
|
654
|
+
alertDate: current.alertDate ?? 0,
|
|
655
|
+
extraInfo: typeof current.extraInfo === "string"
|
|
656
|
+
? current.extraInfo
|
|
657
|
+
: undefined,
|
|
658
|
+
subject: current.subject,
|
|
659
|
+
snippet: extractSnippet(xml),
|
|
660
|
+
};
|
|
661
|
+
const updated = await client.updateNote(id, entry);
|
|
662
|
+
state.notes[id] = {
|
|
663
|
+
id,
|
|
664
|
+
subject: state.notes[id]?.subject ?? currentNote.subject,
|
|
665
|
+
filePath: relPath,
|
|
666
|
+
folderId: currentNote.folderId,
|
|
667
|
+
baseHash: computeHash(item.localMarkdown),
|
|
668
|
+
localHash: computeHash(item.localMarkdown),
|
|
669
|
+
remoteModify: updated.modifyDate,
|
|
670
|
+
};
|
|
671
|
+
}
|
|
672
|
+
return;
|
|
673
|
+
}
|
|
674
|
+
case "delete-local": {
|
|
675
|
+
await assertLocalUnchanged(item);
|
|
676
|
+
if (item.filePath && (await fileExists(item.filePath))) {
|
|
677
|
+
await rm(item.filePath, { force: true });
|
|
678
|
+
}
|
|
679
|
+
delete state.notes[id];
|
|
680
|
+
return;
|
|
681
|
+
}
|
|
682
|
+
case "delete-remote": {
|
|
683
|
+
// 同步里的删除只移到回收站:本地误删文件时仍可在云端恢复
|
|
684
|
+
const tag = item.remote?.tag;
|
|
685
|
+
if (!tag)
|
|
686
|
+
throw new Error("云端笔记缺少 tag,无法删除");
|
|
687
|
+
await client.deleteNote(id, tag, false);
|
|
688
|
+
delete state.notes[id];
|
|
689
|
+
return;
|
|
690
|
+
}
|
|
691
|
+
case "drop-state":
|
|
692
|
+
delete state.notes[id];
|
|
693
|
+
return;
|
|
694
|
+
default:
|
|
695
|
+
return;
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
/** 本地文件名作为云端标题;去掉同名去重时加的 `_<id>` 后缀 */
|
|
699
|
+
function titleFromPath(filePath, id) {
|
|
700
|
+
let name = basename(filePath, ".md");
|
|
701
|
+
const suffix = `_${id}`;
|
|
702
|
+
if (!id.startsWith("local:") && name.endsWith(suffix) && name.length > suffix.length) {
|
|
703
|
+
name = name.slice(0, -suffix.length);
|
|
704
|
+
}
|
|
705
|
+
return name.trim() || "未命名";
|
|
706
|
+
}
|
|
707
|
+
function firstLine(md) {
|
|
708
|
+
return (md
|
|
709
|
+
.split("\n")
|
|
710
|
+
.map((l) => l.replace(/^#+\s*/, "").trim())
|
|
711
|
+
.find((l) => l.length > 0) ?? "未命名");
|
|
712
|
+
}
|
|
713
|
+
/**
|
|
714
|
+
* 扫描输出目录下的 Markdown 文件(含一级子目录即文件夹),跳过 assets/。
|
|
715
|
+
* 用于发现「本地新增、尚未同步」的笔记文件。
|
|
716
|
+
*/
|
|
717
|
+
async function scanMarkdownFiles(outputDir) {
|
|
718
|
+
const result = [];
|
|
719
|
+
let topEntries;
|
|
720
|
+
try {
|
|
721
|
+
topEntries = await readdir(outputDir, { withFileTypes: true });
|
|
722
|
+
}
|
|
723
|
+
catch {
|
|
724
|
+
return result;
|
|
725
|
+
}
|
|
726
|
+
for (const ent of topEntries) {
|
|
727
|
+
if (ent.name === "assets")
|
|
728
|
+
continue;
|
|
729
|
+
const full = join(outputDir, ent.name);
|
|
730
|
+
if (ent.isFile() && ent.name.endsWith(".md")) {
|
|
731
|
+
result.push(full);
|
|
732
|
+
}
|
|
733
|
+
else if (ent.isDirectory()) {
|
|
734
|
+
try {
|
|
735
|
+
const sub = await readdir(full, { withFileTypes: true });
|
|
736
|
+
for (const s of sub) {
|
|
737
|
+
if (s.isFile() && s.name.endsWith(".md")) {
|
|
738
|
+
result.push(join(full, s.name));
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
catch {
|
|
743
|
+
/* ignore unreadable subdir */
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
return result;
|
|
748
|
+
}
|