dsh-config-manager 0.5.0 → 0.6.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/lib/client.d.ts +3 -0
- package/lib/client.js +709 -602
- package/lib/core/messages.d.ts +4 -0
- package/lib/core/messages.js +8 -0
- package/lib/core/migration-history.d.ts +40 -0
- package/lib/core/migration-history.js +55 -0
- package/lib/core/plugin-cli.d.ts +5 -0
- package/lib/core/plugin-cli.js +11 -3
- package/lib/index.d.ts +5 -1
- package/lib/index.js +13 -14
- package/lib/profiles/dsh-profile-manager.d.ts +1 -1
- package/lib/profiles/dsh-profile-manager.js +26 -7
- package/lib/profiles/dsh-profile-shared.d.ts +3 -1
- package/lib/ui/sync-repository-picker.d.ts +21 -0
- package/lib/ui/sync-repository-picker.js +18 -0
- package/lib/utils/remove-tree.d.ts +7 -0
- package/lib/utils/remove-tree.js +265 -0
- package/package.json +1 -1
- package/src/client/config-manager.module.css +21 -1
- package/src/client/environment/EnvironmentPanel.tsx +2 -2
- package/src/client/locales/profiles.ts +2 -0
- package/src/client/sync/ChannelConfigDialog.tsx +32 -1
- package/src/client/sync/SyncPanel.tsx +11 -30
- package/src/client/sync/SyncRepositoryPicker.tsx +34 -13
- package/src/client/sync/SyncSettingsView.test.ts +32 -0
- package/src/client/sync/config-save-state.test.ts +99 -0
- package/src/client/sync/config-save-state.ts +69 -0
- package/src/client/sync/sync-locales.ts +4 -0
- package/src/core/engine-locale.test.ts +4 -0
- package/src/core/messages.ts +8 -0
- package/src/core/migration-history.test.ts +65 -0
- package/src/core/migration-history.ts +70 -0
- package/src/core/plugin-cli.fs.test.ts +30 -3
- package/src/core/plugin-cli.ts +11 -3
- package/src/index.ts +12 -14
- package/src/profiles/dsh-profile-manager.test.ts +20 -0
- package/src/profiles/dsh-profile-manager.ts +25 -7
- package/src/profiles/dsh-profile-shared.ts +2 -0
- package/src/ui/sync-repository-picker.test.ts +34 -1
- package/src/ui/sync-repository-picker.ts +24 -0
- package/src/utils/remove-tree.test.ts +120 -0
- package/src/utils/remove-tree.ts +239 -0
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 「保存通道配置成功」的 state 演进(./config-save-state.ts)。
|
|
3
|
+
*
|
|
4
|
+
* 真机背景:四通道都有输入防抖自动保存,成功后此前完全静默;
|
|
5
|
+
* 而凭据清空与徽章合并原本内联在 SyncPanel,既挤占行数棘轮又不能单测。
|
|
6
|
+
* 这里钉住两条不可放宽的口径:
|
|
7
|
+
* ① 只清「本次确实写出去」的凭据(保存期间用户改过输入 → 保留新值);
|
|
8
|
+
* ② 响应只含布尔/非密视图,绝不把 secret 值写回 state。
|
|
9
|
+
*/
|
|
10
|
+
import test from 'node:test';
|
|
11
|
+
import assert from 'node:assert/strict';
|
|
12
|
+
|
|
13
|
+
import { savedConfigState } from './config-save-state.ts';
|
|
14
|
+
import type { SavedConfigState } from './config-save-state.ts';
|
|
15
|
+
import type { SyncConfigSaveResponse, SyncStatusResponse } from './sync-api.ts';
|
|
16
|
+
|
|
17
|
+
const status = {
|
|
18
|
+
ok: true,
|
|
19
|
+
configured: false,
|
|
20
|
+
credentialConfigured: false,
|
|
21
|
+
credentialWritable: true,
|
|
22
|
+
sectionCount: 0,
|
|
23
|
+
} as SyncStatusResponse;
|
|
24
|
+
|
|
25
|
+
const base: SavedConfigState = {
|
|
26
|
+
savingConfig: true,
|
|
27
|
+
webdavPassword: '',
|
|
28
|
+
token: '',
|
|
29
|
+
s3Secret: '',
|
|
30
|
+
gistToken: '',
|
|
31
|
+
statusInfo: null,
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
function state(over: Partial<SavedConfigState> = {}): SavedConfigState {
|
|
35
|
+
return { ...base, ...over };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
test('savingConfig 复位:无论成功与否,落库的 state 都不再处于保存中', () => {
|
|
39
|
+
const next = savedConfigState(state(), { ok: true, configured: true, transport: 'git', credentialConfigured: true }, {});
|
|
40
|
+
assert.equal(next.savingConfig, false);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test('git:token 等于本次发出的值 → 清空(已写入 credentials)', () => {
|
|
44
|
+
const next = savedConfigState(state({ token: 'ghp_sent' }), { ok: true, configured: true, transport: 'git', credentialConfigured: true }, { token: 'ghp_sent' });
|
|
45
|
+
assert.equal(next.token, '');
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test('git:保存期间用户改了输入(token ≠ 发出的值)→ 保留用户新输入', () => {
|
|
49
|
+
const next = savedConfigState(state({ token: 'ghp_new' }), { ok: true, configured: true, transport: 'git', credentialConfigured: true }, { token: 'ghp_sent' });
|
|
50
|
+
assert.equal(next.token, 'ghp_new');
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
test('webdav:password 走同一口径;未发出的空值不会被当成「清空依据」', () => {
|
|
54
|
+
const cleared = savedConfigState(state({ webdavPassword: 'pw' }), { ok: true, configured: true, transport: 'webdav', credentialConfigured: false }, { password: 'pw' });
|
|
55
|
+
assert.equal(cleared.webdavPassword, '');
|
|
56
|
+
const kept = savedConfigState(state({ webdavPassword: 'pw-other' }), { ok: true, configured: true, transport: 'webdav', credentialConfigured: false }, { password: 'pw' });
|
|
57
|
+
assert.equal(kept.webdavPassword, 'pw-other');
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
test('s3 / gist:secret 槽位共用 → 各自按同一口径清空,互不影响别的通道', () => {
|
|
61
|
+
const next = savedConfigState(state({ s3Secret: 'sk', gistToken: 'gh' }), { ok: true, configured: true, transport: 's3', credentialConfigured: false }, { secret: 'sk' });
|
|
62
|
+
assert.equal(next.s3Secret, '', 's3 密钥已写入 → 清空');
|
|
63
|
+
assert.equal(next.gistToken, 'gh', 'gist token 不等于本次 secret → 保留');
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
test('statusInfo 为 null(还没拉到 /sync/status)→ 不做徽章合并,留待 loadStatus', () => {
|
|
67
|
+
const next = savedConfigState(state(), { ok: true, configured: true, transport: 'git', credentialConfigured: true }, {});
|
|
68
|
+
assert.equal(next.statusInfo, null);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test('徽章合并:configured / credentialConfigured 取响应;webdav 保留本机 url 与 username', () => {
|
|
72
|
+
const withWebdav = { ...status, webdav: { url: 'https://dav.example.com', username: 'me', usernameConfigured: true, passwordConfigured: false } } as SyncStatusResponse;
|
|
73
|
+
const saved: SyncConfigSaveResponse = {
|
|
74
|
+
ok: true,
|
|
75
|
+
configured: true,
|
|
76
|
+
transport: 'webdav',
|
|
77
|
+
credentialConfigured: false,
|
|
78
|
+
webdav: { usernameConfigured: true, passwordConfigured: true },
|
|
79
|
+
};
|
|
80
|
+
const next = savedConfigState(state({ statusInfo: withWebdav }), saved, {});
|
|
81
|
+
assert.equal(next.statusInfo?.configured, true);
|
|
82
|
+
assert.deepEqual(next.statusInfo?.webdav, {
|
|
83
|
+
url: 'https://dav.example.com',
|
|
84
|
+
username: 'me',
|
|
85
|
+
usernameConfigured: true,
|
|
86
|
+
passwordConfigured: true,
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test('徽章合并:s3 / gist 非密视图原样并入(响应里没有就不动旧值)', () => {
|
|
91
|
+
const cloudS3 = { secretStored: true } as unknown as NonNullable<SyncStatusResponse['s3']>;
|
|
92
|
+
const next = savedConfigState(
|
|
93
|
+
state({ statusInfo: status }),
|
|
94
|
+
{ ok: true, configured: true, transport: 's3', credentialConfigured: false, s3: cloudS3 },
|
|
95
|
+
{},
|
|
96
|
+
);
|
|
97
|
+
assert.equal(next.statusInfo?.s3, cloudS3);
|
|
98
|
+
assert.equal(next.statusInfo?.gist, undefined, '响应没带 gist 视图 → 不得凭空造出');
|
|
99
|
+
});
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 「保存通道配置」成功后的本机状态合并(纯函数,node 可测)。
|
|
3
|
+
*
|
|
4
|
+
* 抽成独立文件的原因与 ./SyncRepositoryPicker.tsx 相同:SyncPanel 有行数棘轮,
|
|
5
|
+
* 而「清空已入库凭据 + 合并凭据徽章」是一段自包含的状态演算,
|
|
6
|
+
* 留在组件里既挤占行数,也无法脱离 React 单测。
|
|
7
|
+
*
|
|
8
|
+
* 口径(不得放宽):只清空**本次确实写出去**的 password / token / secret ——
|
|
9
|
+
* 保存期间用户又改了输入则保留新值;响应的凭据字段只有布尔,绝无 secret 值。
|
|
10
|
+
*/
|
|
11
|
+
import type { SyncConfigSaveResponse, SyncPushPayload, SyncStatusResponse } from './sync-api.ts'
|
|
12
|
+
|
|
13
|
+
/** 参与合并的 state 结构子集(不依赖 SyncUiState 全量定义,避免循环 import)。 */
|
|
14
|
+
export interface SavedConfigState {
|
|
15
|
+
savingConfig: boolean
|
|
16
|
+
webdavPassword: string
|
|
17
|
+
token: string
|
|
18
|
+
s3Secret: string
|
|
19
|
+
gistToken: string
|
|
20
|
+
statusInfo: SyncStatusResponse | null
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** 只清「本次已写入的」内存凭据:仍等于刚才发出去的值才清空,否则说明用户改过,保留。 */
|
|
24
|
+
function clearSent(value: string, sent: string | undefined): string {
|
|
25
|
+
return value !== '' && value !== sent ? value : ''
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* 保存成功的 state 演进:savingConfig 复位 → 清空已写入的凭据 → 合并凭据徽章事实。
|
|
30
|
+
* @param s 保存发起时刻之后的权威 state(stateRef.current)
|
|
31
|
+
* @param saved POST /sync/config 响应(只含布尔,无 secret 值)
|
|
32
|
+
* @param sent 本次实际发出的请求体(判断「哪些值确实写出去了」)
|
|
33
|
+
*/
|
|
34
|
+
export function savedConfigState(
|
|
35
|
+
s: SavedConfigState,
|
|
36
|
+
saved: SyncConfigSaveResponse,
|
|
37
|
+
sent: SyncPushPayload,
|
|
38
|
+
): SavedConfigState {
|
|
39
|
+
const next: SavedConfigState = {
|
|
40
|
+
...s,
|
|
41
|
+
savingConfig: false,
|
|
42
|
+
webdavPassword: clearSent(s.webdavPassword, sent.password),
|
|
43
|
+
token: clearSent(s.token, sent.token),
|
|
44
|
+
// 云端点密钥同一口径:s3 与 gist 共用请求体的 secret 槽位
|
|
45
|
+
s3Secret: clearSent(s.s3Secret, sent.secret),
|
|
46
|
+
gistToken: clearSent(s.gistToken, sent.secret),
|
|
47
|
+
}
|
|
48
|
+
// 状态未知(还没拉到 /sync/status)时没有徽章可合并,等下一次 loadStatus 回填
|
|
49
|
+
if (s.statusInfo === null) return next
|
|
50
|
+
const info: SyncStatusResponse = {
|
|
51
|
+
...s.statusInfo,
|
|
52
|
+
configured: true,
|
|
53
|
+
credentialConfigured: saved.credentialConfigured,
|
|
54
|
+
}
|
|
55
|
+
// webdav 徽章:url / username 是可回显的非敏感事实,用本机已有的回填
|
|
56
|
+
if (saved.webdav !== undefined) {
|
|
57
|
+
info.webdav = {
|
|
58
|
+
url: s.statusInfo.webdav?.url,
|
|
59
|
+
username: s.statusInfo.webdav?.username,
|
|
60
|
+
usernameConfigured: saved.webdav.usernameConfigured,
|
|
61
|
+
passwordConfigured: saved.webdav.passwordConfigured,
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
// 云端点:响应只回非密视图 + secretStored 布尔(值永不回传),直接合并供徽章刷新
|
|
65
|
+
if (saved.s3 !== undefined) info.s3 = saved.s3
|
|
66
|
+
if (saved.gist !== undefined) info.gist = saved.gist
|
|
67
|
+
next.statusInfo = info
|
|
68
|
+
return next
|
|
69
|
+
}
|
|
@@ -22,6 +22,7 @@ export const zh = {
|
|
|
22
22
|
// 配置保存(自动保存 + 显式保存按钮)
|
|
23
23
|
'config.save': '立即保存',
|
|
24
24
|
'config.saveHint': '表单改动会自动保存:密码/token 安全写入 DSH credentials,不会写入同步文件或日志。',
|
|
25
|
+
'config.savedTick': '已保存',
|
|
25
26
|
// 通道选择器
|
|
26
27
|
'channel.title': '同步通道',
|
|
27
28
|
'channel.git': 'Git 私有仓库',
|
|
@@ -102,6 +103,7 @@ export const zh = {
|
|
|
102
103
|
'repoPicker.hint': '列出你 GitHub 账号下已有的私有仓库(按最近更新排序),选中即填入下方仓库地址;也可以直接在下方手填地址(ssh / 本地路径 / 不在列表里的仓库)。列表需要先登录 GitHub;新建的仓库一律为私有 —— 公开仓库会公开你的配置内容。',
|
|
103
104
|
'repoPicker.placeholder': '手动填写地址(或从上方选择)',
|
|
104
105
|
'repoPicker.refresh': '刷新列表',
|
|
106
|
+
'repoPicker.refreshed': '已刷新:{count} 个私有仓库',
|
|
105
107
|
'repoPicker.forkBadge': 'Fork',
|
|
106
108
|
'repoPicker.empty': '没有可用的私有仓库',
|
|
107
109
|
'repoPicker.loadFailed': '仓库列表加载失败,可重试或直接手填地址。',
|
|
@@ -482,6 +484,7 @@ export const en: Record<keyof typeof zh, string> = {
|
|
|
482
484
|
// Config save (auto-save + explicit save button)
|
|
483
485
|
'config.save': 'Save now',
|
|
484
486
|
'config.saveHint': 'Form changes are saved automatically: password/token goes securely into DSH credentials, never into sync files or logs.',
|
|
487
|
+
'config.savedTick': 'Saved',
|
|
485
488
|
'channel.title': 'Sync Channel',
|
|
486
489
|
'channel.git': 'Git private repo',
|
|
487
490
|
'channel.webdav': 'WebDAV server',
|
|
@@ -559,6 +562,7 @@ export const en: Record<keyof typeof zh, string> = {
|
|
|
559
562
|
'repoPicker.hint': 'Lists the existing private repositories in your GitHub account (most recently updated first); picking one fills the repository URL below. You can still type an address by hand (ssh / local path / repositories not in the list). The list requires GitHub sign-in. Newly created repositories are always private — a public repository would expose your config contents.',
|
|
560
563
|
'repoPicker.placeholder': 'Type an address (or pick above)',
|
|
561
564
|
'repoPicker.refresh': 'Refresh list',
|
|
565
|
+
'repoPicker.refreshed': 'Refreshed: {count} private repositories',
|
|
562
566
|
'repoPicker.forkBadge': 'Fork',
|
|
563
567
|
'repoPicker.empty': 'No private repository available',
|
|
564
568
|
'repoPicker.loadFailed': 'Could not load the repository list — retry, or type the address below.',
|
|
@@ -173,6 +173,10 @@ test('t2 新增的 host.* 键:zh 逐字保留既有文案、en 非空且无 CJ
|
|
|
173
173
|
['host.snapshotPruneSummary', '自动保留清理删除 {count} 个旧快照'],
|
|
174
174
|
['host.autosyncSummary', '自动同步 {direction}{transportSuffix}'],
|
|
175
175
|
['host.autosyncTransportSuffix', '({transport})'],
|
|
176
|
+
['host.autosyncDirectionPush', '上传'],
|
|
177
|
+
['host.autosyncDirectionPull', '下载'],
|
|
178
|
+
['host.autosyncDirectionBoth', '双向'],
|
|
179
|
+
['host.autosyncDirectionNone', '检查'],
|
|
176
180
|
];
|
|
177
181
|
const zhDict = zh as Readonly<Record<string, string>>;
|
|
178
182
|
const enDict = en as Readonly<Record<string, string>>;
|
package/src/core/messages.ts
CHANGED
|
@@ -215,6 +215,10 @@ export const zh = {
|
|
|
215
215
|
'host.snapshotPruneSummary': '自动保留清理删除 {count} 个旧快照',
|
|
216
216
|
'host.autosyncSummary': '自动同步 {direction}{transportSuffix}',
|
|
217
217
|
'host.autosyncTransportSuffix': '({transport})',
|
|
218
|
+
'host.autosyncDirectionPush': '上传',
|
|
219
|
+
'host.autosyncDirectionPull': '下载',
|
|
220
|
+
'host.autosyncDirectionBoth': '双向',
|
|
221
|
+
'host.autosyncDirectionNone': '检查',
|
|
218
222
|
'host.githubMissingClientId': '未配置 GitHub OAuth client_id:请在插件配置中设置 githubClientId(GitHub → Settings → Developer settings → OAuth Apps 注册应用获取)',
|
|
219
223
|
'host.githubFlowGone': 'GitHub 登录流程不存在或已过期,请重新开始',
|
|
220
224
|
'host.restoreBlobEscape': 'blob 路径越界: {blob}',
|
|
@@ -728,6 +732,10 @@ export const en: Record<keyof typeof zh, string> = {
|
|
|
728
732
|
'host.snapshotPruneSummary': 'Automatic retention cleanup removed {count} old snapshot(s)',
|
|
729
733
|
'host.autosyncSummary': 'Autosync {direction}{transportSuffix}',
|
|
730
734
|
'host.autosyncTransportSuffix': ' ({transport})',
|
|
735
|
+
'host.autosyncDirectionPush': 'upload',
|
|
736
|
+
'host.autosyncDirectionPull': 'download',
|
|
737
|
+
'host.autosyncDirectionBoth': 'two-way',
|
|
738
|
+
'host.autosyncDirectionNone': 'check',
|
|
731
739
|
'host.githubMissingClientId': 'GitHub OAuth client_id is not configured: set githubClientId in the plugin config (GitHub → Settings → Developer settings → OAuth Apps → register an app)',
|
|
732
740
|
'host.githubFlowGone': 'The GitHub sign-in flow does not exist or has expired; please start over',
|
|
733
741
|
'host.restoreBlobEscape': 'Blob path outside snapshot directory: {blob}',
|
|
@@ -23,6 +23,7 @@ import {
|
|
|
23
23
|
makeHistoryFilename, isHistoryBasename,
|
|
24
24
|
isValidHistoryRetention, resolveHistoryRetention,
|
|
25
25
|
HISTORY_RETENTION_MIN, HISTORY_RETENTION_MAX, DEFAULT_MIGRATION_RETENTION,
|
|
26
|
+
isAutosyncIdleRound, autosyncDirectionMessageKey, autosyncUnifiedHistoryInput,
|
|
26
27
|
type StoredMigrationHistoryEntry, type MigrationIo,
|
|
27
28
|
} from './migration-history.ts';
|
|
28
29
|
|
|
@@ -405,6 +406,70 @@ test('best-effort:read 目录不存在 → 空结果不 throw', async (t) => {
|
|
|
405
406
|
assert.equal(res.corrupted.length, 0);
|
|
406
407
|
});
|
|
407
408
|
|
|
409
|
+
// ---------- 自动同步:空检查轮与方向本地化(2026-11-08 实测) ----------
|
|
410
|
+
|
|
411
|
+
test('AUTOSYNC:60s 远端探测的空检查轮(upToDate)不构成活动,其余轮次照记', () => {
|
|
412
|
+
// 真机证据(desktop 档案):sync-history.json 每 60s 一条 upToDate,同期 git 工作副本只有 4 次真实 add。
|
|
413
|
+
assert.equal(isAutosyncIdleRound({ status: 'success', skipReason: 'upToDate' }), true, '命中 upToDate = 什么都没传,是轮询');
|
|
414
|
+
assert.equal(isAutosyncIdleRound({ status: 'success', skipReason: 'unchanged' }), false, 'unchanged 走了下行比对,仍要留痕');
|
|
415
|
+
assert.equal(isAutosyncIdleRound({ status: 'success' }), false, '无 skipReason 的 success = 真的推/拉了');
|
|
416
|
+
assert.equal(isAutosyncIdleRound({ status: 'skipped', skipReason: 'conflict' }), false, '冲突必须留在历史里等用户处理');
|
|
417
|
+
assert.equal(isAutosyncIdleRound({ status: 'failed' }), false, '失败永不静默');
|
|
418
|
+
assert.equal(isAutosyncIdleRound({ status: 'partial', skipReason: 'conflict-kept-local' }), false, '待裁决同属真实事件');
|
|
419
|
+
});
|
|
420
|
+
|
|
421
|
+
test('AUTOSYNC:方向枚举走字典键(两侧同键),未知方向不猜', () => {
|
|
422
|
+
assert.equal(autosyncDirectionMessageKey('push'), 'host.autosyncDirectionPush');
|
|
423
|
+
assert.equal(autosyncDirectionMessageKey('pull'), 'host.autosyncDirectionPull');
|
|
424
|
+
assert.equal(autosyncDirectionMessageKey('both'), 'host.autosyncDirectionBoth');
|
|
425
|
+
assert.equal(autosyncDirectionMessageKey('none'), 'host.autosyncDirectionNone');
|
|
426
|
+
assert.equal(autosyncDirectionMessageKey('bogus'), null, '未知方向返回 null,由调用方原样回显');
|
|
427
|
+
assert.equal(autosyncDirectionMessageKey(undefined), null);
|
|
428
|
+
|
|
429
|
+
// 字典词条本身(zh 逐字 + en 无 CJK)由 engine-locale.test.ts 的 t2 清单钉住,这里只管映射。
|
|
430
|
+
});
|
|
431
|
+
|
|
432
|
+
test('AUTOSYNC:统一历史载荷 —— 空轮不写、方向走字典、失败带原因', () => {
|
|
433
|
+
// 与宿主同口径的桩翻译器(字典词条本身由 engine-locale.test.ts 钉住)。
|
|
434
|
+
const dict: Record<string, string> = {
|
|
435
|
+
'host.autosyncDirectionPush': '上传',
|
|
436
|
+
'host.autosyncDirectionPull': '下载',
|
|
437
|
+
'host.autosyncDirectionBoth': '双向',
|
|
438
|
+
'host.autosyncDirectionNone': '检查',
|
|
439
|
+
};
|
|
440
|
+
const msg = (key: string, params?: Record<string, string | number>): string => {
|
|
441
|
+
if (key === 'host.autosyncSummary') return `自动同步 ${String(params?.direction ?? '')}${String(params?.transportSuffix ?? '')}`;
|
|
442
|
+
if (key === 'host.autosyncTransportSuffix') return `(${String(params?.transport ?? '')})`;
|
|
443
|
+
return dict[key] ?? key;
|
|
444
|
+
};
|
|
445
|
+
|
|
446
|
+
assert.equal(autosyncUnifiedHistoryInput({ status: 'success', skipReason: 'upToDate', direction: 'both' }, msg), null, '空检查轮不产生历史条目');
|
|
447
|
+
|
|
448
|
+
const push = autosyncUnifiedHistoryInput({ status: 'success', direction: 'push', transport: 'git', appliedSections: ['settings'] }, msg);
|
|
449
|
+
assert.ok(push);
|
|
450
|
+
assert.equal(push.result, 'success');
|
|
451
|
+
assert.equal(push.source, 'autosync');
|
|
452
|
+
assert.deepEqual(push.sections, ['settings']);
|
|
453
|
+
assert.equal(push.summary, '自动同步 上传(git)');
|
|
454
|
+
assert.equal(push.error, undefined);
|
|
455
|
+
|
|
456
|
+
// sections 必须是副本:历史条目不得与调用方共享可变数组。
|
|
457
|
+
const owned = ['ui'];
|
|
458
|
+
const shared = autosyncUnifiedHistoryInput({ status: 'skipped', direction: 'both', appliedSections: owned }, msg);
|
|
459
|
+
assert.ok(shared);
|
|
460
|
+
assert.notEqual(shared.sections, owned);
|
|
461
|
+
|
|
462
|
+
const failed = autosyncUnifiedHistoryInput({ status: 'failed', direction: 'pull', error: '鉴权失败' }, msg);
|
|
463
|
+
assert.equal(failed?.result, 'failed');
|
|
464
|
+
assert.equal(failed?.error, '鉴权失败', 'failed 必须带原因(sync-history 的 error 优先)');
|
|
465
|
+
assert.equal(autosyncUnifiedHistoryInput({ status: 'failed', direction: 'pull', skipReason: 'network' }, msg)?.error, 'network');
|
|
466
|
+
|
|
467
|
+
assert.equal(autosyncUnifiedHistoryInput({ status: 'partial', direction: 'both' }, msg)?.result, 'pending', '待裁决不是失败');
|
|
468
|
+
|
|
469
|
+
// 未知方向原样回显,不猜、不吞。
|
|
470
|
+
assert.equal(autosyncUnifiedHistoryInput({ status: 'success', direction: 'weird' }, msg)?.summary, '自动同步 weird');
|
|
471
|
+
});
|
|
472
|
+
|
|
408
473
|
test('BOUND:sanitizeEntry 保留合法 UUID operationId/snapshotId/runId', () => {
|
|
409
474
|
const uuid = '00000000-0000-4000-8000-000000000000';
|
|
410
475
|
const entry = sanitizeEntry(makeRaw({ operationId: uuid, snapshotId: uuid, runId: uuid }));
|
|
@@ -31,6 +31,7 @@ import { maskHighEntropy, redact } from '../security/redaction.ts';
|
|
|
31
31
|
import { scanAndRedact } from '../security/secret-scanner.ts';
|
|
32
32
|
import { sha256Hex } from '../utils/hashing.ts';
|
|
33
33
|
import { coreText, engineMsg } from './messages-core.ts';
|
|
34
|
+
import type { MsgFunc } from './msg-types.ts';
|
|
34
35
|
|
|
35
36
|
// ---------- 常量 ----------
|
|
36
37
|
|
|
@@ -103,6 +104,75 @@ export function historyResultFromAutosyncStatus(status: 'success' | 'skipped' |
|
|
|
103
104
|
return status === 'partial' ? 'pending' : 'failed';
|
|
104
105
|
}
|
|
105
106
|
|
|
107
|
+
/**
|
|
108
|
+
* 自动同步「空检查轮」判定:这一轮既没上传也没下行、更没应用任何分区(sections 恒空),
|
|
109
|
+
* 它只是 realtime 通道缺省每 60s 一次(remoteCheckMs)的远端探测 —— 是**轮询**,不是活动。
|
|
110
|
+
*
|
|
111
|
+
* 为什么必须挡在统一历史之外(2026-11-08 真机实测,desktop 档案):
|
|
112
|
+
* ① 1 条/分钟把首页「最近活动」刷成与真推**完全同形**的 success 行(用户据此报告
|
|
113
|
+
* 「每隔 1 分钟都在推送」,而同期 git 工作副本只有 4 次真实 add/deletes);
|
|
114
|
+
* ② 统一历史保留额度(缺省 1000 条)被空轮吃满:实测 1009 条里 934 条是 autosync,
|
|
115
|
+
* 其中 688 条正是 upToDate 空轮,真实事件(备份 22 / 档案操作 40)被最旧淘汰;
|
|
116
|
+
* ③ /history 是目录全量扫描(条目越多首页越慢),空轮只会让它线性变慢。
|
|
117
|
+
*
|
|
118
|
+
* 不丢诊断:sync-history.json 仍逐轮记录(含 upToDate),同步页「上次运行 / 跳过原因」照常显示。
|
|
119
|
+
*/
|
|
120
|
+
export function isAutosyncIdleRound(entry: { status: string; skipReason?: string }): boolean {
|
|
121
|
+
return entry.status === 'success' && entry.skipReason === 'upToDate';
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* 自动同步方向 → 字典键(`both`/`push`/`pull`/`none` 是机器 token,不得直接铺到界面上)。
|
|
126
|
+
* 未知方向返回 null,由调用方原样回显(不猜、不吞)。
|
|
127
|
+
*/
|
|
128
|
+
export function autosyncDirectionMessageKey(direction: unknown): string | null {
|
|
129
|
+
switch (direction) {
|
|
130
|
+
case 'push': return 'host.autosyncDirectionPush';
|
|
131
|
+
case 'pull': return 'host.autosyncDirectionPull';
|
|
132
|
+
case 'both': return 'host.autosyncDirectionBoth';
|
|
133
|
+
case 'none': return 'host.autosyncDirectionNone';
|
|
134
|
+
default: return null;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** 自动同步一轮 → 统一历史条目的载荷(不写则为 null)。 */
|
|
139
|
+
export type AutosyncHistoryInput = Pick<MigrationHistoryEntry, 'result' | 'sections' | 'source' | 'summary' | 'error'>;
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* 自动同步一轮 → 统一历史条目;空检查轮返回 null(调用方直接 return,不写)。
|
|
143
|
+
*
|
|
144
|
+
* 规则落在 core 而不是宿主闭包里:宿主 src/index.ts 有体量棘轮(tests/file-size-ratchet.test.ts),
|
|
145
|
+
* 且这几条判断本身要能单测(空轮不写 / 方向本地化 / failed 带原因)。
|
|
146
|
+
* msg 由调用方注入(宿主传 engineMsg),本模块只管查表不引入新状态。
|
|
147
|
+
*/
|
|
148
|
+
export function autosyncUnifiedHistoryInput(
|
|
149
|
+
entry: {
|
|
150
|
+
status: 'success' | 'skipped' | 'partial' | 'failed';
|
|
151
|
+
direction?: unknown;
|
|
152
|
+
skipReason?: string;
|
|
153
|
+
transport?: string;
|
|
154
|
+
error?: string;
|
|
155
|
+
appliedSections?: string[];
|
|
156
|
+
},
|
|
157
|
+
msg: MsgFunc = engineMsg,
|
|
158
|
+
): AutosyncHistoryInput | null {
|
|
159
|
+
if (isAutosyncIdleRound(entry)) return null;
|
|
160
|
+
const directionKey = autosyncDirectionMessageKey(entry.direction);
|
|
161
|
+
const transport = entry.transport;
|
|
162
|
+
const input: AutosyncHistoryInput = {
|
|
163
|
+
result: historyResultFromAutosyncStatus(entry.status),
|
|
164
|
+
sections: entry.appliedSections === undefined ? [] : [...entry.appliedSections],
|
|
165
|
+
source: 'autosync',
|
|
166
|
+
summary: msg('host.autosyncSummary', {
|
|
167
|
+
direction: directionKey === null ? String(entry.direction ?? '') : msg(directionKey),
|
|
168
|
+
transportSuffix: transport === undefined || transport === '' ? '' : msg('host.autosyncTransportSuffix', { transport }),
|
|
169
|
+
}),
|
|
170
|
+
};
|
|
171
|
+
const error = entry.status === 'failed' ? (entry.error ?? entry.skipReason) : undefined;
|
|
172
|
+
if (error !== undefined) input.error = error;
|
|
173
|
+
return input;
|
|
174
|
+
}
|
|
175
|
+
|
|
106
176
|
/** 触发来源(常量)。 */
|
|
107
177
|
export type MigrationSource = 'api' | 'autosync' | 'backup-scheduler' | 'recovery' | 'cli' | 'internal';
|
|
108
178
|
|
|
@@ -8,13 +8,13 @@
|
|
|
8
8
|
*/
|
|
9
9
|
import test from 'node:test';
|
|
10
10
|
import assert from 'node:assert/strict';
|
|
11
|
-
import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
|
11
|
+
import { lstatSync, mkdirSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSync } from 'node:fs';
|
|
12
12
|
import { tmpdir } from 'node:os';
|
|
13
13
|
import { join } from 'node:path';
|
|
14
14
|
|
|
15
15
|
import {
|
|
16
|
-
isBundlesOnlyEntry, listInstalledPlugins, mergeBundleDeclaration, readInstalled,
|
|
17
|
-
reconcileBundles, reconcileBundlesAfterInstall, resolveProfileDir,
|
|
16
|
+
cleanupAbortedInstall, isBundlesOnlyEntry, listInstalledPlugins, mergeBundleDeclaration, readInstalled,
|
|
17
|
+
readInstalledVersion, reconcileBundles, reconcileBundlesAfterInstall, resolveProfileDir,
|
|
18
18
|
} from './plugin-cli.ts';
|
|
19
19
|
import { enterRescueMode } from './boot-rescue.ts';
|
|
20
20
|
import type { PluginInfo } from './types.ts';
|
|
@@ -366,3 +366,30 @@ test('issue #95: mergeBundleDeclaration 只追加一个名字;形状不符一
|
|
|
366
366
|
'bundles 字段缺省 → null(非空 bundles 会替换默认插件栈,绝不凭空造)',
|
|
367
367
|
);
|
|
368
368
|
});
|
|
369
|
+
|
|
370
|
+
test('中止安装清理:包目录是悬空 junction 时也必须删净(fs.rmSync 静默空转的回归)', () => {
|
|
371
|
+
const { profileDir, cleanup } = makeTempProfile({ 'pkg-b': '1.0.0' }, ['pkg-b']);
|
|
372
|
+
try {
|
|
373
|
+
const pkgDir = join(profileDir, 'node_modules', 'pkg-b');
|
|
374
|
+
mkdirSync(join(profileDir, 'node_modules'), { recursive: true });
|
|
375
|
+
// DSH 真实形态:node_modules/<pkg> 直接链到 .dsh-module-fallback;fallback 被宿主升级清掉后即成悬空链接。
|
|
376
|
+
symlinkSync(
|
|
377
|
+
join(profileDir, '.dsh-module-fallback', 'node_modules', 'pkg-b'),
|
|
378
|
+
pkgDir,
|
|
379
|
+
process.platform === 'win32' ? 'junction' : 'dir',
|
|
380
|
+
);
|
|
381
|
+
assert.ok(lstatSync(pkgDir).isSymbolicLink(), '链接条目本身在(existsSync 对悬空链接返回 false,不能当判据)');
|
|
382
|
+
|
|
383
|
+
cleanupAbortedInstall(profileDir, 'pkg-b');
|
|
384
|
+
|
|
385
|
+
assert.throws(() => lstatSync(pkgDir), /ENOENT/, '悬空链接必须被真的删掉,否则「跳过当前插件」留半装残留');
|
|
386
|
+
const manifest = JSON.parse(readFileSync(join(profileDir, 'package.json'), 'utf8')) as {
|
|
387
|
+
dependencies: Record<string, string>;
|
|
388
|
+
dsh: { profile: { bundles: string[] } };
|
|
389
|
+
};
|
|
390
|
+
assert.equal('pkg-b' in manifest.dependencies, false, '依赖行同步摘掉(下次安装不再撞缺包)');
|
|
391
|
+
assert.ok(Array.isArray(manifest.dsh.profile.bundles), 'bundles 形状不变');
|
|
392
|
+
} finally {
|
|
393
|
+
cleanup();
|
|
394
|
+
}
|
|
395
|
+
});
|
package/src/core/plugin-cli.ts
CHANGED
|
@@ -19,10 +19,11 @@ import { engineMsg } from './messages-core.ts'
|
|
|
19
19
|
import { isConcreteVersion } from './validator.ts';
|
|
20
20
|
import { spawn } from 'node:child_process'
|
|
21
21
|
import type { ChildProcess, SpawnOptions } from 'node:child_process'
|
|
22
|
-
import { readFileSync
|
|
22
|
+
import { readFileSync } from 'node:fs'
|
|
23
23
|
import { basename, dirname, join, resolve } from 'node:path'
|
|
24
24
|
|
|
25
25
|
import { atomicWriteFileSync } from '../utils/atomic-write.ts'
|
|
26
|
+
import { removeTreeSync } from '../utils/remove-tree.ts'
|
|
26
27
|
import { isDesktopProfile, resolveDesktopCarrier } from '../utils/desktop-carrier.ts'
|
|
27
28
|
import { isRescueActiveSync } from './boot-rescue.ts'
|
|
28
29
|
import type { PluginInfo } from './types.ts'
|
|
@@ -301,6 +302,11 @@ function isRescueActiveForProfileDir(profileDir: string): boolean {
|
|
|
301
302
|
* 直接启动 DSH 会因缺包而失败。本函数把 manifest 恢复为「该插件未安装」的一致状态,
|
|
302
303
|
* 消除「声明了依赖但没装全」的启动风险。pnpm-lock.yaml 不触碰(运行时不被读取,
|
|
303
304
|
* 下次安装 pnpm 自行对齐)。尽力而为:任何失败不抛错,由调用方记录告警。
|
|
305
|
+
*
|
|
306
|
+
* 删包目录必须走 removeTreeSync 而不是 fs.rmSync(recursive):包目录常是**链接**(DSH 把
|
|
307
|
+
* node_modules/<pkg> 链到 .dsh-module-fallback),链接目标消失后就是悬空链接,rmSync 遇到
|
|
308
|
+
* 这种树会静默空转(不抛错、一个都不删),于是「跳过当前插件」留下半装残留,下次安装撞缺包。
|
|
309
|
+
* removeTreeSync 只删链接本身(绝不跟进去删 fallback 里的实体)并在删不净时抛错。
|
|
304
310
|
*/
|
|
305
311
|
export function cleanupAbortedInstall(profileDir: string, pkg: string): void {
|
|
306
312
|
const manifest = readProfileManifest(profileDir)
|
|
@@ -315,9 +321,11 @@ export function cleanupAbortedInstall(profileDir: string, pkg: string): void {
|
|
|
315
321
|
reconcileBundles(profileDir)
|
|
316
322
|
}
|
|
317
323
|
try {
|
|
318
|
-
|
|
324
|
+
removeTreeSync(join(profileDir, 'node_modules', pkg))
|
|
319
325
|
} catch {
|
|
320
|
-
//
|
|
326
|
+
// 尽力而为:目录可能本来就不存在(正常情况下 removeTreeSync 对缺失目标是不抛的),
|
|
327
|
+
// 剩下的失败只有「真被占用 / 权限不足」——这里仍不打断「用户跳过」的流程,
|
|
328
|
+
// 但绝不再让悬空链接这种可修的形态走到这里。
|
|
321
329
|
}
|
|
322
330
|
}
|
|
323
331
|
|
package/src/index.ts
CHANGED
|
@@ -107,6 +107,7 @@ import { PROJECT_KEY_RE, readLogCwdFromBytes, rewriteSessionLogDir } from './uti
|
|
|
107
107
|
import { probeSessionFormats, resolveSessionFormatVersion } from './utils/session-format.ts'
|
|
108
108
|
import { describePatchYamlError, dumpPatchDocument, loadPatchDocument } from './utils/patch-yaml.ts'
|
|
109
109
|
import { atomicCopyFile, atomicWriteFile } from './utils/atomic-write.ts'
|
|
110
|
+
import { removeTree } from './utils/remove-tree.ts'
|
|
110
111
|
import { isENOENT } from './utils/guards.ts'
|
|
111
112
|
import { EnvironmentLockManager, runWithMutationLock, type MutationLockContext, type MutationLockPort } from './utils/env-lock.ts'
|
|
112
113
|
import { activeProxySummary } from './utils/proxy.ts'
|
|
@@ -131,7 +132,7 @@ import type { RetentionPolicy } from './sync/retention-policy.ts'
|
|
|
131
132
|
import type { PruneSelector } from './core/backup.ts'
|
|
132
133
|
import { selectPruneCandidates } from './core/backup.ts'
|
|
133
134
|
import { appendAutosyncEntry } from './sync/sync-history.ts'
|
|
134
|
-
import { MigrationStore, type MigrationKind, type MigrationResult,
|
|
135
|
+
import { MigrationStore, type MigrationKind, type MigrationResult, autosyncUnifiedHistoryInput, MIGRATION_HISTORY_DIR } from './core/migration-history.ts'
|
|
135
136
|
import { createHistoryPruner } from './core/history-prune.ts'
|
|
136
137
|
import { isWebDavConfig, isS3Config, isGistConfig, channelOf, SYNC_CHANNELS, GIST_PROVIDER, cloudSecretRef } from './sync/sync-config.ts'
|
|
137
138
|
import {
|
|
@@ -182,7 +183,7 @@ export const name = 'config-manager'
|
|
|
182
183
|
export const inject = ['settings', 'credentials']
|
|
183
184
|
|
|
184
185
|
/** Plugin version, kept in sync with package.json ("version"). */
|
|
185
|
-
export const PLUGIN_VERSION = '0.
|
|
186
|
+
export const PLUGIN_VERSION = '0.6.0'
|
|
186
187
|
|
|
187
188
|
/** Plugin own package name — excluded from its own exported plugins list. */
|
|
188
189
|
const PLUGIN_NAME = 'dsh-config-manager'
|
|
@@ -1831,8 +1832,12 @@ export class DshFileSystemFacade implements FileSystemFacade {
|
|
|
1831
1832
|
await atomicCopyFile(this.abs(from), this.abs(to))
|
|
1832
1833
|
}
|
|
1833
1834
|
|
|
1835
|
+
/**
|
|
1836
|
+
* 递归删除(home 内,缺失目标视为成功)。**不用 fs.rm(recursive)**:目录里有悬空链接时它静默
|
|
1837
|
+
* 空转(报告写「已还原」而磁盘原样在)。removeTree 逐条删、只删链接本身、删不净就抛错。
|
|
1838
|
+
*/
|
|
1834
1839
|
async remove(relPath: string): Promise<void> {
|
|
1835
|
-
await
|
|
1840
|
+
await removeTree(this.abs(relPath))
|
|
1836
1841
|
}
|
|
1837
1842
|
|
|
1838
1843
|
/** 仅路径列表(既有契约):委托 listRecursiveDetailed,丢弃诊断。 */
|
|
@@ -2792,17 +2797,10 @@ function makeRoutes(deps: RoutesDeps): {
|
|
|
2792
2797
|
// Phase 6:autosync 既写 sync-history.json(既有语义),也写统一迁移历史(COMPLETE 不变量)。
|
|
2793
2798
|
appendHistoryFn: async (entry) => {
|
|
2794
2799
|
await appendAutosyncEntry(syncDir, entry).catch(() => undefined)
|
|
2795
|
-
|
|
2796
|
-
|
|
2797
|
-
|
|
2798
|
-
|
|
2799
|
-
source: 'autosync',
|
|
2800
|
-
summary: engineMsg('host.autosyncSummary', {
|
|
2801
|
-
direction: entry.direction,
|
|
2802
|
-
transportSuffix: entry.transport !== undefined ? engineMsg('host.autosyncTransportSuffix', { transport: entry.transport }) : '',
|
|
2803
|
-
}),
|
|
2804
|
-
error: entry.status === 'failed' ? (entry.error ?? entry.skipReason) : undefined,
|
|
2805
|
-
}).catch(() => undefined)
|
|
2800
|
+
// 空检查轮(每 60s 一次的远端探测,什么都没传)不进统一历史;payload 组装与判据见 core。
|
|
2801
|
+
const input = autosyncUnifiedHistoryInput(entry, engineMsg)
|
|
2802
|
+
if (input === null) return
|
|
2803
|
+
await history.append({ kind: 'autosync', ...input }).catch(() => undefined)
|
|
2806
2804
|
},
|
|
2807
2805
|
})
|
|
2808
2806
|
// P1-B:调度器不再在 makeRoutes 内同步 start —— 由 apply() 在「启动 recovery 分类完成后、仅 NORMAL」时启动。
|
|
@@ -349,6 +349,26 @@ test('remove:物理删除目录;当前 profile 需显式确认', () => {
|
|
|
349
349
|
}
|
|
350
350
|
})
|
|
351
351
|
|
|
352
|
+
test('remove:含悬空 junction 的档案也能删净(fs.rm 静默失效的回归)', () => {
|
|
353
|
+
const { mgr, home, cleanup } = makeManager()
|
|
354
|
+
try {
|
|
355
|
+
mgr.create('work', 'base')
|
|
356
|
+
const dir = join(home, 'profiles', 'work')
|
|
357
|
+
mkdirSync(join(dir, 'node_modules'), { recursive: true })
|
|
358
|
+
// DSH 的真实形态:node_modules/<pkg> → 已消失的 .dsh-module-fallback/node_modules/<pkg>
|
|
359
|
+
symlinkSync(
|
|
360
|
+
join(dir, '.dsh-module-fallback', 'node_modules', 'immer'),
|
|
361
|
+
join(dir, 'node_modules', 'immer'),
|
|
362
|
+
process.platform === 'win32' ? 'junction' : 'dir',
|
|
363
|
+
)
|
|
364
|
+
mgr.remove('work')
|
|
365
|
+
// 曾经:fs.rmSync 遇到悬空链接「不抛错也不删」,路由回 200 + 台账 success,档案留在磁盘上
|
|
366
|
+
assert.equal(existsSync(dir), false, '悬空 junction 不得让删除静默失败')
|
|
367
|
+
} finally {
|
|
368
|
+
cleanup()
|
|
369
|
+
}
|
|
370
|
+
})
|
|
371
|
+
|
|
352
372
|
test('desktop 保留档案:删除与改名一律拒绝(managedProfile),只读操作照常', async () => {
|
|
353
373
|
const { mgr, home, cleanup } = makeManager()
|
|
354
374
|
try {
|
|
@@ -18,14 +18,17 @@
|
|
|
18
18
|
* 历史上这里还有一个 `<dataDir>/next-profile`「下次启动」标记——DSH 根本没有消费者,
|
|
19
19
|
* 只会误导用户(2026-09 按产品决策整体移除,连同前端按钮)。
|
|
20
20
|
*
|
|
21
|
-
* 物理删除:remove() 走 rmSync(recursive)
|
|
22
|
-
*
|
|
21
|
+
* 物理删除:remove() 走 utils/remove-tree 的 removeTreeSync —— 不能用 fs.rmSync(recursive):
|
|
22
|
+
* 目录里只要有一个**悬空链接**(DSH 的 node_modules/<pkg> → 已消失的 .dsh-module-fallback),
|
|
23
|
+
* rmSync 会不报错也不删任何东西(实测),于是「删除成功」而档案原样留在磁盘上。
|
|
24
|
+
* profile 目录内的 junction(pnpm 链接)只删链接本身,不会跟随进 pnpm store。
|
|
23
25
|
*/
|
|
24
26
|
import { existsSync, mkdirSync, readdirSync, renameSync, rmSync, statSync, type Dirent } from 'node:fs'
|
|
25
27
|
// 复制走 async fs(整档案上万个文件,cpSync 会把宿主事件循环卡住二十多秒)
|
|
26
|
-
import { copyFile, cp, mkdir, readdir, readlink,
|
|
28
|
+
import { copyFile, cp, mkdir, readdir, readlink, stat, symlink, unlink } from 'node:fs/promises'
|
|
27
29
|
import { dirname, isAbsolute, join, relative, resolve, sep } from 'node:path'
|
|
28
30
|
import { atomicWriteFileSync } from '../utils/atomic-write.ts'
|
|
31
|
+
import { removeTree, removeTreeSync } from '../utils/remove-tree.ts'
|
|
29
32
|
import { isRecord } from '../utils/guards.ts'
|
|
30
33
|
import { resolveProfileDir, validateProfileName } from '../core/plugin-cli.ts'
|
|
31
34
|
import { readTextSafe } from './dsh-profile-io.ts'
|
|
@@ -103,9 +106,15 @@ export interface DshProfileIncompleteCopiesScan {
|
|
|
103
106
|
/** t89:profiles 根目录枚举失败的原因码(诊断/日志用)。 */
|
|
104
107
|
export const PROFILES_ROOT_UNREADABLE = 'profiles-root-unreadable'
|
|
105
108
|
|
|
106
|
-
/**
|
|
109
|
+
/**
|
|
110
|
+
* 回滚复制目标目录的缺省删除实现(不存在不算错)。
|
|
111
|
+
*
|
|
112
|
+
* 必须走 removeTree 而不是 fs.rm(recursive):半截副本里可能已经有指向
|
|
113
|
+
* `.dsh-module-fallback` 的悬空 junction,fs.rm 会「成功」却一个字节都不删,
|
|
114
|
+
* 回滚于是无声失败。
|
|
115
|
+
*/
|
|
107
116
|
const defaultProfileRemove = async (target: string): Promise<void> => {
|
|
108
|
-
await
|
|
117
|
+
await removeTree(target)
|
|
109
118
|
}
|
|
110
119
|
|
|
111
120
|
/**
|
|
@@ -413,7 +422,7 @@ export class DshProfileManager {
|
|
|
413
422
|
}
|
|
414
423
|
|
|
415
424
|
/**
|
|
416
|
-
* 物理删除 profile 目录(
|
|
425
|
+
* 物理删除 profile 目录(removeTreeSync;目录内 junction 只删链接本身,删不干净报 deleteFailed)。
|
|
417
426
|
* 当前运行中的 profile 需要 allowCurrent=true 显式确认(删除会让重启后的实例直接失败)。
|
|
418
427
|
*
|
|
419
428
|
* Desktop 独占档案(desktop)**一律拒绝**(连 allowCurrent 也不行):它由桌面端应用自己
|
|
@@ -428,7 +437,16 @@ export class DshProfileManager {
|
|
|
428
437
|
if (name === this.currentProfile() && opts.allowCurrent !== true) {
|
|
429
438
|
throw new DshProfileError('currentProfile')
|
|
430
439
|
}
|
|
431
|
-
rmSync(
|
|
440
|
+
// 不用 rmSync(recursive):悬空 junction 会让它静默空转(见 utils/remove-tree.ts 头注)。
|
|
441
|
+
// 删不掉必须变成可见错误码,绝不回 200 —— 那正是「点了删除、档案还在」的成因。
|
|
442
|
+
try {
|
|
443
|
+
removeTreeSync(dir)
|
|
444
|
+
} catch (error) {
|
|
445
|
+
throw new DshProfileError('deleteFailed', error instanceof Error ? error.message : String(error))
|
|
446
|
+
}
|
|
447
|
+
if (existsSync(dir)) {
|
|
448
|
+
throw new DshProfileError('deleteFailed', `profile directory still exists after removal: ${dir}`)
|
|
449
|
+
}
|
|
432
450
|
}
|
|
433
451
|
|
|
434
452
|
/**
|