@twinsearth/timemachine-dsh-plugin 0.2.3 → 0.2.6
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 +43 -0
- package/README.md +10 -7
- package/docs/ARCHITECTURE.md +10 -5
- package/docs/REQUIREMENTS.md +20 -2
- package/docs/TESTPLAN.md +31 -5
- package/lib/client.js +78 -46
- package/package.json +1 -1
- package/plugin.json +1 -1
- package/scripts/host-menu-patch.mjs +1 -0
- package/src/plugin-compat.mjs +241 -0
- package/src/plugins.mjs +174 -17
- package/src/point.mjs +26 -2
- package/src/restore.mjs +88 -18
- package/src/util/paths.mjs +23 -1
- package/src/version.mjs +1 -1
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DSH plugin compatibility checks used before a third-party install.
|
|
3
|
+
*
|
|
4
|
+
* DSH itself refuses to load a plugin whose `engines.dsh` (or DSH peer
|
|
5
|
+
* dependency) does not include the running runtime. A restore must not install
|
|
6
|
+
* such a plugin only for the next boot to reject it, so this module evaluates
|
|
7
|
+
* the same declaration before invoking the package manager.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import fs from 'node:fs';
|
|
11
|
+
import path from 'node:path';
|
|
12
|
+
import zlib from 'node:zlib';
|
|
13
|
+
|
|
14
|
+
import { readTar } from './archive/tar.mjs';
|
|
15
|
+
|
|
16
|
+
const SEMVER = /^v?(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z.-]+))?(?:\+[0-9A-Za-z.-]+)?$/u;
|
|
17
|
+
|
|
18
|
+
/** Parse a complete semantic version, preserving prerelease identifiers. */
|
|
19
|
+
export function parseSemver(value) {
|
|
20
|
+
if (typeof value !== 'string') return null;
|
|
21
|
+
const match = SEMVER.exec(value.trim());
|
|
22
|
+
if (match === null) return null;
|
|
23
|
+
return {
|
|
24
|
+
major: Number(match[1]),
|
|
25
|
+
minor: Number(match[2]),
|
|
26
|
+
patch: Number(match[3]),
|
|
27
|
+
prerelease: match[4] === undefined ? [] : match[4].split('.'),
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function compareIdentifier(left, right) {
|
|
32
|
+
const leftNumeric = /^\d+$/u.test(left);
|
|
33
|
+
const rightNumeric = /^\d+$/u.test(right);
|
|
34
|
+
if (leftNumeric && rightNumeric) return Number(left) - Number(right);
|
|
35
|
+
if (leftNumeric) return -1;
|
|
36
|
+
if (rightNumeric) return 1;
|
|
37
|
+
return left.localeCompare(right);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** semver precedence, including prerelease ordering; build metadata is ignored. */
|
|
41
|
+
export function compareSemver(left, right) {
|
|
42
|
+
for (const key of ['major', 'minor', 'patch']) {
|
|
43
|
+
if (left[key] !== right[key]) return left[key] - right[key];
|
|
44
|
+
}
|
|
45
|
+
if (left.prerelease.length === 0 && right.prerelease.length === 0) return 0;
|
|
46
|
+
if (left.prerelease.length === 0) return 1;
|
|
47
|
+
if (right.prerelease.length === 0) return -1;
|
|
48
|
+
const length = Math.max(left.prerelease.length, right.prerelease.length);
|
|
49
|
+
for (let index = 0; index < length; index += 1) {
|
|
50
|
+
const a = left.prerelease[index];
|
|
51
|
+
const b = right.prerelease[index];
|
|
52
|
+
if (a === undefined) return -1;
|
|
53
|
+
if (b === undefined) return 1;
|
|
54
|
+
const compared = compareIdentifier(a, b);
|
|
55
|
+
if (compared !== 0) return compared;
|
|
56
|
+
}
|
|
57
|
+
return 0;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function partialVersion(value) {
|
|
61
|
+
const text = String(value ?? '').trim();
|
|
62
|
+
const match = /^v?(\d+)(?:\.(\d+|[xX*]))?(?:\.(\d+|[xX*]))?(?:-([0-9A-Za-z.-]+))?$/u.exec(text);
|
|
63
|
+
if (match === null) return null;
|
|
64
|
+
const minor = match[2] === undefined || /^[xX*]$/u.test(match[2]) ? null : Number(match[2]);
|
|
65
|
+
const patch = match[3] === undefined || /^[xX*]$/u.test(match[3]) ? null : Number(match[3]);
|
|
66
|
+
return {
|
|
67
|
+
major: Number(match[1]),
|
|
68
|
+
minor,
|
|
69
|
+
patch,
|
|
70
|
+
prerelease: match[4] === undefined ? [] : match[4].split('.'),
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function floorVersion(parts) {
|
|
75
|
+
return { major: parts.major, minor: parts.minor ?? 0, patch: parts.patch ?? 0, prerelease: parts.prerelease };
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function comparatorMatches(version, operator, parts) {
|
|
79
|
+
const base = floorVersion(parts);
|
|
80
|
+
const compared = compareSemver(version, base);
|
|
81
|
+
if (operator === '>=') return compared >= 0;
|
|
82
|
+
if (operator === '>') return compared > 0;
|
|
83
|
+
if (operator === '<=') return compared <= 0;
|
|
84
|
+
if (operator === '<') return compared < 0;
|
|
85
|
+
return compared === 0;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function caretRange(parts) {
|
|
89
|
+
const low = floorVersion(parts);
|
|
90
|
+
let high;
|
|
91
|
+
if (parts.minor === null) high = { major: parts.major + 1, minor: 0, patch: 0, prerelease: [] };
|
|
92
|
+
else if (parts.major > 0) high = { major: parts.major + 1, minor: 0, patch: 0, prerelease: [] };
|
|
93
|
+
else if (parts.minor > 0) high = { major: 0, minor: parts.minor + 1, patch: 0, prerelease: [] };
|
|
94
|
+
else if (parts.patch === null) high = { major: 0, minor: 1, patch: 0, prerelease: [] };
|
|
95
|
+
else high = { major: 0, minor: 0, patch: parts.patch + 1, prerelease: [] };
|
|
96
|
+
return { low, high };
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function tildeRange(parts) {
|
|
100
|
+
const low = floorVersion(parts);
|
|
101
|
+
const high = parts.minor === null
|
|
102
|
+
? { major: parts.major + 1, minor: 0, patch: 0, prerelease: [] }
|
|
103
|
+
: { major: parts.major, minor: parts.minor + 1, patch: 0, prerelease: [] };
|
|
104
|
+
return { low, high };
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function matchesComparator(version, raw) {
|
|
108
|
+
const token = raw.trim();
|
|
109
|
+
if (token === '' || token === '*' || /^[xX]$/u.test(token)) return true;
|
|
110
|
+
const operatorMatch = /^(>=|<=|>|<|=|\^|~)?\s*(.+)$/u.exec(token);
|
|
111
|
+
if (operatorMatch === null) return false;
|
|
112
|
+
const operator = operatorMatch[1] ?? '';
|
|
113
|
+
const parts = partialVersion(operatorMatch[2]);
|
|
114
|
+
if (parts === null) return false;
|
|
115
|
+
if (operator === '^') {
|
|
116
|
+
const range = caretRange(parts);
|
|
117
|
+
return compareSemver(version, range.low) >= 0 && compareSemver(version, range.high) < 0;
|
|
118
|
+
}
|
|
119
|
+
if (operator === '~') {
|
|
120
|
+
const range = tildeRange(parts);
|
|
121
|
+
return compareSemver(version, range.low) >= 0 && compareSemver(version, range.high) < 0;
|
|
122
|
+
}
|
|
123
|
+
if (parts.minor === null || parts.patch === null) {
|
|
124
|
+
const low = floorVersion(parts);
|
|
125
|
+
const high = parts.minor === null
|
|
126
|
+
? { major: parts.major + 1, minor: 0, patch: 0, prerelease: [] }
|
|
127
|
+
: { major: parts.major, minor: parts.minor + 1, patch: 0, prerelease: [] };
|
|
128
|
+
if (operator === '>=') return compareSemver(version, low) >= 0;
|
|
129
|
+
if (operator === '>') return compareSemver(version, high) >= 0;
|
|
130
|
+
if (operator === '<') return compareSemver(version, low) < 0;
|
|
131
|
+
if (operator === '<=') return compareSemver(version, high) < 0;
|
|
132
|
+
return compareSemver(version, low) >= 0 && compareSemver(version, high) < 0;
|
|
133
|
+
}
|
|
134
|
+
return comparatorMatches(version, operator === '' ? '=' : operator, parts);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* A dependency-free subset of semver that covers every range shape used by DSH
|
|
139
|
+
* plugins: exact, comparators, `||`, caret, tilde, wildcards and hyphen ranges.
|
|
140
|
+
* Prereleases participate, matching DSH's `includePrerelease: true`.
|
|
141
|
+
*/
|
|
142
|
+
export function satisfiesRange(version, range) {
|
|
143
|
+
const parsedVersion = parseSemver(version);
|
|
144
|
+
if (parsedVersion === null || typeof range !== 'string' || range.trim() === '') return false;
|
|
145
|
+
for (const alternative of range.split('||')) {
|
|
146
|
+
const text = alternative.trim().replace(/(>=|<=|>|<|=|\^|~)\s+/gu, '$1');
|
|
147
|
+
if (text === '') continue;
|
|
148
|
+
const hyphen = /^(\S+)\s+-\s+(\S+)$/u.exec(text);
|
|
149
|
+
if (hyphen !== null) {
|
|
150
|
+
const low = partialVersion(hyphen[1]);
|
|
151
|
+
const high = partialVersion(hyphen[2]);
|
|
152
|
+
if (low === null || high === null) continue;
|
|
153
|
+
const lowVersion = floorVersion(low);
|
|
154
|
+
const highVersion = floorVersion(high);
|
|
155
|
+
if (compareSemver(parsedVersion, lowVersion) >= 0 && compareSemver(parsedVersion, highVersion) <= 0) return true;
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
const comparators = text.split(/\s+/u).filter((token) => token !== '');
|
|
159
|
+
if (comparators.every((token) => matchesComparator(parsedVersion, token))) return true;
|
|
160
|
+
}
|
|
161
|
+
return false;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Return the DSH requirements a manifest does not satisfy. An own
|
|
166
|
+
* `engines.dsh` decides alone, matching DSH's app-boot behavior.
|
|
167
|
+
*/
|
|
168
|
+
export function unmetDshRequirements(manifest, runtimeVersion) {
|
|
169
|
+
if (manifest === null || typeof manifest !== 'object') return {};
|
|
170
|
+
const engines = manifest.engines;
|
|
171
|
+
if (engines !== null && typeof engines === 'object' && Object.hasOwn(engines, 'dsh')) {
|
|
172
|
+
const range = engines.dsh;
|
|
173
|
+
if (typeof range !== 'string') return { 'engines.dsh': String(range) };
|
|
174
|
+
return satisfiesRange(runtimeVersion, range) ? {} : { 'engines.dsh': range };
|
|
175
|
+
}
|
|
176
|
+
const peers = manifest.peerDependencies;
|
|
177
|
+
if (peers === null || typeof peers !== 'object') return {};
|
|
178
|
+
const unmet = {};
|
|
179
|
+
for (const [name, range] of Object.entries(peers)) {
|
|
180
|
+
if (name !== '@deepseek-ai/dsh' && !name.startsWith('@deepseek-ai/dsh-')) continue;
|
|
181
|
+
if (typeof range !== 'string') {
|
|
182
|
+
unmet[name] = String(range);
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
const effective = ['workspace:^', 'workspace:~', 'workspace:*'].includes(range) ? runtimeVersion : range;
|
|
186
|
+
if (!satisfiesRange(runtimeVersion, effective)) unmet[name] = range;
|
|
187
|
+
}
|
|
188
|
+
return unmet;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** Read `package/package.json` from an npm `.tgz` without spawning tar. */
|
|
192
|
+
export function readPackageManifestFromTarball(file) {
|
|
193
|
+
const archive = zlib.gunzipSync(fs.readFileSync(file));
|
|
194
|
+
for (const entry of readTar(archive)) {
|
|
195
|
+
if (entry.name === 'package/package.json' || entry.name.endsWith('/package/package.json')) {
|
|
196
|
+
return JSON.parse(entry.data.toString('utf8'));
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
throw new Error('the saved package has no package/package.json');
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function runtimeVersionFromJson(file) {
|
|
203
|
+
try {
|
|
204
|
+
const raw = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
205
|
+
const value = typeof raw?.desktopVersion === 'string' ? raw.desktopVersion : raw?.version;
|
|
206
|
+
return parseSemver(value) === null ? null : value;
|
|
207
|
+
} catch {
|
|
208
|
+
return null;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Resolve the running DSH version. The desktop runtime writes it to
|
|
214
|
+
* `resources/runtime/primary-runtime/runtime.json`; tests and embedders can
|
|
215
|
+
* pass an explicit version. Returns `null` when no trustworthy version exists.
|
|
216
|
+
*/
|
|
217
|
+
export function resolveDshRuntimeVersion(input = {}) {
|
|
218
|
+
const explicit = input.version ?? process.env.DSH_VERSION ?? process.env.DSH_CLIENT_VERSION;
|
|
219
|
+
if (parseSemver(explicit) !== null) return String(explicit).trim();
|
|
220
|
+
const execPath = input.execPath ?? process.execPath;
|
|
221
|
+
const dir = path.dirname(execPath);
|
|
222
|
+
const candidates = [
|
|
223
|
+
path.join(dir, 'resources', 'runtime', 'primary-runtime', 'runtime.json'),
|
|
224
|
+
path.join(dir, 'runtime', 'primary-runtime', 'runtime.json'),
|
|
225
|
+
// The DSH host may run the plugin under the bundled Node binary:
|
|
226
|
+
// resources/runtime/primary-runtime/node/node.exe -> ../runtime.json
|
|
227
|
+
path.join(dir, '..', 'runtime.json'),
|
|
228
|
+
path.join(dir, '..', '..', 'runtime', 'primary-runtime', 'runtime.json'),
|
|
229
|
+
];
|
|
230
|
+
if (typeof input.profileDir === 'string' && input.profileDir !== '') {
|
|
231
|
+
candidates.push(
|
|
232
|
+
path.join(input.profileDir, 'node_modules', '@deepseek-ai', 'dsh', 'package.json'),
|
|
233
|
+
path.join(input.profileDir, 'node_modules', '@deepseek-ai', 'dsh-app-boot', 'package.json'),
|
|
234
|
+
);
|
|
235
|
+
}
|
|
236
|
+
for (const file of candidates) {
|
|
237
|
+
const version = runtimeVersionFromJson(file);
|
|
238
|
+
if (version !== null) return version;
|
|
239
|
+
}
|
|
240
|
+
return null;
|
|
241
|
+
}
|
package/src/plugins.mjs
CHANGED
|
@@ -10,6 +10,8 @@ import fs from 'node:fs';
|
|
|
10
10
|
import path from 'node:path';
|
|
11
11
|
import { spawn } from 'node:child_process';
|
|
12
12
|
|
|
13
|
+
import { readPackageManifestFromTarball, resolveDshRuntimeVersion, unmetDshRequirements } from './plugin-compat.mjs';
|
|
14
|
+
|
|
13
15
|
/** The package doing the restore must never reinstall itself while running. */
|
|
14
16
|
export const SELF_PLUGIN = '@twinsearth/timemachine-dsh-plugin';
|
|
15
17
|
|
|
@@ -142,6 +144,93 @@ export function readProfilePlugins(input) {
|
|
|
142
144
|
};
|
|
143
145
|
}
|
|
144
146
|
|
|
147
|
+
/**
|
|
148
|
+
* Whether a profile can resolve a package without asking the DSH installation.
|
|
149
|
+
* Official DSH bundles live in the app and third-party packages live under the
|
|
150
|
+
* profile's node_modules. We deliberately do not trust package.json alone: a
|
|
151
|
+
* restore may have written an archived dependency that was never installed.
|
|
152
|
+
*/
|
|
153
|
+
export function isProfilePackageAvailable(profileDir, name) {
|
|
154
|
+
if (!isSafePackageName(name)) return false;
|
|
155
|
+
if (name === SELF_PLUGIN || name.startsWith(OFFICIAL_SCOPE)) return true;
|
|
156
|
+
const segments = name.split('/');
|
|
157
|
+
return fs.existsSync(path.join(profileDir, 'node_modules', ...segments, 'package.json'));
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Remove bundle/dependency entries that the target profile cannot resolve.
|
|
162
|
+
* This keeps a restore from writing another machine's plugin manifest into a
|
|
163
|
+
* profile where the plugin download failed or was skipped. The caller should
|
|
164
|
+
* surface `dropped_*` as a yellow restore warning.
|
|
165
|
+
*/
|
|
166
|
+
export function sanitizeProfilePackage(input) {
|
|
167
|
+
const profileDir = input?.profileDir;
|
|
168
|
+
const source = input?.pkg;
|
|
169
|
+
const installed = new Set(Array.isArray(input?.installed) ? input.installed : []);
|
|
170
|
+
const pkg = JSON.parse(JSON.stringify(source ?? {}));
|
|
171
|
+
const droppedBundles = [];
|
|
172
|
+
const droppedDependencies = [];
|
|
173
|
+
const available = (name) => isProfilePackageAvailable(profileDir, name) || installed.has(name);
|
|
174
|
+
|
|
175
|
+
if (Array.isArray(pkg?.dsh?.profile?.bundles)) {
|
|
176
|
+
pkg.dsh.profile.bundles = pkg.dsh.profile.bundles.filter((name) => {
|
|
177
|
+
if (available(name)) return true;
|
|
178
|
+
droppedBundles.push(name);
|
|
179
|
+
return false;
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
if (pkg.dependencies !== null && typeof pkg.dependencies === 'object' && !Array.isArray(pkg.dependencies)) {
|
|
183
|
+
for (const name of Object.keys(pkg.dependencies)) {
|
|
184
|
+
if (available(name)) continue;
|
|
185
|
+
droppedDependencies.push(name);
|
|
186
|
+
delete pkg.dependencies[name];
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
return { pkg, dropped_bundles: droppedBundles, dropped_dependencies: droppedDependencies };
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Drop top-level cordis.patch.yml rows whose `name` is a third-party package
|
|
194
|
+
* that is not installed in the target profile. Rows without a name (pure
|
|
195
|
+
* config overrides) are preserved.
|
|
196
|
+
*/
|
|
197
|
+
export function filterProfilePatch(input) {
|
|
198
|
+
const profileDir = input?.profileDir;
|
|
199
|
+
const lines = String(input?.text ?? '').split(/\r?\n/u);
|
|
200
|
+
const output = [];
|
|
201
|
+
const dropped = [];
|
|
202
|
+
let block = [];
|
|
203
|
+
let blockName = null;
|
|
204
|
+
const flush = () => {
|
|
205
|
+
if (block.length === 0) return;
|
|
206
|
+
if (blockName !== null && isThirdParty(blockName) && !isProfilePackageAvailable(profileDir, blockName)) {
|
|
207
|
+
dropped.push(blockName);
|
|
208
|
+
} else {
|
|
209
|
+
output.push(...block);
|
|
210
|
+
}
|
|
211
|
+
block = [];
|
|
212
|
+
blockName = null;
|
|
213
|
+
};
|
|
214
|
+
for (const line of lines) {
|
|
215
|
+
if (/^-\s/u.test(line)) {
|
|
216
|
+
flush();
|
|
217
|
+
block = [line];
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
if (block.length > 0) {
|
|
221
|
+
block.push(line);
|
|
222
|
+
if (blockName === null) {
|
|
223
|
+
const match = /^\s{2}name:\s*['"]?([^'"\s]+)['"]?/u.exec(line);
|
|
224
|
+
if (match !== null) blockName = match[1];
|
|
225
|
+
}
|
|
226
|
+
} else {
|
|
227
|
+
output.push(line);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
flush();
|
|
231
|
+
return { text: output.join('\n'), dropped };
|
|
232
|
+
}
|
|
233
|
+
|
|
145
234
|
/** A filesystem-safe stem for a plugin tarball name. */
|
|
146
235
|
export function packageFileStem(name) {
|
|
147
236
|
return String(name).replace(/^@/u, '').replace(/[\\/]/gu, '-').replace(/[^a-z0-9._-]/giu, '_');
|
|
@@ -221,9 +310,15 @@ export function resolvePnpm(execPath = process.execPath) {
|
|
|
221
310
|
/**
|
|
222
311
|
* Install third-party plugins. pnpm is preferred; npm is a fallback that uses
|
|
223
312
|
* `--no-save` so the archived package.json is not rewritten by an installer.
|
|
313
|
+
*
|
|
314
|
+
* One package failing must not abort the whole restore: every package is
|
|
315
|
+
* attempted, failures are collected for the yellow UI warning, and the caller
|
|
316
|
+
* continues with the file/config restore.
|
|
317
|
+
*
|
|
224
318
|
* @param {{
|
|
225
319
|
* profileDir: string, plugins: Array<{name: string, version?: string|null}>,
|
|
226
|
-
* packages?: Record<string,string>,
|
|
320
|
+
* packages?: Record<string,string>, dshVersion?: string|null, manifests?: Record<string, object>,
|
|
321
|
+
* checkCompatibility?: boolean, runner?: typeof runProcess,
|
|
227
322
|
* timeout_ms?: number, pnpmFile?: string|null, onProgress?: (event: object) => void,
|
|
228
323
|
* }} input
|
|
229
324
|
*/
|
|
@@ -233,13 +328,15 @@ export async function reinstallPlugins(input) {
|
|
|
233
328
|
const installed = [];
|
|
234
329
|
const failed = [];
|
|
235
330
|
const skipped = [];
|
|
331
|
+
const warnings = [];
|
|
236
332
|
const plugins = Array.isArray(input.plugins) ? input.plugins : [];
|
|
237
|
-
if (plugins.length === 0) return { ok: true, manager: 'none', installed, failed, skipped };
|
|
333
|
+
if (plugins.length === 0) return { ok: true, complete: true, manager: 'none', runtime_version: null, installed, failed, skipped, warnings };
|
|
238
334
|
if (!fs.existsSync(input.profileDir)) {
|
|
239
|
-
return { ok: false, reason_code: 'RESTORE_PROFILE_NOT_FOUND', detail: input.profileDir, manager: 'none', installed, failed, skipped };
|
|
335
|
+
return { ok: false, reason_code: 'RESTORE_PROFILE_NOT_FOUND', detail: input.profileDir, manager: 'none', installed, failed, skipped, warnings };
|
|
240
336
|
}
|
|
241
337
|
const pnpmFile = input.pnpmFile === undefined ? resolvePnpm() : input.pnpmFile;
|
|
242
338
|
const manager = pnpmFile !== null && pnpmFile !== undefined ? 'pnpm' : 'npm';
|
|
339
|
+
const runtimeVersion = input.dshVersion ?? resolveDshRuntimeVersion({ profileDir: input.profileDir });
|
|
243
340
|
for (const plugin of plugins) {
|
|
244
341
|
const name = plugin?.name;
|
|
245
342
|
if (!isSafePackageName(name)) {
|
|
@@ -252,16 +349,61 @@ export async function reinstallPlugins(input) {
|
|
|
252
349
|
}
|
|
253
350
|
const tarball = packages[name];
|
|
254
351
|
const spec = typeof tarball === 'string' && tarball !== '' ? `file:${tarball}` : `${name}@latest`;
|
|
352
|
+
|
|
353
|
+
if (input.checkCompatibility !== false) {
|
|
354
|
+
let manifest = input.manifests?.[name];
|
|
355
|
+
if (manifest === undefined && typeof tarball === 'string' && tarball !== '') {
|
|
356
|
+
try {
|
|
357
|
+
manifest = readPackageManifestFromTarball(tarball);
|
|
358
|
+
} catch (error) {
|
|
359
|
+
warnings.push(`${name}:无法读取离线安装包的版本要求(${error instanceof Error ? error.message : String(error)}),继续安装。`);
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
if (manifest === undefined && (typeof tarball !== 'string' || tarball === '')) {
|
|
363
|
+
const viewed = await readRegistryManifest(runner, input.profileDir, name, input.timeout_ms);
|
|
364
|
+
if (viewed.ok) manifest = viewed.manifest;
|
|
365
|
+
else warnings.push(`${name}:无法读取 npm 版本要求(${viewed.detail}),继续安装。`);
|
|
366
|
+
}
|
|
367
|
+
if (manifest !== undefined && manifest !== null) {
|
|
368
|
+
if (runtimeVersion === null) {
|
|
369
|
+
warnings.push(`${name}:无法确认当前 DSH 客户端版本,未做兼容性检查。`);
|
|
370
|
+
} else {
|
|
371
|
+
const unmet = unmetDshRequirements(manifest, runtimeVersion);
|
|
372
|
+
if (Object.keys(unmet).length > 0) {
|
|
373
|
+
const requirement = Object.entries(unmet).map(([field, range]) => `${field} ${range}`).join('、');
|
|
374
|
+
const detail = `要求 ${requirement},当前 DSH ${runtimeVersion}`;
|
|
375
|
+
skipped.push({
|
|
376
|
+
name,
|
|
377
|
+
spec,
|
|
378
|
+
reason_code: 'PLUGIN_DSH_INCOMPATIBLE',
|
|
379
|
+
reason: detail,
|
|
380
|
+
manifest_version: typeof manifest.version === 'string' ? manifest.version : null,
|
|
381
|
+
requirements: unmet,
|
|
382
|
+
runtime_version: runtimeVersion,
|
|
383
|
+
});
|
|
384
|
+
warnings.push(`${name}:与当前 DSH 不兼容(${detail}),已跳过安装。`);
|
|
385
|
+
continue;
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
|
|
255
391
|
const args = manager === 'pnpm'
|
|
256
392
|
? ['--dir', input.profileDir, 'add', spec, '--reporter=append-only', '--prefer-online']
|
|
257
393
|
: ['install', '--no-save', '--package-lock=false', '--omit=dev', '--prefer-online', spec];
|
|
258
394
|
const command = manager === 'pnpm' ? process.execPath : npmCommand();
|
|
259
395
|
const argv = manager === 'pnpm' ? [pnpmFile, ...args] : args;
|
|
260
396
|
input.onProgress?.({ phase: 'install', name, spec, manager });
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
397
|
+
let result;
|
|
398
|
+
try {
|
|
399
|
+
result = await runner(command, argv, {
|
|
400
|
+
cwd: input.profileDir,
|
|
401
|
+
timeout_ms: input.timeout_ms ?? 240_000,
|
|
402
|
+
});
|
|
403
|
+
} catch (error) {
|
|
404
|
+
result = { code: null, stdout: '', stderr: '', error: error instanceof Error ? error.message : String(error) };
|
|
405
|
+
}
|
|
406
|
+
result = result ?? { code: null, stdout: '', stderr: '' };
|
|
265
407
|
const row = {
|
|
266
408
|
name,
|
|
267
409
|
spec,
|
|
@@ -275,15 +417,7 @@ export async function reinstallPlugins(input) {
|
|
|
275
417
|
installed.push(row);
|
|
276
418
|
} else {
|
|
277
419
|
failed.push({ ...row, error: result.error ?? null });
|
|
278
|
-
|
|
279
|
-
ok: false,
|
|
280
|
-
reason_code: 'RESTORE_PLUGIN_INSTALL_FAILED',
|
|
281
|
-
detail: `${name}: ${manager} exited ${String(result.code)}${result.timed_out === true ? ' (timeout)' : ''}`,
|
|
282
|
-
manager,
|
|
283
|
-
installed,
|
|
284
|
-
failed,
|
|
285
|
-
skipped,
|
|
286
|
-
};
|
|
420
|
+
warnings.push(`${name}:安装失败,请自行手动安装!`);
|
|
287
421
|
}
|
|
288
422
|
}
|
|
289
423
|
if (manager === 'npm') {
|
|
@@ -303,7 +437,30 @@ export async function reinstallPlugins(input) {
|
|
|
303
437
|
fs.writeFileSync(pkgFile, `${JSON.stringify(pkg, null, 2)}\n`, 'utf8');
|
|
304
438
|
} catch { /* a package.json write failure is reported by the next status, not here */ }
|
|
305
439
|
}
|
|
306
|
-
return { ok: true, manager, installed, failed, skipped };
|
|
440
|
+
return { ok: true, complete: failed.length === 0, manager, runtime_version: runtimeVersion, installed, failed, skipped, warnings };
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
async function readRegistryManifest(runner, cwd, name, timeoutMs) {
|
|
444
|
+
let result;
|
|
445
|
+
try {
|
|
446
|
+
result = await runner(npmCommand(), ['view', `${name}@latest`, 'version', 'engines', 'peerDependencies', '--json'], {
|
|
447
|
+
cwd,
|
|
448
|
+
timeout_ms: Math.min(timeoutMs ?? 60_000, 60_000),
|
|
449
|
+
});
|
|
450
|
+
} catch (error) {
|
|
451
|
+
return { ok: false, detail: `npm view failed: ${error instanceof Error ? error.message : String(error)}` };
|
|
452
|
+
}
|
|
453
|
+
if (result?.code !== 0) {
|
|
454
|
+
return { ok: false, detail: `npm view exited ${String(result?.code)}${result?.timed_out === true ? ' (timeout)' : ''}` };
|
|
455
|
+
}
|
|
456
|
+
try {
|
|
457
|
+
const parsed = JSON.parse(String(result.stdout ?? '').trim());
|
|
458
|
+
const manifest = Array.isArray(parsed) ? parsed[0] : parsed;
|
|
459
|
+
if (manifest === null || typeof manifest !== 'object') return { ok: false, detail: 'npm view returned no manifest' };
|
|
460
|
+
return { ok: true, manifest };
|
|
461
|
+
} catch {
|
|
462
|
+
return { ok: false, detail: 'npm view returned invalid JSON' };
|
|
463
|
+
}
|
|
307
464
|
}
|
|
308
465
|
|
|
309
466
|
/**
|
package/src/point.mjs
CHANGED
|
@@ -29,7 +29,7 @@ import { scanExclusions, scanHome } from './scan.mjs';
|
|
|
29
29
|
import { packPluginTarballs, readProfilePlugins, SELF_PLUGIN } from './plugins.mjs';
|
|
30
30
|
import { buildPointTree } from './tree.mjs';
|
|
31
31
|
import { atomicWriteFile, ensureDir, formatBytes, isSafeArchiveName, readJson, sha256, stamp } from './util/fs.mjs';
|
|
32
|
-
import { encodeSegment, profileDir, projectKey, resolveDshHome } from './util/paths.mjs';
|
|
32
|
+
import { encodeSegment, missingDriveForPath, profileDir, projectKey, resolveDshHome } from './util/paths.mjs';
|
|
33
33
|
|
|
34
34
|
export const POINT_SCHEMA = 'twinsearth-timemachine-point/1';
|
|
35
35
|
export const MANIFEST_SCHEMA = 'twinsearth-timemachine-manifest/1';
|
|
@@ -633,6 +633,21 @@ export function importRestorePoint(input) {
|
|
|
633
633
|
if (source.startsWith(`${path.resolve(root)}${path.sep}`)) {
|
|
634
634
|
return { ok: false, reason_code: CODES.IMPORT_FAILED, detail: 'the source file is already inside the restore-point root' };
|
|
635
635
|
}
|
|
636
|
+
const pathIssues = [];
|
|
637
|
+
const seenDrives = new Set();
|
|
638
|
+
for (const workspace of row.point?.workspaces ?? []) {
|
|
639
|
+
const workspacePath = typeof workspace?.path === 'string' ? workspace.path : '';
|
|
640
|
+
const drive = missingDriveForPath(workspacePath);
|
|
641
|
+
if (drive === null || seenDrives.has(`${drive}|${workspacePath}`)) continue;
|
|
642
|
+
seenDrives.add(`${drive}|${workspacePath}`);
|
|
643
|
+
pathIssues.push({
|
|
644
|
+
code: 'TIMEMACHINE_PATH_DRIVE_MISSING',
|
|
645
|
+
drive,
|
|
646
|
+
source_path: workspacePath,
|
|
647
|
+
target_path: workspacePath,
|
|
648
|
+
message: `本机没有 ${drive} 盘,还原点中的工作区“${workspace.title ?? workspacePath}”无法直接落到 ${workspacePath};请修改工作区文件夹。`,
|
|
649
|
+
});
|
|
650
|
+
}
|
|
636
651
|
const writable = ensureWritableRoot(root);
|
|
637
652
|
if (writable !== null) return { ok: false, reason_code: CODES.ROOT_UNWRITABLE, detail: writable };
|
|
638
653
|
const folder = categoryDir(root, 'imported');
|
|
@@ -648,7 +663,16 @@ export function importRestorePoint(input) {
|
|
|
648
663
|
} catch (error) {
|
|
649
664
|
return { ok: false, reason_code: CODES.IMPORT_FAILED, detail: error instanceof Error ? error.message : String(error) };
|
|
650
665
|
}
|
|
651
|
-
return {
|
|
666
|
+
return {
|
|
667
|
+
ok: true,
|
|
668
|
+
file: target,
|
|
669
|
+
source,
|
|
670
|
+
point: row.point,
|
|
671
|
+
bytes: row.bytes,
|
|
672
|
+
sha256: row.sha256,
|
|
673
|
+
path_issues: pathIssues,
|
|
674
|
+
warnings: pathIssues.map((issue) => issue.message),
|
|
675
|
+
};
|
|
652
676
|
}
|
|
653
677
|
|
|
654
678
|
/** Estimate what a point will contain, without writing. */
|