@sparkelf/dsh-plus 0.2.9-alpha.105 → 0.2.9-alpha.107
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/cordis.patch.yml +0 -15
- package/lib/apply-16__lfdL.js +1281 -0
- package/lib/apply-B1AtqWbU.js +1324 -0
- package/lib/apply-BVHHAP0a.js +1283 -0
- package/lib/apply-D7zOZgHa.js +1322 -0
- package/lib/apply.js +2 -2
- package/lib/bin.js +1 -1
- package/lib/types/apply.d.ts +74 -1
- package/lib/types/apply.js +201 -29
- package/package.json +480 -481
|
@@ -0,0 +1,1283 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
import { spawnSync } from "node:child_process";
|
|
3
|
+
import { chmodSync, copyFileSync, existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, renameSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
|
|
4
|
+
import { createHash } from "node:crypto";
|
|
5
|
+
import { dirname, isAbsolute, join, relative, resolve } from "node:path";
|
|
6
|
+
import { satisfies, valid, validRange } from "semver";
|
|
7
|
+
import { tmpdir } from "node:os";
|
|
8
|
+
import { parseDocument } from "yaml";
|
|
9
|
+
//#region lib/types/patch-set.js
|
|
10
|
+
/**
|
|
11
|
+
* Read the Plus distribution's embedded patch set.
|
|
12
|
+
*
|
|
13
|
+
* The distribution owns one patch set for official DSH and, separately, the patches it
|
|
14
|
+
* carries for third-party packages. Both live inside the distribution package: the
|
|
15
|
+
* manifest declares entries, and each entry's payload is a file under `patches/`. A
|
|
16
|
+
* consumer resolves no independently published patch package.
|
|
17
|
+
*
|
|
18
|
+
* The two lists are governed differently. A Plus entry must target an official package
|
|
19
|
+
* that the profile republishes and overrides; a third-party entry must name its upstream
|
|
20
|
+
* and a retirement condition, and never becomes part of the official patch set.
|
|
21
|
+
*/
|
|
22
|
+
function object(value, label) {
|
|
23
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) throw new Error(label + " must be an object");
|
|
24
|
+
return value;
|
|
25
|
+
}
|
|
26
|
+
function string(value, label) {
|
|
27
|
+
if (typeof value !== "string" || value === "") throw new Error(label + " must be a non-empty string");
|
|
28
|
+
return value;
|
|
29
|
+
}
|
|
30
|
+
function range(value, label) {
|
|
31
|
+
const text = string(value, label);
|
|
32
|
+
if (validRange(text) === null) throw new Error(label + " is not a semantic-version range: " + text);
|
|
33
|
+
return text;
|
|
34
|
+
}
|
|
35
|
+
/** A payload stays inside the distribution package it is declared by. */
|
|
36
|
+
function patchFile(distributionDirectory, declared, label) {
|
|
37
|
+
const absolute = resolve(distributionDirectory, declared);
|
|
38
|
+
const local = relative(distributionDirectory, absolute);
|
|
39
|
+
if (local === "" || isAbsolute(local) || local === ".." || local.startsWith("../")) throw new Error(label + " must stay inside the distribution package: " + declared);
|
|
40
|
+
if (!existsSync(absolute)) throw new Error(label + " does not exist: " + absolute);
|
|
41
|
+
return local.replaceAll("\\", "/");
|
|
42
|
+
}
|
|
43
|
+
function sourceTarget(value, sourceBaseRevision, label) {
|
|
44
|
+
const target = object(value, label);
|
|
45
|
+
if (target.kind !== "dsh-source") throw new Error(label + ".kind must be dsh-source");
|
|
46
|
+
if (!Array.isArray(target.paths) || target.paths.length === 0) throw new Error(label + ".paths must be a non-empty array");
|
|
47
|
+
const paths = target.paths.map((entry, index) => {
|
|
48
|
+
const path = string(entry, label + ".paths[" + String(index) + "]");
|
|
49
|
+
if (!path.endsWith("/")) throw new Error(label + ".paths entries must be repository directory prefixes");
|
|
50
|
+
return path;
|
|
51
|
+
});
|
|
52
|
+
if (!Array.isArray(target.packages) || target.packages.length === 0) throw new Error(label + ".packages must be a non-empty array");
|
|
53
|
+
return {
|
|
54
|
+
kind: "dsh-source",
|
|
55
|
+
baseRevision: sourceBaseRevision,
|
|
56
|
+
packages: target.packages.map((entry, index) => {
|
|
57
|
+
const name = string(entry, label + ".packages[" + String(index) + "]");
|
|
58
|
+
if (!name.startsWith("@deepseek-ai/")) throw new Error(label + ".packages entries must be official package names");
|
|
59
|
+
return name;
|
|
60
|
+
}),
|
|
61
|
+
paths
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
function npmTarget(value, label) {
|
|
65
|
+
const target = object(value, label);
|
|
66
|
+
if (target.kind !== "npm") throw new Error(label + ".kind must be npm");
|
|
67
|
+
return {
|
|
68
|
+
kind: "npm",
|
|
69
|
+
name: string(target.name, label + ".name"),
|
|
70
|
+
range: range(target.range, label + ".range")
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
function entry(value, scope, sourceBaseRevision, distributionDirectory, label) {
|
|
74
|
+
const record = object(value, label);
|
|
75
|
+
const id = string(record.id, label + ".id");
|
|
76
|
+
const dsh = range(record.dsh, label + ".dsh");
|
|
77
|
+
const file = patchFile(distributionDirectory, string(record.file, label + ".file"), label + ".file");
|
|
78
|
+
const retireWhen = string(record.retireWhen, label + ".retireWhen");
|
|
79
|
+
const targetLabel = label + ".target";
|
|
80
|
+
const target = scope === "plus" ? object(record.target, targetLabel).kind === "dsh-source" ? sourceTarget(record.target, sourceBaseRevision, targetLabel) : npmTarget(record.target, targetLabel) : npmTarget(record.target, targetLabel);
|
|
81
|
+
if (scope === "third-party") return {
|
|
82
|
+
id,
|
|
83
|
+
scope,
|
|
84
|
+
dsh,
|
|
85
|
+
file,
|
|
86
|
+
retireWhen,
|
|
87
|
+
upstreamUrl: string(record.upstreamUrl, label + ".upstreamUrl"),
|
|
88
|
+
target
|
|
89
|
+
};
|
|
90
|
+
return {
|
|
91
|
+
id,
|
|
92
|
+
scope,
|
|
93
|
+
dsh,
|
|
94
|
+
file,
|
|
95
|
+
retireWhen,
|
|
96
|
+
target
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Read and validate the embedded patch set from an installed distribution.
|
|
101
|
+
* @param distributionDirectory - installed `@sparkelf/dsh-plus` directory.
|
|
102
|
+
* @param manifest - its parsed package manifest.
|
|
103
|
+
* @returns the ordered Plus entries, third-party entries, and the exact source base.
|
|
104
|
+
*/
|
|
105
|
+
function readPatchSet(distributionDirectory, manifest) {
|
|
106
|
+
const plus = object(manifest.dshPlus, "dshPlus");
|
|
107
|
+
const sourceBaseRevision = string(object(plus.sourceBase, "dshPlus.sourceBase").revision, "dshPlus.sourceBase.revision");
|
|
108
|
+
const declaration = object(plus.patchSet, "dshPlus.patchSet");
|
|
109
|
+
if (declaration.formatVersion !== 1) throw new Error("dshPlus.patchSet.formatVersion must be 1");
|
|
110
|
+
if (!Array.isArray(declaration.entries) || declaration.entries.length === 0) throw new Error("dshPlus.patchSet.entries must be a non-empty array");
|
|
111
|
+
const entries = declaration.entries.map((value, index) => entry(value, "plus", sourceBaseRevision, distributionDirectory, "dshPlus.patchSet.entries[" + String(index) + "]"));
|
|
112
|
+
const thirdPartyRaw = plus.thirdPartyPatches;
|
|
113
|
+
const thirdParty = thirdPartyRaw === void 0 ? [] : Array.isArray(thirdPartyRaw) ? thirdPartyRaw.map((value, index) => entry(value, "third-party", sourceBaseRevision, distributionDirectory, "dshPlus.thirdPartyPatches[" + String(index) + "]")) : (() => {
|
|
114
|
+
throw new Error("dshPlus.thirdPartyPatches must be an array");
|
|
115
|
+
})();
|
|
116
|
+
const ids = /* @__PURE__ */ new Set();
|
|
117
|
+
for (const item of [...entries, ...thirdParty]) {
|
|
118
|
+
if (ids.has(item.id)) throw new Error("duplicate patch entry id: " + item.id);
|
|
119
|
+
ids.add(item.id);
|
|
120
|
+
}
|
|
121
|
+
return {
|
|
122
|
+
entries,
|
|
123
|
+
thirdPartyPatches: thirdParty,
|
|
124
|
+
sourceBaseRevision
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
//#endregion
|
|
128
|
+
//#region lib/types/manifest.js
|
|
129
|
+
/**
|
|
130
|
+
* Derive the profile's override table from the distribution's own facts.
|
|
131
|
+
*
|
|
132
|
+
* The distribution records one truth per fact: the patch set names the official packages it
|
|
133
|
+
* patches, the runtime names the version its republished builds carry, and an edition names the
|
|
134
|
+
* capabilities a deployment omits. The override table npm and pnpm read is a projection of those
|
|
135
|
+
* three, never a fourth list: an entry that cannot be derived fails the build instead of being
|
|
136
|
+
* carried forward, which is what kept a stale `@deepseek-ai/dsh-client-ui-slots` pin alive long
|
|
137
|
+
* after the patch behind it had gone.
|
|
138
|
+
*
|
|
139
|
+
* @module manifest
|
|
140
|
+
*/
|
|
141
|
+
/** npm scope every patched official package is republished under. */
|
|
142
|
+
const OFFICIAL_PREFIX = "@deepseek-ai/";
|
|
143
|
+
/** npm scope the distribution publishes its republished builds under. */
|
|
144
|
+
const REPUBLISHED_PREFIX = "@sparkelf/";
|
|
145
|
+
/** Independent profile metadata the app-boot preflight reads before importing plugins. */
|
|
146
|
+
const PROFILE_COMPATIBILITY_FILENAME = "compatibility.json";
|
|
147
|
+
function record(value, label) {
|
|
148
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) throw new Error(label + " must be an object");
|
|
149
|
+
return value;
|
|
150
|
+
}
|
|
151
|
+
/** A declared value as text, or the fallback when the manifest holds something else. */
|
|
152
|
+
function text(value, fallback = "") {
|
|
153
|
+
return typeof value === "string" ? value : fallback;
|
|
154
|
+
}
|
|
155
|
+
function stringArray(value, label) {
|
|
156
|
+
if (!Array.isArray(value) || value.some((entry) => typeof entry !== "string" || entry === "")) throw new Error(label + " must be a non-empty string array");
|
|
157
|
+
return value;
|
|
158
|
+
}
|
|
159
|
+
function stringMap(value, label) {
|
|
160
|
+
const result = {};
|
|
161
|
+
for (const [name, spec] of Object.entries(record(value, label))) {
|
|
162
|
+
if (typeof spec !== "string" || spec === "") throw new Error(label + "." + name + " must be a non-empty string");
|
|
163
|
+
result[name] = spec;
|
|
164
|
+
}
|
|
165
|
+
return result;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* The republished scope name for one official package.
|
|
169
|
+
*
|
|
170
|
+
* @param official - an `@deepseek-ai/dsh-*` name.
|
|
171
|
+
* @returns the `@sparkelf/dsh-*` name the distribution publishes it as.
|
|
172
|
+
*/
|
|
173
|
+
function republishedName(official) {
|
|
174
|
+
if (!official.startsWith(OFFICIAL_PREFIX)) throw new Error(official + " is not an official package name");
|
|
175
|
+
return REPUBLISHED_PREFIX + official.slice(13);
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Derive the redirect table from the patch set.
|
|
179
|
+
*
|
|
180
|
+
* Every `dsh-source` entry republishes the packages its payload modifies, because a registry
|
|
181
|
+
* installation has no source tree to patch. An entry that changes only types sets
|
|
182
|
+
* `target.republish: false` and must say why; anything else would look like a redirect that
|
|
183
|
+
* silently never happens.
|
|
184
|
+
*
|
|
185
|
+
* @param entries - the distribution's patch entries.
|
|
186
|
+
* @param runtimeVersion - exact version the republished builds carry.
|
|
187
|
+
* @returns official name to `npm:` alias spec.
|
|
188
|
+
*/
|
|
189
|
+
function deriveRedirects(entries, runtimeVersion) {
|
|
190
|
+
const redirects = {};
|
|
191
|
+
const ids = {};
|
|
192
|
+
for (const entry of entries) {
|
|
193
|
+
if (entry.target.kind !== "dsh-source") continue;
|
|
194
|
+
const packages = entry.target.packages ?? [];
|
|
195
|
+
if (packages.length === 0) throw new Error("patch entry " + entry.id + " declares no target packages");
|
|
196
|
+
if (!(entry.target.republish !== false)) {
|
|
197
|
+
if (entry.target.reason === void 0 || entry.target.reason.trim() === "") throw new Error("patch entry " + entry.id + " sets republish:false without a reason");
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
for (const official of packages) {
|
|
201
|
+
const spec = "npm:" + republishedName(official) + "@" + runtimeVersion;
|
|
202
|
+
if (redirects[official] !== void 0 && redirects[official] !== spec) throw new Error("patch entries " + (ids[official] ?? "?") + " and " + entry.id + " redirect " + official + " to different targets");
|
|
203
|
+
redirects[official] = spec;
|
|
204
|
+
ids[official] = entry.id;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
if (Object.keys(redirects).length === 0) throw new Error("the patch set declares no republishable target");
|
|
208
|
+
return {
|
|
209
|
+
redirects,
|
|
210
|
+
ids
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
/** Read and validate the facts one installed distribution declares. */
|
|
214
|
+
function readDistributionFacts(directory) {
|
|
215
|
+
const manifest = record(JSON.parse(readFileSync(join(directory, "package.json"), "utf8")), "distribution manifest");
|
|
216
|
+
const plus = record(manifest.dshPlus, "dshPlus");
|
|
217
|
+
const profile = record(plus.profile, "dshPlus.profile");
|
|
218
|
+
if (profile.overrides !== void 0) throw new Error("dshPlus.profile.overrides is retired; overrides are derived from dshPlus.patchSet");
|
|
219
|
+
const runtimeVersion = text(record(plus.runtime, "dshPlus.runtime").version);
|
|
220
|
+
if (valid(runtimeVersion) === null) throw new Error("dshPlus.runtime.version must be an exact version, got: " + runtimeVersion);
|
|
221
|
+
const omission = record(plus.omission, "dshPlus.omission");
|
|
222
|
+
const omissionPackage = text(omission.package);
|
|
223
|
+
const omissionVersion = text(omission.version);
|
|
224
|
+
if (!omissionPackage.startsWith("@sparkelf/") || valid(omissionVersion) === null) throw new Error("dshPlus.omission must name an @sparkelf package and an exact version");
|
|
225
|
+
if (validRange(text(record(plus.compatibility, "dshPlus.compatibility").dsh)) === null) throw new Error("dshPlus.compatibility.dsh must be a semver range");
|
|
226
|
+
const rawEntries = record(plus.patchSet, "dshPlus.patchSet").entries;
|
|
227
|
+
if (!Array.isArray(rawEntries) || rawEntries.length === 0) throw new Error("dshPlus.patchSet.entries must be a non-empty array");
|
|
228
|
+
const { redirects, ids } = deriveRedirects(rawEntries.map((value, index) => {
|
|
229
|
+
const entry = record(value, "patchSet.entries[" + String(index) + "]");
|
|
230
|
+
const target = record(entry.target, "patchSet.entries[" + String(index) + "].target");
|
|
231
|
+
const targetFacts = { kind: text(target.kind) };
|
|
232
|
+
if (target.packages !== void 0) Object.assign(targetFacts, { packages: stringArray(target.packages, "target.packages") });
|
|
233
|
+
if (target.republish === false) Object.assign(targetFacts, { republish: false });
|
|
234
|
+
if (typeof target.reason === "string") Object.assign(targetFacts, { reason: target.reason });
|
|
235
|
+
return {
|
|
236
|
+
id: text(entry.id),
|
|
237
|
+
target: targetFacts
|
|
238
|
+
};
|
|
239
|
+
}), runtimeVersion);
|
|
240
|
+
const dependencies = stringMap(profile.dependencies, "dshPlus.profile.dependencies");
|
|
241
|
+
const compatibilityExemptions = profile.compatibility === void 0 ? [] : stringArray(profile.compatibility, "dshPlus.profile.compatibility");
|
|
242
|
+
for (const entry of compatibilityExemptions) {
|
|
243
|
+
const separator = entry.lastIndexOf("@");
|
|
244
|
+
const name = entry.slice(0, separator);
|
|
245
|
+
const version = entry.slice(separator + 1);
|
|
246
|
+
if (separator <= 0 || valid(version) === null) throw new Error("dshPlus.profile.compatibility entry must be an exact package@version, got: " + entry);
|
|
247
|
+
if (dependencies[name] !== version) throw new Error("dshPlus.profile.compatibility names " + entry + ", which dshPlus.profile.dependencies does not pin");
|
|
248
|
+
}
|
|
249
|
+
const allowBuilds = {};
|
|
250
|
+
for (const [name, allowed] of Object.entries(record(profile.allowBuilds, "dshPlus.profile.allowBuilds"))) {
|
|
251
|
+
if (typeof allowed !== "boolean") throw new Error("dshPlus.profile.allowBuilds." + name + " must be a boolean");
|
|
252
|
+
allowBuilds[name] = allowed;
|
|
253
|
+
}
|
|
254
|
+
const editions = {};
|
|
255
|
+
const rawEditions = plus.editions === void 0 ? {} : record(plus.editions, "dshPlus.editions");
|
|
256
|
+
for (const [id, value] of Object.entries(rawEditions)) {
|
|
257
|
+
const edition = record(value, "dshPlus.editions." + id);
|
|
258
|
+
const rawPackages = edition.packages;
|
|
259
|
+
if (rawPackages === void 0) throw new Error("dshPlus.editions." + id + ".packages is required");
|
|
260
|
+
const names = Array.isArray(rawPackages) ? stringArray(rawPackages, "dshPlus.editions." + id + ".packages") : Object.keys(record(rawPackages, "dshPlus.editions." + id + ".packages"));
|
|
261
|
+
for (const name of names) if (dependencyKey(dependencies, name) === void 0) throw new Error("edition \"" + id + "\" installs " + name + ", which dshPlus.profile.dependencies does not pin");
|
|
262
|
+
const omit = stringArray(edition.omit ?? [], "dshPlus.editions." + id + ".omit");
|
|
263
|
+
for (const name of omit) if (dependencies[name] === void 0 && manifest.dependencies?.[name] === void 0) throw new Error("edition \"" + id + "\" omits " + name + ", which the distribution does not declare");
|
|
264
|
+
const excludePackages = stringArray(edition.excludePackages ?? [], "dshPlus.editions." + id + ".excludePackages");
|
|
265
|
+
for (const name of excludePackages) {
|
|
266
|
+
if (dependencies[name] === void 0 && !stringArray(edition.bundles ?? [], "x").includes(name)) throw new Error("edition \"" + id + "\" excludes " + name + ", which it does not install or mount");
|
|
267
|
+
if (omit.includes(name)) throw new Error("edition \"" + id + "\" both excludes and omits " + name);
|
|
268
|
+
for (const [other, spec] of Object.entries(dependencies)) if (/^npm:/u.test(spec) && spec.includes(name + "@")) throw new Error("edition \"" + id + "\" excludes " + name + " but " + other + " aliases it");
|
|
269
|
+
}
|
|
270
|
+
const bundles = stringArray(edition.bundles ?? [], "dshPlus.editions." + id + ".bundles");
|
|
271
|
+
for (const name of bundles) if (omit.includes(name)) throw new Error("edition \"" + id + "\" mounts " + name + " but also omits it");
|
|
272
|
+
editions[id] = {
|
|
273
|
+
label: text(edition.label, id),
|
|
274
|
+
profile: text(edition.profile, "plus"),
|
|
275
|
+
bundles,
|
|
276
|
+
packages: names,
|
|
277
|
+
omit,
|
|
278
|
+
excludePackages
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
return {
|
|
282
|
+
name: String(manifest.name),
|
|
283
|
+
version: String(manifest.version),
|
|
284
|
+
runtimeVersion,
|
|
285
|
+
omissionSpec: "npm:" + omissionPackage + "@" + omissionVersion,
|
|
286
|
+
bundles: stringArray(profile.bundles, "dshPlus.profile.bundles"),
|
|
287
|
+
dependencies,
|
|
288
|
+
compatibilityExemptions,
|
|
289
|
+
allowBuilds,
|
|
290
|
+
redirects,
|
|
291
|
+
redirectIds: ids,
|
|
292
|
+
editions
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* The dependency-table key that installs one package.
|
|
297
|
+
*
|
|
298
|
+
* A bundle published under a scope we own is installed through an alias: the official name a
|
|
299
|
+
* consumer imports (`dsh-better-sidebar`) is the key, and the spec names our build
|
|
300
|
+
* (`npm:@sparkelf/dsh-better-sidebar@…`). Editions name the real package, so the
|
|
301
|
+
* lookup has to follow the alias or it reports a reviewed package as unpinned.
|
|
302
|
+
*
|
|
303
|
+
* @param dependencies - the distribution's dependency table.
|
|
304
|
+
* @param name - package name an edition names.
|
|
305
|
+
* @returns the key that installs it, or undefined when the distribution does not carry it.
|
|
306
|
+
*/
|
|
307
|
+
function dependencyKey(dependencies, name) {
|
|
308
|
+
if (dependencies[name] !== void 0) return name;
|
|
309
|
+
for (const [key, spec] of Object.entries(dependencies)) if (/^npm:(@[^/]+\/[^@]+)@/u.exec(spec)?.[1] === name) return key;
|
|
310
|
+
}
|
|
311
|
+
/**
|
|
312
|
+
* Project an edition into the profile npm and pnpm install.
|
|
313
|
+
*
|
|
314
|
+
* The distribution's reviewed order is the base; the edition names the bundle order and package
|
|
315
|
+
* set it installs and the capabilities it must not run. Versions always come from
|
|
316
|
+
* `profile.dependencies`, so an edition cannot pin a version the distribution did not review.
|
|
317
|
+
* No edition means the full distribution, which is what a launcher start without a selection
|
|
318
|
+
* materializes.
|
|
319
|
+
*
|
|
320
|
+
* @param facts - validated distribution facts.
|
|
321
|
+
* @param editionId - edition to project, or undefined for the full distribution.
|
|
322
|
+
* @returns the profile's dependencies, bundles, and override table.
|
|
323
|
+
*/
|
|
324
|
+
function projectEdition(facts, editionId, extraExcludes = []) {
|
|
325
|
+
const edition = editionId === void 0 ? void 0 : facts.editions[editionId];
|
|
326
|
+
if (editionId !== void 0 && edition === void 0) throw new Error("unknown edition \"" + editionId + "\"; declared: " + (Object.keys(facts.editions).join(", ") || "(none)"));
|
|
327
|
+
const excludedNames = new Set([...edition?.excludePackages ?? [], ...extraExcludes]);
|
|
328
|
+
for (const name of excludedNames) if (!(facts.dependencies[name] !== void 0 || facts.bundles.includes(name))) throw new Error("exclude names " + name + ", which the distribution does not install or mount");
|
|
329
|
+
const omittedNames = new Set(edition?.omit ?? []);
|
|
330
|
+
const omitted = {};
|
|
331
|
+
for (const name of omittedNames) omitted[name] = facts.omissionSpec;
|
|
332
|
+
const dependencies = {};
|
|
333
|
+
const sharedOfficials = Object.keys(facts.dependencies).filter((name) => name.startsWith("@deepseek-ai/dsh"));
|
|
334
|
+
const packageNames = edition === void 0 ? [...new Set([...Object.keys(facts.dependencies), ...sharedOfficials])] : [...new Set([...edition.packages, ...sharedOfficials])];
|
|
335
|
+
for (const name of packageNames) {
|
|
336
|
+
if (omittedNames.has(name) || excludedNames.has(name)) continue;
|
|
337
|
+
const key = dependencyKey(facts.dependencies, name) ?? name;
|
|
338
|
+
const spec = facts.dependencies[key];
|
|
339
|
+
if (spec === void 0) throw new Error(name + " is not a reviewed package");
|
|
340
|
+
dependencies[key] = spec;
|
|
341
|
+
}
|
|
342
|
+
for (const [name, spec] of Object.entries(facts.redirects)) {
|
|
343
|
+
if (omittedNames.has(name) || excludedNames.has(name)) continue;
|
|
344
|
+
dependencies[name] = spec;
|
|
345
|
+
}
|
|
346
|
+
const bundles = (edition === void 0 ? [...facts.bundles] : [...new Set(edition.bundles)]).filter((name) => !excludedNames.has(name));
|
|
347
|
+
for (const name of bundles) if (!facts.bundles.includes(name) && !(edition?.bundles ?? []).includes(name)) throw new Error("edition \"" + String(editionId) + "\" mounts bundle " + name + ", which the distribution does not declare");
|
|
348
|
+
const overrides = {};
|
|
349
|
+
for (const [name, spec] of Object.entries(facts.redirects)) {
|
|
350
|
+
if (omittedNames.has(name) || excludedNames.has(name)) continue;
|
|
351
|
+
overrides[name] = spec;
|
|
352
|
+
}
|
|
353
|
+
overrides["@deepseek-ai/dsh"] = facts.runtimeVersion;
|
|
354
|
+
Object.assign(overrides, omitted);
|
|
355
|
+
return {
|
|
356
|
+
profileName: edition?.profile ?? "plus",
|
|
357
|
+
dependencies,
|
|
358
|
+
bundles,
|
|
359
|
+
overrides,
|
|
360
|
+
allowBuilds: facts.allowBuilds,
|
|
361
|
+
omittedPackages: omitted
|
|
362
|
+
};
|
|
363
|
+
}
|
|
364
|
+
/**
|
|
365
|
+
* Assert every redirect target is served by a registry.
|
|
366
|
+
*
|
|
367
|
+
* A redirect names a published build; one that names a version the registry does not serve
|
|
368
|
+
* installs nothing at all, and the failure surfaces as a resolver error that reads like a
|
|
369
|
+
* packaging defect rather than a release that never happened.
|
|
370
|
+
*
|
|
371
|
+
* @param redirects - official name to `npm:` alias spec.
|
|
372
|
+
* @param registry - npm registry base URL.
|
|
373
|
+
* @returns nothing; throws on the first unavailable target.
|
|
374
|
+
*/
|
|
375
|
+
async function assertRedirectsPublished(redirects, registry = "https://registry.npmjs.org") {
|
|
376
|
+
for (const spec of Object.values(redirects)) {
|
|
377
|
+
const match = /^npm:(@[^/]+\/[^@]+)@(.+)$/u.exec(spec);
|
|
378
|
+
if (match === null) throw new Error("redirect " + spec + " is not an npm alias");
|
|
379
|
+
const [, name, version] = match;
|
|
380
|
+
if (name === void 0 || version === void 0) throw new Error("redirect " + spec + " is not an npm alias");
|
|
381
|
+
if (!(await fetch(registry.replace(/\/$/u, "") + "/" + name.replace("/", "%2f") + "/" + version)).ok) throw new Error("redirect target " + name + "@" + version + " is not served by " + registry);
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
/**
|
|
385
|
+
* Record the distribution's exact-version grants in the profile's compatibility file.
|
|
386
|
+
*
|
|
387
|
+
* The file is the app-boot preflight's own grant mechanism: a missing or unreadable file
|
|
388
|
+
* authorizes nothing, and every other key belongs to the deployment. Writing merges the
|
|
389
|
+
* distribution's grants into the existing object rather than replacing it, so a grant a
|
|
390
|
+
* user or image added for another plugin survives a `dsh-plus install`/`start`.
|
|
391
|
+
*
|
|
392
|
+
* @param directory - profile directory that receives the file.
|
|
393
|
+
* @param exemptions - exact `package@version` grants the distribution admits.
|
|
394
|
+
* @param runtimeVersion - exact runtime each grant names.
|
|
395
|
+
*/
|
|
396
|
+
function writeProfileCompatibility(directory, exemptions, runtimeVersion) {
|
|
397
|
+
mkdirSync(directory, { recursive: true });
|
|
398
|
+
const path = join(directory, PROFILE_COMPATIBILITY_FILENAME);
|
|
399
|
+
let current = {};
|
|
400
|
+
try {
|
|
401
|
+
const parsed = JSON.parse(readFileSync(path, "utf8"));
|
|
402
|
+
if (parsed !== null && typeof parsed === "object" && !Array.isArray(parsed)) current = parsed;
|
|
403
|
+
} catch {}
|
|
404
|
+
for (const entry of exemptions) {
|
|
405
|
+
const versions = Array.isArray(current[entry]) ? current[entry].filter((version) => typeof version === "string") : [];
|
|
406
|
+
current[entry] = [...new Set([...versions, runtimeVersion])];
|
|
407
|
+
}
|
|
408
|
+
writeFileSync(path, JSON.stringify(current, null, 2) + "\n");
|
|
409
|
+
}
|
|
410
|
+
/**
|
|
411
|
+
* Write the profile manifest and the pnpm workspace the install reads.
|
|
412
|
+
*
|
|
413
|
+
* npm reads `overrides` from the root manifest and pnpm reads them from the workspace file, so
|
|
414
|
+
* both are written from the same projection. Resolver settings a container needs (store location,
|
|
415
|
+
* supported architectures, release-age window) belong to the caller and are appended to the
|
|
416
|
+
* workspace file afterwards.
|
|
417
|
+
*
|
|
418
|
+
* @param directory - profile directory to write into.
|
|
419
|
+
* @param facts - validated distribution facts.
|
|
420
|
+
* @param editionId - edition to project.
|
|
421
|
+
* @param extraExcludes - packages the caller drops on top of the edition's own exclusions.
|
|
422
|
+
* @returns the projection that was written.
|
|
423
|
+
*/
|
|
424
|
+
function writeProfileProjection(directory, facts, editionId, extraExcludes = []) {
|
|
425
|
+
const projection = projectEdition(facts, editionId, extraExcludes);
|
|
426
|
+
mkdirSync(directory, { recursive: true });
|
|
427
|
+
const dependencies = {
|
|
428
|
+
"@sparkelf/dsh-plus": facts.version,
|
|
429
|
+
"@deepseek-ai/dsh": facts.runtimeVersion,
|
|
430
|
+
...projection.dependencies
|
|
431
|
+
};
|
|
432
|
+
writeFileSync(join(directory, "package.json"), JSON.stringify({
|
|
433
|
+
name: "dsh-profile-" + projection.profileName,
|
|
434
|
+
private: true,
|
|
435
|
+
type: "module",
|
|
436
|
+
dependencies,
|
|
437
|
+
dsh: { profile: {
|
|
438
|
+
bundles: projection.bundles,
|
|
439
|
+
patchReload: "live"
|
|
440
|
+
} }
|
|
441
|
+
}, null, 2) + "\n");
|
|
442
|
+
const workspace = [
|
|
443
|
+
"packages:",
|
|
444
|
+
" - .",
|
|
445
|
+
"nodeLinker: hoisted",
|
|
446
|
+
"autoInstallPeers: false",
|
|
447
|
+
"",
|
|
448
|
+
"overrides:",
|
|
449
|
+
...Object.keys(projection.overrides).sort().map((name) => " " + JSON.stringify(name) + ": " + (projection.overrides[name] ?? "")),
|
|
450
|
+
"",
|
|
451
|
+
"allowBuilds:",
|
|
452
|
+
...Object.keys(projection.allowBuilds).sort().map((name) => " " + JSON.stringify(name) + ": " + String(projection.allowBuilds[name])),
|
|
453
|
+
""
|
|
454
|
+
];
|
|
455
|
+
writeFileSync(join(directory, "pnpm-workspace.yaml"), workspace.join("\n"));
|
|
456
|
+
writeProfileCompatibility(directory, facts.compatibilityExemptions, facts.runtimeVersion);
|
|
457
|
+
return projection;
|
|
458
|
+
}
|
|
459
|
+
//#endregion
|
|
460
|
+
//#region lib/types/apply.js
|
|
461
|
+
/** Materialize the Plus distribution's independent patch packages without Desktop. */
|
|
462
|
+
const FORMAT_VERSION = 1;
|
|
463
|
+
/** Marker proving the source tree was built from the currently selected source patch set. */
|
|
464
|
+
const BUILD_MARKER = ".dataops-official-build.json";
|
|
465
|
+
function isSelectedNpmPatch(patch) {
|
|
466
|
+
return patch.target.kind === "npm";
|
|
467
|
+
}
|
|
468
|
+
function readJsonObject(path, label) {
|
|
469
|
+
const parsed = JSON.parse(readFileSync(path, "utf8"));
|
|
470
|
+
if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) throw new Error(label + " must be a JSON object");
|
|
471
|
+
return parsed;
|
|
472
|
+
}
|
|
473
|
+
function requireObject(value, label) {
|
|
474
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) throw new Error(label + " must be an object");
|
|
475
|
+
return value;
|
|
476
|
+
}
|
|
477
|
+
function requireString(value, label) {
|
|
478
|
+
if (typeof value !== "string" || value === "") throw new Error(label + " must be a non-empty string");
|
|
479
|
+
return value;
|
|
480
|
+
}
|
|
481
|
+
function requireRange(value, label) {
|
|
482
|
+
const range = requireString(value, label);
|
|
483
|
+
if (validRange(range) === null) throw new Error(label + " is not a semantic-version range: " + range);
|
|
484
|
+
return range;
|
|
485
|
+
}
|
|
486
|
+
function requireVersion(value, label) {
|
|
487
|
+
const version = requireString(value, label);
|
|
488
|
+
if (valid(version) === null) throw new Error(label + " is not a semantic version: " + version);
|
|
489
|
+
return version;
|
|
490
|
+
}
|
|
491
|
+
function requireStringArray(value, label) {
|
|
492
|
+
if (!Array.isArray(value) || value.length === 0) throw new Error(label + " must be a non-empty array");
|
|
493
|
+
const strings = value.map((entry, index) => requireString(entry, label + "[" + String(index) + "]"));
|
|
494
|
+
if (new Set(strings).size !== strings.length) throw new Error(label + " must not contain duplicates");
|
|
495
|
+
return strings;
|
|
496
|
+
}
|
|
497
|
+
/**
|
|
498
|
+
* Resolve one installed package's manifest directory.
|
|
499
|
+
*
|
|
500
|
+
* `require.resolve(name + '/package.json')` respects the package's `exports`
|
|
501
|
+
* map, so a dependency that publishes only `"."` — `@sparkelf/dsh-quant`
|
|
502
|
+
* 0.91.0-sparkelf.13 among the `personal` edition's packages — fails to
|
|
503
|
+
* resolve even though the package is installed. Reading a manifest is not a
|
|
504
|
+
* public-export lookup, so resolve the package entry and walk up to the
|
|
505
|
+
* manifest that names it.
|
|
506
|
+
* @param distributionDirectory - directory whose resolution anchors the lookup.
|
|
507
|
+
* @param packageName - dependency name to locate.
|
|
508
|
+
* @returns path of the `package.json` that declares `packageName`.
|
|
509
|
+
*/
|
|
510
|
+
function resolvePackageManifest(distributionDirectory, packageName) {
|
|
511
|
+
const requireFromDistribution = createRequire(resolve(distributionDirectory, "package.json"));
|
|
512
|
+
try {
|
|
513
|
+
return requireFromDistribution.resolve(packageName + "/package.json");
|
|
514
|
+
} catch {}
|
|
515
|
+
let directory = dirname(requireFromDistribution.resolve(packageName));
|
|
516
|
+
for (;;) {
|
|
517
|
+
const candidate = resolve(directory, "package.json");
|
|
518
|
+
if (existsSync(candidate)) {
|
|
519
|
+
if (JSON.parse(readFileSync(candidate, "utf8")).name === packageName) return candidate;
|
|
520
|
+
}
|
|
521
|
+
const parent = dirname(directory);
|
|
522
|
+
if (parent === directory) throw new Error(packageName + " is installed but no package.json naming it was found");
|
|
523
|
+
directory = parent;
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
function resolveInstalledPackage(distributionDirectory, packageName) {
|
|
527
|
+
const manifestPath = resolvePackageManifest(distributionDirectory, packageName);
|
|
528
|
+
const manifest = readJsonObject(manifestPath, packageName + " manifest");
|
|
529
|
+
return {
|
|
530
|
+
name: packageName,
|
|
531
|
+
version: requireVersion(manifest.version, packageName + " version"),
|
|
532
|
+
directory: dirname(manifestPath),
|
|
533
|
+
manifest
|
|
534
|
+
};
|
|
535
|
+
}
|
|
536
|
+
function resolvePatchFile(packageDirectory, declared) {
|
|
537
|
+
const absoluteFile = resolve(packageDirectory, declared);
|
|
538
|
+
const local = relative(packageDirectory, absoluteFile);
|
|
539
|
+
if (isAbsolute(local) || local === ".." || local.startsWith("../")) throw new Error("patch file must stay inside its patch package: " + declared);
|
|
540
|
+
if (!existsSync(absoluteFile)) throw new Error("patch file does not exist: " + absoluteFile);
|
|
541
|
+
return {
|
|
542
|
+
relativeFile: local.replaceAll("\\", "/"),
|
|
543
|
+
absoluteFile
|
|
544
|
+
};
|
|
545
|
+
}
|
|
546
|
+
function parseVariant(value, index, patchPackage) {
|
|
547
|
+
const label = patchPackage.name + " dshPatch.variants[" + String(index) + "]";
|
|
548
|
+
const variant = requireObject(value, label);
|
|
549
|
+
const target = requireObject(variant.target, label + ".target");
|
|
550
|
+
const kind = requireString(target.kind, label + ".target.kind");
|
|
551
|
+
const common = {
|
|
552
|
+
id: requireString(variant.id, label + ".id"),
|
|
553
|
+
dsh: requireRange(variant.dsh, label + ".dsh"),
|
|
554
|
+
file: requireString(variant.file, label + ".file"),
|
|
555
|
+
patchPackage
|
|
556
|
+
};
|
|
557
|
+
if (kind === "npm") return {
|
|
558
|
+
...common,
|
|
559
|
+
target: {
|
|
560
|
+
kind,
|
|
561
|
+
name: requireString(target.name, label + ".target.name"),
|
|
562
|
+
range: requireRange(target.range, label + ".target.range")
|
|
563
|
+
}
|
|
564
|
+
};
|
|
565
|
+
if (kind === "dsh-source") return {
|
|
566
|
+
...common,
|
|
567
|
+
target: {
|
|
568
|
+
kind,
|
|
569
|
+
baseRevision: requireString(target.baseRevision, label + ".target.baseRevision")
|
|
570
|
+
}
|
|
571
|
+
};
|
|
572
|
+
throw new Error("unsupported patch target kind: " + kind);
|
|
573
|
+
}
|
|
574
|
+
function git(root, args, acceptFailure = false) {
|
|
575
|
+
const result = spawnSync("git", args, {
|
|
576
|
+
cwd: root,
|
|
577
|
+
encoding: "utf8"
|
|
578
|
+
});
|
|
579
|
+
if (result.status === 0) return result.stdout.trim();
|
|
580
|
+
if (acceptFailure) return void 0;
|
|
581
|
+
const detail = result.stderr.trim();
|
|
582
|
+
throw new Error("git " + args.join(" ") + " failed" + (detail === "" ? "" : ": " + detail));
|
|
583
|
+
}
|
|
584
|
+
function sourcePatchState(root, file) {
|
|
585
|
+
if (git(root, [
|
|
586
|
+
"apply",
|
|
587
|
+
"--check",
|
|
588
|
+
file
|
|
589
|
+
], true) !== void 0) return "pending";
|
|
590
|
+
if (git(root, [
|
|
591
|
+
"apply",
|
|
592
|
+
"--reverse",
|
|
593
|
+
"--check",
|
|
594
|
+
file
|
|
595
|
+
], true) !== void 0) return "applied";
|
|
596
|
+
git(root, [
|
|
597
|
+
"apply",
|
|
598
|
+
"--check",
|
|
599
|
+
file
|
|
600
|
+
]);
|
|
601
|
+
throw new Error("unreachable");
|
|
602
|
+
}
|
|
603
|
+
/** 在隔离index中按真实顺序预演source patches,避免失败后污染official worktree。 */
|
|
604
|
+
function preflightSourcePatches(root, files) {
|
|
605
|
+
if (files.length === 0) return;
|
|
606
|
+
const directory = mkdtempSync(resolve(tmpdir(), "dsh-plus-source-patches-"));
|
|
607
|
+
const index = resolve(directory, "index");
|
|
608
|
+
const env = {
|
|
609
|
+
...process.env,
|
|
610
|
+
GIT_INDEX_FILE: index
|
|
611
|
+
};
|
|
612
|
+
const run = (args) => {
|
|
613
|
+
const result = spawnSync("git", args, {
|
|
614
|
+
cwd: root,
|
|
615
|
+
encoding: "utf8",
|
|
616
|
+
env
|
|
617
|
+
});
|
|
618
|
+
if (result.status === 0) return;
|
|
619
|
+
const detail = result.stderr.trim();
|
|
620
|
+
throw new Error("git " + args.join(" ") + " failed" + (detail === "" ? "" : ": " + detail));
|
|
621
|
+
};
|
|
622
|
+
try {
|
|
623
|
+
run(["read-tree", "HEAD"]);
|
|
624
|
+
for (const file of files) run([
|
|
625
|
+
"apply",
|
|
626
|
+
"--cached",
|
|
627
|
+
"--3way",
|
|
628
|
+
file
|
|
629
|
+
]);
|
|
630
|
+
} finally {
|
|
631
|
+
rmSync(directory, {
|
|
632
|
+
recursive: true,
|
|
633
|
+
force: true
|
|
634
|
+
});
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
function requireSourceBase(root, baseRevision) {
|
|
638
|
+
const checkoutRevision = requireString(git(root, ["rev-parse", "HEAD"]), "DSH checkout revision");
|
|
639
|
+
if (checkoutRevision !== baseRevision) throw new Error("DSH checkout must be exact official base " + baseRevision + ", found " + checkoutRevision);
|
|
640
|
+
return checkoutRevision;
|
|
641
|
+
}
|
|
642
|
+
/**
|
|
643
|
+
* Names of distribution dependencies that carry `dshPatch` variants.
|
|
644
|
+
*
|
|
645
|
+
* `patchPackageVariants()` resolves these names from the Plus install tree, so they must be
|
|
646
|
+
* packages this distribution actually depends on. The current manifest declares `dshPlus.patchSet`
|
|
647
|
+
* instead of a flat list: only entries whose target is an npm package name a `dshPatch` carrier
|
|
648
|
+
* (`dsh-source` entries name the source packages being patched, which are never Plus dependencies).
|
|
649
|
+
* An older tarball may still carry the flat `dshPlus.patchPackages` list, which wins when present.
|
|
650
|
+
*
|
|
651
|
+
* @param plus - Parsed `dshPlus` block of the distribution manifest.
|
|
652
|
+
* @returns Patch-carrier package names; empty when the distribution ships only source patches.
|
|
653
|
+
*/
|
|
654
|
+
function resolvePatchPackageNames(plus) {
|
|
655
|
+
const declared = plus.patchPackages;
|
|
656
|
+
if (declared !== void 0) {
|
|
657
|
+
const names = requireStringArray(declared, "dshPlus.patchPackages");
|
|
658
|
+
if (names.length > 0) return names;
|
|
659
|
+
}
|
|
660
|
+
const entries = requireObject(plus.patchSet, "dshPlus.patchSet").entries;
|
|
661
|
+
if (!Array.isArray(entries)) throw new Error("dshPlus.patchSet.entries must be an array");
|
|
662
|
+
const names = /* @__PURE__ */ new Set();
|
|
663
|
+
for (const [index, value] of entries.entries()) {
|
|
664
|
+
const target = requireObject(requireObject(value, "dshPlus.patchSet.entries[" + String(index) + "]").target, "dshPlus.patchSet.entries[" + String(index) + "].target");
|
|
665
|
+
if (target.kind !== "npm") continue;
|
|
666
|
+
names.add(requireString(target.name, "dshPlus.patchSet.entries[" + String(index) + "].target.name"));
|
|
667
|
+
}
|
|
668
|
+
return [...names].sort();
|
|
669
|
+
}
|
|
670
|
+
function patchPackageVariants(distributionDirectory, names) {
|
|
671
|
+
const variants = [];
|
|
672
|
+
for (const name of names) {
|
|
673
|
+
const patchPackage = resolveInstalledPackage(distributionDirectory, name);
|
|
674
|
+
const declaration = requireObject(patchPackage.manifest.dshPatch, name + " dshPatch");
|
|
675
|
+
if (declaration.formatVersion !== FORMAT_VERSION) throw new Error("unsupported dshPatch formatVersion in " + name + ": " + String(declaration.formatVersion));
|
|
676
|
+
if (!Array.isArray(declaration.variants) || declaration.variants.length === 0) throw new Error(name + " dshPatch.variants must be a non-empty array");
|
|
677
|
+
for (const [index, value] of declaration.variants.entries()) variants.push(parseVariant(value, index, patchPackage));
|
|
678
|
+
}
|
|
679
|
+
return variants;
|
|
680
|
+
}
|
|
681
|
+
/**
|
|
682
|
+
* Build patch variants from the distribution's `dshPlus.patchSet`.
|
|
683
|
+
*
|
|
684
|
+
* The patch files live inside the Plus package (`./patches/*.patch`), so every variant points at
|
|
685
|
+
* the distribution itself as its patch package: `resolvePatchFile()` then resolves the declared
|
|
686
|
+
* path inside the tarball, and `materializePatchFiles()` copies it into the profile's stable
|
|
687
|
+
* patch directory. Unlike the retired `dshPatch` carrier packages, the source packages named in
|
|
688
|
+
* `target.packages` are never resolved from the Plus tree -- they are the packages being patched.
|
|
689
|
+
*
|
|
690
|
+
* @param distribution - Installed Plus distribution package.
|
|
691
|
+
*/
|
|
692
|
+
function patchSetVariants(distribution) {
|
|
693
|
+
const patchSet = readPatchSet(distribution.directory, distribution.manifest);
|
|
694
|
+
return [...patchSet.entries, ...patchSet.thirdPartyPatches].map((entry) => ({
|
|
695
|
+
id: entry.id,
|
|
696
|
+
dsh: entry.dsh,
|
|
697
|
+
file: entry.file,
|
|
698
|
+
patchPackage: distribution,
|
|
699
|
+
target: entry.target.kind === "npm" ? {
|
|
700
|
+
kind: "npm",
|
|
701
|
+
name: entry.target.name,
|
|
702
|
+
range: entry.target.range
|
|
703
|
+
} : {
|
|
704
|
+
kind: "dsh-source",
|
|
705
|
+
baseRevision: entry.target.baseRevision
|
|
706
|
+
}
|
|
707
|
+
}));
|
|
708
|
+
}
|
|
709
|
+
function prepareSelection(distributionDirectory, profileDirectory, dshRoot, skipBuild) {
|
|
710
|
+
const distributionManifest = readJsonObject(resolve(distributionDirectory, "package.json"), "Plus distribution manifest");
|
|
711
|
+
const distributionName = requireString(distributionManifest.name, "Plus distribution name");
|
|
712
|
+
const distributionVersion = requireVersion(distributionManifest.version, "Plus distribution version");
|
|
713
|
+
const plus = requireObject(distributionManifest.dshPlus, "Plus distribution dshPlus");
|
|
714
|
+
if (plus.formatVersion !== FORMAT_VERSION) throw new Error("unsupported dshPlus formatVersion: " + String(plus.formatVersion));
|
|
715
|
+
const dshRange = requireRange(requireObject(plus.compatibility, "dshPlus.compatibility").dsh, "dshPlus.compatibility.dsh");
|
|
716
|
+
const baseRevision = requireString(requireObject(plus.sourceBase, "dshPlus.sourceBase").revision, "dshPlus.sourceBase.revision");
|
|
717
|
+
const profile = requireObject(plus.profile, "dshPlus.profile");
|
|
718
|
+
const bundles = requireStringArray(profile.bundles, "dshPlus.profile.bundles");
|
|
719
|
+
const rawProfileDependencySpecs = requireObject(profile.dependencies, "dshPlus.profile.dependencies");
|
|
720
|
+
const profileDependencySpecs = {};
|
|
721
|
+
for (const [name, spec] of Object.entries(rawProfileDependencySpecs)) profileDependencySpecs[name] = requireString(spec, "dshPlus.profile.dependencies." + name);
|
|
722
|
+
const rawAllowBuilds = requireObject(profile.allowBuilds, "dshPlus.profile.allowBuilds");
|
|
723
|
+
const allowBuilds = {};
|
|
724
|
+
for (const [name, allowed] of Object.entries(rawAllowBuilds)) {
|
|
725
|
+
if (typeof allowed !== "boolean") throw new Error("dshPlus.profile.allowBuilds." + name + " must be a boolean");
|
|
726
|
+
allowBuilds[name] = allowed;
|
|
727
|
+
}
|
|
728
|
+
const compatibilityExemptions = profile.compatibility === void 0 ? [] : requireStringArray(profile.compatibility, "dshPlus.profile.compatibility");
|
|
729
|
+
for (const entry of compatibilityExemptions) {
|
|
730
|
+
const separator = entry.lastIndexOf("@");
|
|
731
|
+
const name = entry.slice(0, separator);
|
|
732
|
+
const version = entry.slice(separator + 1);
|
|
733
|
+
if (separator <= 0 || valid(version) === null) throw new Error("dshPlus.profile.compatibility entry must be an exact package@version, got: " + entry);
|
|
734
|
+
if (profileDependencySpecs[name] !== version) throw new Error("dshPlus.profile.compatibility names " + entry + ", which dshPlus.profile.dependencies does not pin");
|
|
735
|
+
}
|
|
736
|
+
const patchPackageNames = resolvePatchPackageNames(plus);
|
|
737
|
+
const dependencies = requireObject(distributionManifest.dependencies, "Plus distribution dependencies");
|
|
738
|
+
const dshVersion = requireVersion(readJsonObject(resolve(dshRoot, "package.json"), "DSH source manifest").version, "DSH source version");
|
|
739
|
+
if (!satisfies(dshVersion, dshRange, { includePrerelease: true })) throw new Error(distributionName + "@" + distributionVersion + " requires DSH " + dshRange + ", found " + dshVersion);
|
|
740
|
+
const checkoutRevision = requireSourceBase(dshRoot, baseRevision);
|
|
741
|
+
const variants = plus.patchPackages === void 0 ? patchSetVariants({
|
|
742
|
+
name: distributionName,
|
|
743
|
+
version: distributionVersion,
|
|
744
|
+
directory: distributionDirectory,
|
|
745
|
+
manifest: distributionManifest
|
|
746
|
+
}) : patchPackageVariants(distributionDirectory, patchPackageNames);
|
|
747
|
+
const groups = /* @__PURE__ */ new Map();
|
|
748
|
+
for (const variant of variants) {
|
|
749
|
+
const entries = groups.get(variant.id) ?? [];
|
|
750
|
+
entries.push(variant);
|
|
751
|
+
groups.set(variant.id, entries);
|
|
752
|
+
}
|
|
753
|
+
const sourceStates = /* @__PURE__ */ new Map();
|
|
754
|
+
const orderedGroups = [...groups].sort(([left], [right]) => left.localeCompare(right));
|
|
755
|
+
if (!skipBuild) {
|
|
756
|
+
for (const [, variants] of orderedGroups) for (const variant of variants) {
|
|
757
|
+
if (variant.target.kind !== "dsh-source" || variant.target.baseRevision !== baseRevision || !satisfies(dshVersion, variant.dsh, { includePrerelease: true })) continue;
|
|
758
|
+
const file = resolvePatchFile(variant.patchPackage.directory, variant.file);
|
|
759
|
+
sourceStates.set(file.absoluteFile, sourcePatchState(dshRoot, file.absoluteFile));
|
|
760
|
+
}
|
|
761
|
+
preflightSourcePatches(dshRoot, [...sourceStates].flatMap(([file, state]) => state === "pending" ? [file] : []));
|
|
762
|
+
}
|
|
763
|
+
writeProfileRequirements(profileDirectory, profileDependencySpecs, allowBuilds);
|
|
764
|
+
writeProfileCompatibility(profileDirectory, compatibilityExemptions, dshVersion);
|
|
765
|
+
runPnpm(profileDirectory, ["install", "--no-frozen-lockfile"], "pnpm install Plus profile dependencies");
|
|
766
|
+
const profileDependencies = Object.keys(profileDependencySpecs).sort().map((name) => {
|
|
767
|
+
return {
|
|
768
|
+
name,
|
|
769
|
+
version: resolveInstalledPackage(profileDirectory, name).version
|
|
770
|
+
};
|
|
771
|
+
});
|
|
772
|
+
const runtimePackages = [...Object.keys(dependencies).filter((name) => !patchPackageNames.includes(name)).sort().map((name) => {
|
|
773
|
+
return {
|
|
774
|
+
name,
|
|
775
|
+
version: resolveInstalledPackage(distributionDirectory, name).version
|
|
776
|
+
};
|
|
777
|
+
}), ...profileDependencies].sort((left, right) => left.name.localeCompare(right.name));
|
|
778
|
+
const selected = [];
|
|
779
|
+
for (const [id, variants] of orderedGroups) {
|
|
780
|
+
const candidates = variants.flatMap((variant) => {
|
|
781
|
+
if (!satisfies(dshVersion, variant.dsh, { includePrerelease: true })) return [];
|
|
782
|
+
const file = resolvePatchFile(variant.patchPackage.directory, variant.file);
|
|
783
|
+
if (variant.target.kind === "dsh-source") {
|
|
784
|
+
if (variant.target.baseRevision !== baseRevision) return [];
|
|
785
|
+
return [{
|
|
786
|
+
id,
|
|
787
|
+
patchPackage: {
|
|
788
|
+
name: variant.patchPackage.name,
|
|
789
|
+
version: variant.patchPackage.version
|
|
790
|
+
},
|
|
791
|
+
file: file.relativeFile,
|
|
792
|
+
absoluteFile: file.absoluteFile,
|
|
793
|
+
target: {
|
|
794
|
+
kind: "dsh-source",
|
|
795
|
+
baseRevision
|
|
796
|
+
},
|
|
797
|
+
alreadyApplied: skipBuild || sourceStates.get(file.absoluteFile) === "applied"
|
|
798
|
+
}];
|
|
799
|
+
}
|
|
800
|
+
const targetVersion = resolveInstalledPackage(variant.target.name in profileDependencySpecs ? profileDirectory : distributionDirectory, variant.target.name).version;
|
|
801
|
+
if (!satisfies(targetVersion, variant.target.range, { includePrerelease: true })) return [];
|
|
802
|
+
return [{
|
|
803
|
+
id,
|
|
804
|
+
patchPackage: {
|
|
805
|
+
name: variant.patchPackage.name,
|
|
806
|
+
version: variant.patchPackage.version
|
|
807
|
+
},
|
|
808
|
+
file: file.relativeFile,
|
|
809
|
+
absoluteFile: file.absoluteFile,
|
|
810
|
+
target: {
|
|
811
|
+
kind: "npm",
|
|
812
|
+
name: variant.target.name,
|
|
813
|
+
version: targetVersion
|
|
814
|
+
}
|
|
815
|
+
}];
|
|
816
|
+
});
|
|
817
|
+
const [candidate] = candidates;
|
|
818
|
+
if (candidate === void 0) throw new Error("no compatible patch variant for " + id + " on DSH " + dshVersion);
|
|
819
|
+
if (candidates.length > 1) throw new Error("multiple compatible patch variants for " + id + " on DSH " + dshVersion);
|
|
820
|
+
selected.push(candidate);
|
|
821
|
+
}
|
|
822
|
+
const lockPatches = selected.map((patch) => {
|
|
823
|
+
if ("alreadyApplied" in patch) {
|
|
824
|
+
const { absoluteFile: _absoluteFile, alreadyApplied: _alreadyApplied, ...locked } = patch;
|
|
825
|
+
return locked;
|
|
826
|
+
}
|
|
827
|
+
const { absoluteFile: _absoluteFile, ...locked } = patch;
|
|
828
|
+
return locked;
|
|
829
|
+
});
|
|
830
|
+
return {
|
|
831
|
+
patches: selected,
|
|
832
|
+
bundles,
|
|
833
|
+
runtimePackages,
|
|
834
|
+
profileDependencies,
|
|
835
|
+
allowBuilds,
|
|
836
|
+
sourceRevision: checkoutRevision,
|
|
837
|
+
lock: {
|
|
838
|
+
formatVersion: FORMAT_VERSION,
|
|
839
|
+
distribution: {
|
|
840
|
+
name: distributionName,
|
|
841
|
+
version: distributionVersion
|
|
842
|
+
},
|
|
843
|
+
dsh: {
|
|
844
|
+
version: dshVersion,
|
|
845
|
+
baseRevision,
|
|
846
|
+
checkoutRevision
|
|
847
|
+
},
|
|
848
|
+
runtimePackages,
|
|
849
|
+
patches: lockPatches
|
|
850
|
+
}
|
|
851
|
+
};
|
|
852
|
+
}
|
|
853
|
+
/** 保留每个独立payload,并为同一npm目标生成pnpm可消费的唯一组合patch。 */
|
|
854
|
+
function materializePatchFiles(profileDirectory, patches) {
|
|
855
|
+
const root = resolve(profileDirectory, ".dsh-plus", "patches");
|
|
856
|
+
rmSync(root, {
|
|
857
|
+
recursive: true,
|
|
858
|
+
force: true
|
|
859
|
+
});
|
|
860
|
+
const stablePatches = patches.map((patch) => {
|
|
861
|
+
const destination = resolve(root, patch.patchPackage.name, patch.patchPackage.version, patch.file);
|
|
862
|
+
mkdirSync(dirname(destination), { recursive: true });
|
|
863
|
+
copyFileSync(patch.absoluteFile, destination);
|
|
864
|
+
return {
|
|
865
|
+
...patch,
|
|
866
|
+
absoluteFile: destination
|
|
867
|
+
};
|
|
868
|
+
});
|
|
869
|
+
const npmGroups = /* @__PURE__ */ new Map();
|
|
870
|
+
for (const patch of stablePatches) {
|
|
871
|
+
if (!isSelectedNpmPatch(patch)) continue;
|
|
872
|
+
const key = patch.target.name + "@" + patch.target.version;
|
|
873
|
+
const group = npmGroups.get(key);
|
|
874
|
+
if (group === void 0) npmGroups.set(key, [patch]);
|
|
875
|
+
else group.push(patch);
|
|
876
|
+
}
|
|
877
|
+
const npmPatchFiles = /* @__PURE__ */ new Map();
|
|
878
|
+
for (const [key, group] of npmGroups) {
|
|
879
|
+
const first = group[0];
|
|
880
|
+
if (group.length === 1) {
|
|
881
|
+
npmPatchFiles.set(key, first.absoluteFile);
|
|
882
|
+
continue;
|
|
883
|
+
}
|
|
884
|
+
const members = group.map((patch) => encodeURIComponent(patch.id + "@" + patch.patchPackage.version)).join("+");
|
|
885
|
+
const destination = resolve(root, "combined", encodeURIComponent(key), members + ".patch");
|
|
886
|
+
mkdirSync(dirname(destination), { recursive: true });
|
|
887
|
+
writeFileSync(destination, group.map((patch) => {
|
|
888
|
+
const content = readFileSync(patch.absoluteFile, "utf8");
|
|
889
|
+
return content.endsWith("\n") ? content : content + "\n";
|
|
890
|
+
}).join(""));
|
|
891
|
+
npmPatchFiles.set(key, destination);
|
|
892
|
+
}
|
|
893
|
+
return {
|
|
894
|
+
patches: stablePatches,
|
|
895
|
+
npmPatchFiles
|
|
896
|
+
};
|
|
897
|
+
}
|
|
898
|
+
function profilePatchConfiguration(profileDirectory, npmPatchFiles) {
|
|
899
|
+
const document = parseDocument(readFileSync(resolve(profileDirectory, "pnpm-workspace.yaml"), "utf8"));
|
|
900
|
+
const [documentError] = document.errors;
|
|
901
|
+
if (documentError !== void 0) throw new Error("Plus profile workspace is not valid YAML", { cause: documentError });
|
|
902
|
+
let changed = false;
|
|
903
|
+
const desiredKeys = new Set(npmPatchFiles.keys());
|
|
904
|
+
const workspace = requireObject(document.toJS(), "Plus profile workspace");
|
|
905
|
+
const configured = workspace.patchedDependencies === void 0 ? {} : requireObject(workspace.patchedDependencies, "Plus profile patchedDependencies");
|
|
906
|
+
for (const [key, value] of Object.entries(configured)) if (typeof value === "string" && value.startsWith(".dsh-plus/patches/") && !desiredKeys.has(key)) {
|
|
907
|
+
document.deleteIn(["patchedDependencies", key]);
|
|
908
|
+
changed = true;
|
|
909
|
+
}
|
|
910
|
+
for (const [key, file] of npmPatchFiles) {
|
|
911
|
+
const value = relative(profileDirectory, file).replaceAll("\\", "/");
|
|
912
|
+
const current = document.getIn(["patchedDependencies", key]);
|
|
913
|
+
if (current !== void 0 && current !== value && (typeof current !== "string" || !current.startsWith(".dsh-plus/patches/"))) throw new Error("profile patchedDependencies." + key + " is already owned by another payload");
|
|
914
|
+
if (current === value) continue;
|
|
915
|
+
document.setIn(["patchedDependencies", key], value);
|
|
916
|
+
changed = true;
|
|
917
|
+
}
|
|
918
|
+
return {
|
|
919
|
+
text: String(document),
|
|
920
|
+
changed
|
|
921
|
+
};
|
|
922
|
+
}
|
|
923
|
+
/**
|
|
924
|
+
* Profile-local pnpm hook that keeps the CDN-only xlsx dependency installable.
|
|
925
|
+
*
|
|
926
|
+
* `@deepseek-ai/dsh-client-ui-sidebar-documentpreview` declares `xlsx` as
|
|
927
|
+
* `https://cdn.sheetjs.com/xlsx-0.20.3/xlsx-0.20.3.tgz`. SheetJS stopped publishing to npm
|
|
928
|
+
* after 0.18.5, so no registry mirror carries 0.20.3, and the build network cannot reach that
|
|
929
|
+
* CDN (measured 2026-11-06: every request times out). The image vendors the tarball at
|
|
930
|
+
* /opt/dsh-vendor; this hook rewrites the spec wherever it appears so a cold profile install
|
|
931
|
+
* never requests the CDN.
|
|
932
|
+
*
|
|
933
|
+
* Written into the profile root rather than a template: the Plus bundle has no profile
|
|
934
|
+
* template directory, the profile manifest and pnpm-workspace.yaml are generated here.
|
|
935
|
+
*/
|
|
936
|
+
const PROFILE_PNPMFILE = `// Generated by @sparkelf/dsh-plus -- do not edit; rewritten on every apply.
|
|
937
|
+
const VENDORED_XLSX = '/opt/dsh-vendor/xlsx-0.20.3.tgz'
|
|
938
|
+
const CDN_XLSX = /^https?:\\/\\/cdn\\.sheetjs\\.com\\//
|
|
939
|
+
const FIELDS = ['dependencies', 'devDependencies', 'optionalDependencies', 'peerDependencies']
|
|
940
|
+
|
|
941
|
+
module.exports = {
|
|
942
|
+
hooks: {
|
|
943
|
+
readPackage(pkg) {
|
|
944
|
+
for (const field of FIELDS) {
|
|
945
|
+
const deps = pkg && pkg[field]
|
|
946
|
+
if (deps && typeof deps.xlsx === 'string' && CDN_XLSX.test(deps.xlsx)) {
|
|
947
|
+
deps.xlsx = 'file:' + VENDORED_XLSX
|
|
948
|
+
}
|
|
949
|
+
}
|
|
950
|
+
return pkg
|
|
951
|
+
},
|
|
952
|
+
},
|
|
953
|
+
}
|
|
954
|
+
`;
|
|
955
|
+
/** Write the profile pnpmfile when its content changed; returns whether it wrote. */
|
|
956
|
+
function writeProfilePnpmfile(profileDirectory) {
|
|
957
|
+
const path = resolve(profileDirectory, ".pnpmfile.cjs");
|
|
958
|
+
if ((existsSync(path) ? readFileSync(path, "utf8") : void 0) === PROFILE_PNPMFILE) return false;
|
|
959
|
+
writeFileSync(path, PROFILE_PNPMFILE);
|
|
960
|
+
console.log("profile pnpmfile: xlsx -> /opt/dsh-vendor/xlsx-0.20.3.tgz");
|
|
961
|
+
return true;
|
|
962
|
+
}
|
|
963
|
+
function writeProfileRequirements(profileDirectory, dependencySpecs, allowBuilds) {
|
|
964
|
+
const manifestPath = resolve(profileDirectory, "package.json");
|
|
965
|
+
const manifest = readJsonObject(manifestPath, "DSH profile manifest");
|
|
966
|
+
const dependencies = requireObject(manifest.dependencies, "DSH profile dependencies");
|
|
967
|
+
let manifestChanged = false;
|
|
968
|
+
for (const [name, spec] of Object.entries(dependencySpecs)) {
|
|
969
|
+
const current = dependencies[name];
|
|
970
|
+
if (current !== void 0 && current !== spec) throw new Error("profile dependency " + name + " is configured as " + JSON.stringify(current) + ", expected " + spec);
|
|
971
|
+
if (current === void 0) {
|
|
972
|
+
dependencies[name] = spec;
|
|
973
|
+
manifestChanged = true;
|
|
974
|
+
}
|
|
975
|
+
}
|
|
976
|
+
const workspacePath = resolve(profileDirectory, "pnpm-workspace.yaml");
|
|
977
|
+
const document = parseDocument(readFileSync(workspacePath, "utf8"));
|
|
978
|
+
const [documentError] = document.errors;
|
|
979
|
+
if (documentError !== void 0) throw new Error("Plus profile workspace is not valid YAML", { cause: documentError });
|
|
980
|
+
let workspaceChanged = false;
|
|
981
|
+
const autoInstallPeers = document.get("autoInstallPeers");
|
|
982
|
+
if (autoInstallPeers !== void 0 && autoInstallPeers !== false) throw new Error("profile autoInstallPeers must be false because official peers come from the selected DSH source");
|
|
983
|
+
if (autoInstallPeers === void 0) {
|
|
984
|
+
document.set("autoInstallPeers", false);
|
|
985
|
+
workspaceChanged = true;
|
|
986
|
+
}
|
|
987
|
+
const managedFileAsset = (value) => value.startsWith("file:") && (/[\\/]\.dsh[\\/]releases[\\/]plus[\\/].*[\\/]packages[\\/]/u.test(value) || /[\\/]\.dsh-plus[\\/]packages[\\/]/u.test(value));
|
|
988
|
+
const retiredOverrides = new Set([
|
|
989
|
+
"@huanlin/dsh-plugin-better-sidebar-plugin-office",
|
|
990
|
+
"@sparkelf/dsh-office-viewer-fonts",
|
|
991
|
+
"dsh-better-sidebar",
|
|
992
|
+
"dsh-video-preview"
|
|
993
|
+
]);
|
|
994
|
+
const workspace = requireObject(document.toJS(), "Plus profile workspace");
|
|
995
|
+
const configuredOverrides = workspace.overrides === void 0 ? {} : requireObject(workspace.overrides, "Plus profile overrides");
|
|
996
|
+
for (const [name, value] of Object.entries(configuredOverrides)) if (retiredOverrides.has(name) || typeof value === "string" && managedFileAsset(value)) {
|
|
997
|
+
document.deleteIn(["overrides", name]);
|
|
998
|
+
workspaceChanged = true;
|
|
999
|
+
}
|
|
1000
|
+
for (const [name, allowed] of Object.entries(allowBuilds)) {
|
|
1001
|
+
const current = document.getIn(["allowBuilds", name]);
|
|
1002
|
+
if (current !== void 0 && current !== allowed) throw new Error("profile allowBuilds." + name + " is configured as " + JSON.stringify(current) + ", expected " + String(allowed));
|
|
1003
|
+
if (current === void 0) {
|
|
1004
|
+
document.setIn(["allowBuilds", name], allowed);
|
|
1005
|
+
workspaceChanged = true;
|
|
1006
|
+
}
|
|
1007
|
+
}
|
|
1008
|
+
if (manifestChanged) {
|
|
1009
|
+
manifest.dependencies = dependencies;
|
|
1010
|
+
writeFileSync(manifestPath, JSON.stringify(manifest, void 0, 2) + "\n");
|
|
1011
|
+
}
|
|
1012
|
+
if (workspaceChanged) writeFileSync(workspacePath, String(document));
|
|
1013
|
+
const pnpmfileChanged = writeProfilePnpmfile(profileDirectory);
|
|
1014
|
+
return manifestChanged || workspaceChanged || pnpmfileChanged;
|
|
1015
|
+
}
|
|
1016
|
+
function writeProfileComposition(profileDirectory, bundles, runtimePackages) {
|
|
1017
|
+
const path = resolve(profileDirectory, "package.json");
|
|
1018
|
+
const manifest = readJsonObject(path, "DSH profile manifest");
|
|
1019
|
+
const dependencies = requireObject(manifest.dependencies, "DSH profile dependencies");
|
|
1020
|
+
let changed = false;
|
|
1021
|
+
for (const runtimePackage of runtimePackages) {
|
|
1022
|
+
if (dependencies[runtimePackage.name] === void 0) {
|
|
1023
|
+
dependencies[runtimePackage.name] = runtimePackage.version;
|
|
1024
|
+
changed = true;
|
|
1025
|
+
continue;
|
|
1026
|
+
}
|
|
1027
|
+
const installed = resolveInstalledPackage(profileDirectory, runtimePackage.name);
|
|
1028
|
+
if (installed.version !== runtimePackage.version) throw new Error("profile dependency " + runtimePackage.name + " resolves to " + installed.version + ", but the Plus distribution requires " + runtimePackage.version);
|
|
1029
|
+
}
|
|
1030
|
+
const dsh = manifest.dsh === void 0 ? {} : requireObject(manifest.dsh, "DSH profile dsh");
|
|
1031
|
+
const profile = dsh.profile === void 0 ? {} : requireObject(dsh.profile, "DSH profile dsh.profile");
|
|
1032
|
+
if (JSON.stringify(profile.bundles) !== JSON.stringify(bundles)) changed = true;
|
|
1033
|
+
manifest.dependencies = dependencies;
|
|
1034
|
+
manifest.dsh = {
|
|
1035
|
+
...dsh,
|
|
1036
|
+
profile: {
|
|
1037
|
+
...profile,
|
|
1038
|
+
bundles: [...bundles]
|
|
1039
|
+
}
|
|
1040
|
+
};
|
|
1041
|
+
if (changed) writeFileSync(path, JSON.stringify(manifest, void 0, 2) + "\n");
|
|
1042
|
+
return changed;
|
|
1043
|
+
}
|
|
1044
|
+
function runPnpm(profileDirectory, args, label) {
|
|
1045
|
+
const result = spawnSync("pnpm", args, {
|
|
1046
|
+
cwd: profileDirectory,
|
|
1047
|
+
stdio: "inherit",
|
|
1048
|
+
shell: process.platform === "win32"
|
|
1049
|
+
});
|
|
1050
|
+
if (result.error !== void 0) throw result.error;
|
|
1051
|
+
if (result.status !== 0) throw new Error(label + " failed with exit code " + String(result.status));
|
|
1052
|
+
}
|
|
1053
|
+
function pendingSourcePatchFiles(patches) {
|
|
1054
|
+
return patches.flatMap((patch) => "alreadyApplied" in patch && !patch.alreadyApplied ? [patch.absoluteFile] : []);
|
|
1055
|
+
}
|
|
1056
|
+
/**
|
|
1057
|
+
* Digest of the source half of a patch selection.
|
|
1058
|
+
*
|
|
1059
|
+
* The row set matches `scripts/apply-plus-preflight.sh`: one row per selected `dsh-source`
|
|
1060
|
+
* patch with its id, distribution-relative file, base revision, and payload bytes, sorted by
|
|
1061
|
+
* id. Hashing the sorted JSON keeps the digest independent of selection order.
|
|
1062
|
+
*
|
|
1063
|
+
* @param patches - Selected patches, after or before materialization; payload bytes are equal.
|
|
1064
|
+
* @returns Lowercase hex SHA-256 of the canonical row list.
|
|
1065
|
+
*/
|
|
1066
|
+
function sourcePatchsetDigest(patches) {
|
|
1067
|
+
const rows = patches.filter((patch) => patch.target.kind === "dsh-source").map((patch) => ({
|
|
1068
|
+
id: patch.id,
|
|
1069
|
+
file: patch.file,
|
|
1070
|
+
baseRevision: patch.target.baseRevision,
|
|
1071
|
+
sha256: createHash("sha256").update(readFileSync(patch.absoluteFile)).digest("hex")
|
|
1072
|
+
})).sort((left, right) => left.id.localeCompare(right.id));
|
|
1073
|
+
return createHash("sha256").update(JSON.stringify(rows)).digest("hex");
|
|
1074
|
+
}
|
|
1075
|
+
/**
|
|
1076
|
+
* Write the build marker after `build:official` succeeds.
|
|
1077
|
+
* @param dshRoot - Built official checkout.
|
|
1078
|
+
* @param sourceRevision - Its `git rev-parse HEAD`, already checked against the distribution.
|
|
1079
|
+
* @param patches - Selected patches whose source half the build now contains.
|
|
1080
|
+
* @returns Absolute marker path.
|
|
1081
|
+
*/
|
|
1082
|
+
function writeBuildMarker(dshRoot, sourceRevision, patches) {
|
|
1083
|
+
const path = resolve(dshRoot, BUILD_MARKER);
|
|
1084
|
+
const marker = {
|
|
1085
|
+
sourceRevision,
|
|
1086
|
+
patchsetDigest: sourcePatchsetDigest(patches),
|
|
1087
|
+
builtAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
1088
|
+
};
|
|
1089
|
+
const temporary = path + ".tmp";
|
|
1090
|
+
writeFileSync(temporary, JSON.stringify(marker, void 0, 2) + "\n");
|
|
1091
|
+
renameSync(temporary, path);
|
|
1092
|
+
return path;
|
|
1093
|
+
}
|
|
1094
|
+
/**
|
|
1095
|
+
* Refuse `--skip-build` unless the checkout still carries the exact prebuilt result.
|
|
1096
|
+
*
|
|
1097
|
+
* Four facts have to agree: every selected source patch is already applied, the marker
|
|
1098
|
+
* belongs to the checkout revision, the marker's digest covers the same patch bytes the
|
|
1099
|
+
* distribution selects now, and the build entry points exist. Skipping any one of them
|
|
1100
|
+
* would ship stale `lib/` output under a patch set that no longer matches it.
|
|
1101
|
+
*
|
|
1102
|
+
* @param dshRoot - Official checkout the profile links into.
|
|
1103
|
+
* @param patches - Selected patches, materialized into the profile.
|
|
1104
|
+
* @param pendingFiles - Selected source patch payloads git still reports as unapplied.
|
|
1105
|
+
*/
|
|
1106
|
+
function assertPrebuiltSource(dshRoot, patches, pendingFiles) {
|
|
1107
|
+
if (pendingFiles.length > 0) throw new Error("--skip-build requires every selected source patch to be applied already; " + String(pendingFiles.length) + " still pending");
|
|
1108
|
+
const markerPath = resolve(dshRoot, BUILD_MARKER);
|
|
1109
|
+
if (!existsSync(markerPath)) throw new Error("--skip-build needs " + markerPath + " from an earlier full apply");
|
|
1110
|
+
const parsed = JSON.parse(readFileSync(markerPath, "utf8"));
|
|
1111
|
+
if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) throw new Error(markerPath + " must be a JSON object");
|
|
1112
|
+
const marker = parsed;
|
|
1113
|
+
const sourceRevision = requireString(git(dshRoot, ["rev-parse", "HEAD"]), "built source revision");
|
|
1114
|
+
if (marker.sourceRevision !== sourceRevision) throw new Error("--skip-build marker revision " + String(marker.sourceRevision) + " differs from checkout " + sourceRevision);
|
|
1115
|
+
const digest = sourcePatchsetDigest(patches);
|
|
1116
|
+
if (marker.patchsetDigest !== digest) throw new Error("--skip-build patch set differs from the prebuilt marker");
|
|
1117
|
+
const missing = [resolve(dshRoot, "apps/cli/lib/bin.js"), resolve(dshRoot, "packages/client/ui-chat/lib/client.js")].filter((entry) => !existsSync(entry));
|
|
1118
|
+
if (missing.length > 0) throw new Error("--skip-build prebuilt entry points are missing: " + missing.join(", "));
|
|
1119
|
+
console.log("[dsh-plus] skip-build verified " + markerPath + " (" + digest + ")");
|
|
1120
|
+
}
|
|
1121
|
+
function applySelectedPatches(profileDirectory, dshRoot, patches, profileChanged, skipBuild, sourceRevision) {
|
|
1122
|
+
const materialized = materializePatchFiles(profileDirectory, patches);
|
|
1123
|
+
const workspacePath = resolve(profileDirectory, "pnpm-workspace.yaml");
|
|
1124
|
+
const workspace = profilePatchConfiguration(profileDirectory, materialized.npmPatchFiles);
|
|
1125
|
+
if (workspace.changed) writeFileSync(workspacePath, workspace.text);
|
|
1126
|
+
if (profileChanged || workspace.changed) runPnpm(profileDirectory, ["install", "--no-frozen-lockfile"], "pnpm install in Plus profile");
|
|
1127
|
+
const sourceFiles = pendingSourcePatchFiles(materialized.patches);
|
|
1128
|
+
if (skipBuild) {
|
|
1129
|
+
assertPrebuiltSource(dshRoot, materialized.patches, sourceFiles);
|
|
1130
|
+
return;
|
|
1131
|
+
}
|
|
1132
|
+
if (sourceFiles.length > 0) git(dshRoot, ["update-index", "--refresh"], true);
|
|
1133
|
+
for (const file of sourceFiles) git(dshRoot, [
|
|
1134
|
+
"apply",
|
|
1135
|
+
"--3way",
|
|
1136
|
+
file
|
|
1137
|
+
]);
|
|
1138
|
+
if (materialized.patches.some((patch) => patch.target.kind === "dsh-source")) {
|
|
1139
|
+
if (sourceFiles.length > 0) runPnpm(dshRoot, ["install", "--no-frozen-lockfile"], "pnpm install after source patches");
|
|
1140
|
+
runPnpm(dshRoot, ["run", "build:official"], "official DSH build after source patches");
|
|
1141
|
+
const marker = writeBuildMarker(dshRoot, sourceRevision, materialized.patches);
|
|
1142
|
+
console.log("[dsh-plus] wrote build marker " + marker);
|
|
1143
|
+
}
|
|
1144
|
+
}
|
|
1145
|
+
function officialPackageRoot(dshRoot) {
|
|
1146
|
+
return resolve(dshRoot, "apps", "cli");
|
|
1147
|
+
}
|
|
1148
|
+
/**
|
|
1149
|
+
* Parse pnpm's workspace roster into exact official package directories.
|
|
1150
|
+
* @param dshRoot - exact official DSH checkout root.
|
|
1151
|
+
* @param stdout - JSON emitted by `pnpm -r list --json --depth -1`.
|
|
1152
|
+
* @returns sorted official package names and directories inside the checkout.
|
|
1153
|
+
*/
|
|
1154
|
+
function parseOfficialWorkspacePackages(dshRoot, stdout) {
|
|
1155
|
+
const parsed = JSON.parse(stdout);
|
|
1156
|
+
if (!Array.isArray(parsed)) throw new Error("pnpm workspace package list must be an array");
|
|
1157
|
+
const packages = /* @__PURE__ */ new Map();
|
|
1158
|
+
for (const [index, value] of parsed.entries()) {
|
|
1159
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) throw new Error("pnpm workspace package list[" + String(index) + "] must be an object");
|
|
1160
|
+
const entry = value;
|
|
1161
|
+
if (typeof entry.name !== "string" || !entry.name.startsWith("@deepseek-ai/")) continue;
|
|
1162
|
+
const localName = entry.name.slice(13);
|
|
1163
|
+
if (localName === "" || localName.includes("/")) throw new Error("unsupported official workspace package name: " + entry.name);
|
|
1164
|
+
if (typeof entry.path !== "string" || entry.path === "") throw new Error("pnpm workspace package " + entry.name + " has no path");
|
|
1165
|
+
const directory = resolve(dshRoot, entry.path);
|
|
1166
|
+
const local = relative(dshRoot, directory);
|
|
1167
|
+
if (isAbsolute(local) || local === ".." || local.startsWith("../")) throw new Error("official workspace package escapes DSH root: " + entry.name);
|
|
1168
|
+
const previous = packages.get(entry.name);
|
|
1169
|
+
if (previous !== void 0 && previous !== directory) throw new Error("duplicate official workspace package: " + entry.name);
|
|
1170
|
+
packages.set(entry.name, directory);
|
|
1171
|
+
}
|
|
1172
|
+
return [...packages].sort(([left], [right]) => left.localeCompare(right)).map(([name, directory]) => ({
|
|
1173
|
+
name,
|
|
1174
|
+
directory
|
|
1175
|
+
}));
|
|
1176
|
+
}
|
|
1177
|
+
function listOfficialWorkspacePackages(dshRoot) {
|
|
1178
|
+
const result = spawnSync("pnpm", [
|
|
1179
|
+
"-r",
|
|
1180
|
+
"list",
|
|
1181
|
+
"--json",
|
|
1182
|
+
"--depth",
|
|
1183
|
+
"-1"
|
|
1184
|
+
], {
|
|
1185
|
+
cwd: dshRoot,
|
|
1186
|
+
encoding: "utf8",
|
|
1187
|
+
shell: process.platform === "win32"
|
|
1188
|
+
});
|
|
1189
|
+
if (result.error !== void 0) throw result.error;
|
|
1190
|
+
if (result.status !== 0) throw new Error("pnpm list official DSH workspace packages failed with exit code " + String(result.status));
|
|
1191
|
+
return parseOfficialWorkspacePackages(dshRoot, result.stdout);
|
|
1192
|
+
}
|
|
1193
|
+
/**
|
|
1194
|
+
* Build the profile's official scope from the CLI closure plus source workspaces.
|
|
1195
|
+
* @param profileDirectory - materialized Plus profile root.
|
|
1196
|
+
* @param cliScope - official packages already installed under the DSH CLI.
|
|
1197
|
+
* @param workspacePackages - additional official source workspaces required by external peers.
|
|
1198
|
+
*/
|
|
1199
|
+
function materializeOfficialPackageScope(profileDirectory, cliScope, workspacePackages) {
|
|
1200
|
+
if (!existsSync(cliScope)) throw new Error("official DSH dependencies are missing under " + cliScope);
|
|
1201
|
+
const destination = resolve(profileDirectory, "node_modules", "@deepseek-ai");
|
|
1202
|
+
rmSync(destination, {
|
|
1203
|
+
recursive: true,
|
|
1204
|
+
force: true
|
|
1205
|
+
});
|
|
1206
|
+
mkdirSync(destination, { recursive: true });
|
|
1207
|
+
const linkType = process.platform === "win32" ? "junction" : "dir";
|
|
1208
|
+
for (const name of readdirSync(cliScope).sort()) symlinkSync(resolve(cliScope, name), resolve(destination, name), linkType);
|
|
1209
|
+
for (const entry of workspacePackages) {
|
|
1210
|
+
const target = resolve(destination, entry.name.slice(13));
|
|
1211
|
+
rmSync(target, {
|
|
1212
|
+
recursive: true,
|
|
1213
|
+
force: true
|
|
1214
|
+
});
|
|
1215
|
+
symlinkSync(entry.directory, target, linkType);
|
|
1216
|
+
}
|
|
1217
|
+
}
|
|
1218
|
+
function linkOfficialPackages(profileDirectory, dshRoot) {
|
|
1219
|
+
materializeOfficialPackageScope(profileDirectory, resolve(officialPackageRoot(dshRoot), "node_modules", "@deepseek-ai"), listOfficialWorkspacePackages(dshRoot));
|
|
1220
|
+
}
|
|
1221
|
+
function writeLock(profileDirectory, lock) {
|
|
1222
|
+
const directory = resolve(profileDirectory, ".dsh-plus");
|
|
1223
|
+
mkdirSync(directory, { recursive: true });
|
|
1224
|
+
const path = resolve(directory, "patchset.lock.json");
|
|
1225
|
+
writeFileSync(path, JSON.stringify(lock, void 0, 2) + "\n", { mode: 384 });
|
|
1226
|
+
chmodSync(path, 384);
|
|
1227
|
+
return path;
|
|
1228
|
+
}
|
|
1229
|
+
const APPLY_USAGE = "usage: dsh-plus apply --dsh-root <official-dsh-root> [--skip-build]";
|
|
1230
|
+
function parseApplyArguments(argv) {
|
|
1231
|
+
if (argv[0] !== "apply") throw new Error(APPLY_USAGE);
|
|
1232
|
+
let dshRoot;
|
|
1233
|
+
let skipBuild = false;
|
|
1234
|
+
for (let index = 1; index < argv.length; index += 1) {
|
|
1235
|
+
const key = argv[index];
|
|
1236
|
+
if (key === "--skip-build") {
|
|
1237
|
+
skipBuild = true;
|
|
1238
|
+
continue;
|
|
1239
|
+
}
|
|
1240
|
+
const value = argv[index + 1];
|
|
1241
|
+
if (key !== "--dsh-root" || value === void 0) throw new Error(APPLY_USAGE);
|
|
1242
|
+
dshRoot = resolve(value);
|
|
1243
|
+
index += 1;
|
|
1244
|
+
}
|
|
1245
|
+
if (dshRoot === void 0) throw new Error(APPLY_USAGE);
|
|
1246
|
+
return {
|
|
1247
|
+
dshRoot,
|
|
1248
|
+
profileDirectory: process.cwd(),
|
|
1249
|
+
skipBuild
|
|
1250
|
+
};
|
|
1251
|
+
}
|
|
1252
|
+
/**
|
|
1253
|
+
* Apply the installed Plus distribution metadata from a DSH profile directory.
|
|
1254
|
+
*
|
|
1255
|
+
* A full run owns both phases: source patches, the official build and its marker, and the
|
|
1256
|
+
* profile install/link. `--skip-build` runs the profile half only and verifies the marker a
|
|
1257
|
+
* previous full run left in the checkout.
|
|
1258
|
+
*
|
|
1259
|
+
* @param argv - `apply --dsh-root <path>` plus optional `--skip-build`.
|
|
1260
|
+
*/
|
|
1261
|
+
function runApply(argv) {
|
|
1262
|
+
const { dshRoot, profileDirectory, skipBuild } = parseApplyArguments(argv);
|
|
1263
|
+
if (requireObject(readJsonObject(resolve(profileDirectory, "package.json"), "DSH profile manifest").dependencies, "DSH profile dependencies")["@sparkelf/dsh-plus"] === void 0) throw new Error("current directory is not a DSH profile with @sparkelf/dsh-plus installed");
|
|
1264
|
+
const selection = prepareSelection(dirname(createRequire(resolve(profileDirectory, "package.json")).resolve("@sparkelf/dsh-plus/package.json")), profileDirectory, dshRoot, skipBuild);
|
|
1265
|
+
const profileChanged = writeProfileComposition(profileDirectory, selection.bundles, selection.runtimePackages);
|
|
1266
|
+
applySelectedPatches(profileDirectory, dshRoot, selection.patches, profileChanged, skipBuild, selection.sourceRevision);
|
|
1267
|
+
linkOfficialPackages(profileDirectory, dshRoot);
|
|
1268
|
+
const installedProfileDependencies = requireObject(readJsonObject(resolve(profileDirectory, "package.json"), "DSH profile manifest").dependencies, "DSH profile dependencies");
|
|
1269
|
+
selection.lock.profile = {
|
|
1270
|
+
bundles: selection.bundles.map((name) => {
|
|
1271
|
+
return {
|
|
1272
|
+
name,
|
|
1273
|
+
version: (installedProfileDependencies[name] === void 0 ? resolveInstalledPackage(officialPackageRoot(dshRoot), name) : resolveInstalledPackage(profileDirectory, name)).version
|
|
1274
|
+
};
|
|
1275
|
+
}),
|
|
1276
|
+
dependencies: selection.profileDependencies,
|
|
1277
|
+
allowBuilds: selection.allowBuilds
|
|
1278
|
+
};
|
|
1279
|
+
const lock = writeLock(profileDirectory, selection.lock);
|
|
1280
|
+
process.stdout.write("[dsh-plus] applied " + String(selection.patches.length) + " patches; lock " + lock + "\n");
|
|
1281
|
+
}
|
|
1282
|
+
//#endregion
|
|
1283
|
+
export { runApply as a, assertRedirectsPublished as c, writeProfileCompatibility as d, writeProfileProjection as f, resolvePatchPackageNames as i, projectEdition as l, materializeOfficialPackageScope as n, sourcePatchsetDigest as o, readPatchSet as p, parseOfficialWorkspacePackages as r, writeBuildMarker as s, assertPrebuiltSource as t, readDistributionFacts as u };
|