dsh-coexist-cli 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/dsh-coexist.js +319 -0
- package/package.json +25 -4
- package/preload.mjs +74 -0
- package/src/apply.js +255 -0
- package/src/backup.js +104 -0
- package/src/commands.js +274 -0
- package/src/desktop.js +369 -0
- package/src/detectors.js +374 -0
- package/src/dumpconfig.js +69 -0
- package/src/graph.js +150 -0
- package/src/installer.js +918 -0
- package/src/launcher.js +262 -0
- package/src/load.js +68 -0
- package/src/logscan.js +113 -0
- package/src/paths.js +54 -0
- package/src/plan.js +203 -0
- package/src/ports.js +62 -0
- package/src/preload/patch.js +144 -0
- package/src/preview.js +122 -0
- package/src/report.js +542 -0
- package/src/semver.js +138 -0
- package/src/sourcescan.js +147 -0
- package/src/state.js +125 -0
- package/README.md +0 -3
package/src/detectors.js
ADDED
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
import net from 'node:net'
|
|
2
|
+
import { compareVersions, minVersionForRange, satisfies } from './semver.js'
|
|
3
|
+
|
|
4
|
+
const HOST_PEER_PREFIXES = ['@deepseek-ai/']
|
|
5
|
+
const PORT_KEY_RE = /^ports?$/i
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* insert 行之间撞同一个 loader entry id。
|
|
9
|
+
* 来自第三方 bundle 的行仍然报告,但是否可自动改名由 plan 判断。
|
|
10
|
+
*/
|
|
11
|
+
export function detectLoaderEntryConflicts(rows) {
|
|
12
|
+
const inserts = rows.filter((row) => row.op === 'insert' || row.op === 'row')
|
|
13
|
+
const byId = new Map()
|
|
14
|
+
|
|
15
|
+
for (const row of inserts) {
|
|
16
|
+
if (typeof row.id !== 'string') continue
|
|
17
|
+
const list = byId.get(row.id) ?? []
|
|
18
|
+
list.push(row)
|
|
19
|
+
byId.set(row.id, list)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const conflicts = []
|
|
23
|
+
for (const [key, entries] of byId) {
|
|
24
|
+
if (entries.length < 2) continue
|
|
25
|
+
// 同 id 同包名的 profile 行是对 bundle 行的覆盖,不是冲突。
|
|
26
|
+
const names = new Set(entries.map((row) => row.name ?? `<${row.source}>`))
|
|
27
|
+
if (names.size === 1) continue
|
|
28
|
+
conflicts.push({
|
|
29
|
+
resource: { type: 'loader-entry', key },
|
|
30
|
+
owners: entries.map((row) => row.name ?? `<${row.source}>`),
|
|
31
|
+
rows: entries.map((row) => ({
|
|
32
|
+
file: row.file,
|
|
33
|
+
source: row.source,
|
|
34
|
+
op: row.op,
|
|
35
|
+
name: row.name ?? null,
|
|
36
|
+
config: row.config,
|
|
37
|
+
disabled: row.disabled,
|
|
38
|
+
})),
|
|
39
|
+
strategies: ['rename-entry', 'merge-row'],
|
|
40
|
+
recommended: 'rename-entry',
|
|
41
|
+
severity: 'conflict',
|
|
42
|
+
})
|
|
43
|
+
}
|
|
44
|
+
return conflicts
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function sourceLabel(row) {
|
|
48
|
+
return row.name ?? `<${row.source}>`
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* patch 层覆盖分析:以“最终生效行”为准。
|
|
53
|
+
* - 只有 replace、没有 insert → dangling;
|
|
54
|
+
* - 最终行没有保留前层的 disabled: true → 冲突;
|
|
55
|
+
* - 最终行清空 config → 冲突;
|
|
56
|
+
* - 最终行丢掉部分 config 键 → info。
|
|
57
|
+
* 每条冲突带 suggestedPatch,供 plan 生成补全行。
|
|
58
|
+
*/
|
|
59
|
+
export function detectPatchOverrides(rows) {
|
|
60
|
+
const byId = new Map()
|
|
61
|
+
for (const row of rows) {
|
|
62
|
+
if (typeof row.id !== 'string') continue
|
|
63
|
+
const list = byId.get(row.id) ?? []
|
|
64
|
+
list.push(row)
|
|
65
|
+
byId.set(row.id, list)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const conflicts = []
|
|
69
|
+
|
|
70
|
+
for (const [id, entries] of byId) {
|
|
71
|
+
const final = entries[entries.length - 1]
|
|
72
|
+
const inserts = entries.filter((row) => row.op === 'insert' || row.op === 'row')
|
|
73
|
+
|
|
74
|
+
if (inserts.length === 0) {
|
|
75
|
+
conflicts.push({
|
|
76
|
+
resource: { type: 'patch-dangling-replace', key: id },
|
|
77
|
+
owners: [sourceLabel(final)],
|
|
78
|
+
detail: `replace 的目标 "${id}" 在所有 patch 层里都没有 insert`,
|
|
79
|
+
rows: [{ file: final.file, source: final.source, op: final.op, name: final.name ?? null }],
|
|
80
|
+
strategies: ['fix-id', 'change-to-insert'],
|
|
81
|
+
recommended: 'manual',
|
|
82
|
+
severity: 'conflict',
|
|
83
|
+
})
|
|
84
|
+
continue
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (entries.length < 2) continue
|
|
88
|
+
|
|
89
|
+
const disabledWanted = entries.some((row) => row.disabled === true)
|
|
90
|
+
const disabledLost = disabledWanted && final.disabled === undefined
|
|
91
|
+
|
|
92
|
+
const earlierConfig = {}
|
|
93
|
+
for (const row of entries.slice(0, -1)) {
|
|
94
|
+
Object.assign(earlierConfig, asObject(row.config) ?? {})
|
|
95
|
+
}
|
|
96
|
+
const finalConfig = asObject(final.config)
|
|
97
|
+
const finalKeys = finalConfig ? Object.keys(finalConfig) : []
|
|
98
|
+
const missingKeys = Object.keys(earlierConfig).filter((key) => !finalKeys.includes(key))
|
|
99
|
+
const configWiped = Object.keys(earlierConfig).length > 0 && finalKeys.length === 0
|
|
100
|
+
|
|
101
|
+
if (!disabledLost && !configWiped && missingKeys.length === 0) continue
|
|
102
|
+
|
|
103
|
+
const mergedConfig = { ...earlierConfig, ...(finalConfig ?? {}) }
|
|
104
|
+
const suggestedPatch = {
|
|
105
|
+
id,
|
|
106
|
+
name: final.name ?? entries[0].name ?? null,
|
|
107
|
+
config: mergedConfig,
|
|
108
|
+
}
|
|
109
|
+
if (disabledWanted) suggestedPatch.disabled = true
|
|
110
|
+
|
|
111
|
+
conflicts.push({
|
|
112
|
+
resource: { type: 'patch-override', key: id },
|
|
113
|
+
owners: [sourceLabel(entries[0]), sourceLabel(final)],
|
|
114
|
+
detail: [
|
|
115
|
+
disabledLost ? '最终生效行没有保留前层的 disabled: true' : null,
|
|
116
|
+
configWiped ? '最终生效行清空了 config' : null,
|
|
117
|
+
!configWiped && missingKeys.length > 0
|
|
118
|
+
? `最终生效行丢失 config 键: ${missingKeys.join(', ')}`
|
|
119
|
+
: null,
|
|
120
|
+
]
|
|
121
|
+
.filter(Boolean)
|
|
122
|
+
.join(';'),
|
|
123
|
+
rows: entries.map((row) => ({
|
|
124
|
+
file: row.file,
|
|
125
|
+
source: row.source,
|
|
126
|
+
op: row.op,
|
|
127
|
+
name: row.name ?? null,
|
|
128
|
+
config: row.config,
|
|
129
|
+
disabled: row.disabled,
|
|
130
|
+
})),
|
|
131
|
+
strategies: ['write-full-config', 'reorder-layers'],
|
|
132
|
+
recommended: 'manual',
|
|
133
|
+
severity: disabledLost || configWiped ? 'conflict' : 'info',
|
|
134
|
+
suggestedPatch,
|
|
135
|
+
})
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return conflicts
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function asObject(value) {
|
|
142
|
+
return value && typeof value === 'object' && !Array.isArray(value) ? value : null
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function isPort(value) {
|
|
146
|
+
return Number.isInteger(value) && value > 0 && value <= 65535
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function portsIn(value, depth = 0) {
|
|
150
|
+
if (!value || typeof value !== 'object' || depth > 4) return []
|
|
151
|
+
|
|
152
|
+
const ports = []
|
|
153
|
+
for (const [key, entry] of Object.entries(value)) {
|
|
154
|
+
if (PORT_KEY_RE.test(key)) {
|
|
155
|
+
if (isPort(entry)) ports.push(entry)
|
|
156
|
+
else if (Array.isArray(entry)) {
|
|
157
|
+
for (const item of entry) if (isPort(item)) ports.push(item)
|
|
158
|
+
} else if (typeof entry === 'string' && /^\d+$/.test(entry) && isPort(Number(entry))) {
|
|
159
|
+
ports.push(Number(entry))
|
|
160
|
+
}
|
|
161
|
+
continue
|
|
162
|
+
}
|
|
163
|
+
if (entry && typeof entry === 'object') ports.push(...portsIn(entry, depth + 1))
|
|
164
|
+
}
|
|
165
|
+
return ports
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** 从 patch 行的 config 里收集显式声明的端口。 */
|
|
169
|
+
export function collectDeclaredPorts(rows) {
|
|
170
|
+
const found = new Map()
|
|
171
|
+
|
|
172
|
+
for (const row of rows) {
|
|
173
|
+
const config = asObject(row.config)
|
|
174
|
+
if (!config) continue
|
|
175
|
+
for (const port of portsIn(config)) {
|
|
176
|
+
const list = found.get(port) ?? []
|
|
177
|
+
list.push({
|
|
178
|
+
owner: sourceLabel(row),
|
|
179
|
+
file: row.file,
|
|
180
|
+
source: row.source,
|
|
181
|
+
id: row.id ?? null,
|
|
182
|
+
name: row.name ?? null,
|
|
183
|
+
})
|
|
184
|
+
found.set(port, list)
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
return found
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** 尝试绑定端口;EADDRINUSE 视为被占用,其它错误不阻止启动前扫描。 */
|
|
192
|
+
export function defaultProbePort(port, host = '127.0.0.1', timeoutMs = 500) {
|
|
193
|
+
return new Promise((resolve) => {
|
|
194
|
+
let settled = false
|
|
195
|
+
const finish = (busy) => {
|
|
196
|
+
if (settled) return
|
|
197
|
+
settled = true
|
|
198
|
+
server.close(() => resolve(busy))
|
|
199
|
+
setTimeout(() => resolve(busy), 50).unref?.()
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
const server = net.createServer()
|
|
203
|
+
server.once('error', (error) => finish(error.code === 'EADDRINUSE'))
|
|
204
|
+
server.once('listening', () => finish(false))
|
|
205
|
+
server.listen({ port, host, exclusive: true })
|
|
206
|
+
setTimeout(() => finish(false), timeoutMs).unref?.()
|
|
207
|
+
})
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
export function detectPortConflicts(rows, options = {}) {
|
|
211
|
+
const declared = collectDeclaredPorts(rows)
|
|
212
|
+
const busyPorts = options.busyPorts ?? new Set()
|
|
213
|
+
const conflicts = []
|
|
214
|
+
|
|
215
|
+
for (const [port, owners] of declared) {
|
|
216
|
+
if (owners.length > 1) {
|
|
217
|
+
conflicts.push({
|
|
218
|
+
resource: { type: 'port', key: String(port) },
|
|
219
|
+
owners: [...new Set(owners.map((owner) => owner.owner))],
|
|
220
|
+
detail: busyPorts.has(port)
|
|
221
|
+
? `端口 ${port} 已被占用,且被多个插件声明`
|
|
222
|
+
: `多个插件都声明了端口 ${port}`,
|
|
223
|
+
rows: owners.map((owner) => ({
|
|
224
|
+
file: owner.file,
|
|
225
|
+
source: owner.source,
|
|
226
|
+
id: owner.id,
|
|
227
|
+
name: owner.name,
|
|
228
|
+
})),
|
|
229
|
+
strategies: ['change-port'],
|
|
230
|
+
recommended: 'manual',
|
|
231
|
+
severity: 'conflict',
|
|
232
|
+
})
|
|
233
|
+
continue
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
if (busyPorts.has(port)) {
|
|
237
|
+
conflicts.push({
|
|
238
|
+
resource: { type: 'port', key: String(port) },
|
|
239
|
+
owners: [owners[0].owner, '其它进程'],
|
|
240
|
+
detail: `端口 ${port} 已被其它进程占用`,
|
|
241
|
+
rows: [
|
|
242
|
+
{
|
|
243
|
+
file: owners[0].file,
|
|
244
|
+
source: owners[0].source,
|
|
245
|
+
id: owners[0].id,
|
|
246
|
+
name: owners[0].name,
|
|
247
|
+
},
|
|
248
|
+
],
|
|
249
|
+
strategies: ['change-port', 'stop-process'],
|
|
250
|
+
recommended: 'manual',
|
|
251
|
+
severity: 'conflict',
|
|
252
|
+
})
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
return conflicts
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* peer 版本比对。bundles 里每项形如:
|
|
261
|
+
* { name, version, peers: [{ name, range, installed }] }
|
|
262
|
+
* 未安装的 @deepseek-ai/* 视为宿主提供,跳过;其它未安装依赖报告出来。
|
|
263
|
+
*/
|
|
264
|
+
export function detectPeerConflicts(bundles) {
|
|
265
|
+
const groups = new Map()
|
|
266
|
+
|
|
267
|
+
for (const bundle of bundles) {
|
|
268
|
+
for (const peer of bundle.peers ?? []) {
|
|
269
|
+
const list = groups.get(peer.name) ?? []
|
|
270
|
+
list.push({ bundle, peer })
|
|
271
|
+
groups.set(peer.name, list)
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
const conflicts = []
|
|
276
|
+
|
|
277
|
+
for (const [dep, entries] of groups) {
|
|
278
|
+
const installed = entries.find((entry) => entry.peer.installed)?.peer.installed ?? null
|
|
279
|
+
const unsatisfied = entries.filter(
|
|
280
|
+
(entry) => entry.peer.installed && !satisfies(entry.peer.installed, entry.peer.range),
|
|
281
|
+
)
|
|
282
|
+
const missing = entries.filter(
|
|
283
|
+
(entry) =>
|
|
284
|
+
!entry.peer.installed &&
|
|
285
|
+
!HOST_PEER_PREFIXES.some((prefix) => dep.startsWith(prefix)),
|
|
286
|
+
)
|
|
287
|
+
|
|
288
|
+
if (unsatisfied.length > 0) {
|
|
289
|
+
const suggestedVersion =
|
|
290
|
+
unsatisfied
|
|
291
|
+
.map((entry) => minVersionForRange(entry.peer.range))
|
|
292
|
+
.filter(Boolean)
|
|
293
|
+
.sort((left, right) => compareVersions(right, left))[0] ?? null
|
|
294
|
+
|
|
295
|
+
conflicts.push({
|
|
296
|
+
resource: { type: 'peer', key: dep },
|
|
297
|
+
owners: [...new Set(unsatisfied.map((entry) => entry.bundle.name))],
|
|
298
|
+
detail: [
|
|
299
|
+
...unsatisfied.map((entry) => `${entry.bundle.name} 需要 ${entry.peer.range}`),
|
|
300
|
+
`已安装 ${installed}`,
|
|
301
|
+
].join(';'),
|
|
302
|
+
peers: entries.map((entry) => ({
|
|
303
|
+
bundle: entry.bundle.name,
|
|
304
|
+
range: entry.peer.range,
|
|
305
|
+
installed: entry.peer.installed ?? null,
|
|
306
|
+
})),
|
|
307
|
+
suggestedVersion,
|
|
308
|
+
strategies: ['report-only', 'upgrade', 'downgrade', 'peer-override'],
|
|
309
|
+
recommended: 'manual',
|
|
310
|
+
severity: 'conflict',
|
|
311
|
+
})
|
|
312
|
+
continue
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
if (missing.length > 0) {
|
|
316
|
+
const suggestedVersion =
|
|
317
|
+
missing
|
|
318
|
+
.map((entry) => minVersionForRange(entry.peer.range))
|
|
319
|
+
.filter(Boolean)
|
|
320
|
+
.sort((left, right) => compareVersions(right, left))[0] ?? null
|
|
321
|
+
|
|
322
|
+
conflicts.push({
|
|
323
|
+
resource: { type: 'peer', key: dep },
|
|
324
|
+
owners: [...new Set(missing.map((entry) => entry.bundle.name))],
|
|
325
|
+
detail: [
|
|
326
|
+
...missing.map((entry) => `${entry.bundle.name} 需要 ${entry.peer.range}`),
|
|
327
|
+
'未安装',
|
|
328
|
+
].join(';'),
|
|
329
|
+
peers: missing.map((entry) => ({
|
|
330
|
+
bundle: entry.bundle.name,
|
|
331
|
+
range: entry.peer.range,
|
|
332
|
+
installed: null,
|
|
333
|
+
})),
|
|
334
|
+
suggestedVersion,
|
|
335
|
+
strategies: ['report-only', 'install', 'peer-override'],
|
|
336
|
+
recommended: 'manual',
|
|
337
|
+
severity: 'conflict',
|
|
338
|
+
})
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
return conflicts
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/** 同一个 bundle 在 profile 的 bundles 列表里出现多次。 */
|
|
346
|
+
export function detectDuplicateBundles(bundles) {
|
|
347
|
+
const counts = new Map()
|
|
348
|
+
for (const bundle of bundles) {
|
|
349
|
+
counts.set(bundle, (counts.get(bundle) ?? 0) + 1)
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
const conflicts = []
|
|
353
|
+
for (const [key, count] of counts) {
|
|
354
|
+
if (count <= 1) continue
|
|
355
|
+
conflicts.push({
|
|
356
|
+
resource: { type: 'bundle', key },
|
|
357
|
+
owners: bundles.filter((bundle) => bundle === key),
|
|
358
|
+
strategies: ['dedupe'],
|
|
359
|
+
recommended: 'dedupe',
|
|
360
|
+
severity: 'conflict',
|
|
361
|
+
})
|
|
362
|
+
}
|
|
363
|
+
return conflicts
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
export function detectAllConflicts({ rows, bundles, bundleInfo, busyPorts }) {
|
|
367
|
+
return [
|
|
368
|
+
...detectDuplicateBundles(bundles ?? []),
|
|
369
|
+
...detectLoaderEntryConflicts(rows),
|
|
370
|
+
...detectPatchOverrides(rows),
|
|
371
|
+
...detectPeerConflicts(bundleInfo ?? []),
|
|
372
|
+
...detectPortConflicts(rows, { busyPorts }),
|
|
373
|
+
]
|
|
374
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process'
|
|
2
|
+
import fs from 'node:fs'
|
|
3
|
+
import path from 'node:path'
|
|
4
|
+
|
|
5
|
+
function profileDshBin(home, profile) {
|
|
6
|
+
const file = path.join(home, 'profiles', profile, 'node_modules', '@deepseek-ai', 'dsh', 'lib', 'bin.js')
|
|
7
|
+
return fs.existsSync(file) ? { file: process.execPath, prefix: [file] } : null
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function globalDshBin() {
|
|
11
|
+
const explicit = process.env.DSH_BIN
|
|
12
|
+
if (explicit) return { file: explicit, prefix: [] }
|
|
13
|
+
return { file: process.platform === 'win32' ? 'dsh.cmd' : 'dsh', prefix: [] }
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function resolveDshBin(home, profile) {
|
|
17
|
+
return profileDshBin(home, profile) ?? globalDshBin()
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* 调 DSH 自己的 --dump-config 做合并结果预演。
|
|
22
|
+
* 桌面版没有可执行的 dsh CLI 时返回 ok=false,不阻断主流程。
|
|
23
|
+
*/
|
|
24
|
+
export function dumpConfig({ home, profile, dshBin, timeoutMs = 60_000 } = {}) {
|
|
25
|
+
const target = dshBin ? { file: dshBin, prefix: [] } : resolveDshBin(home, profile)
|
|
26
|
+
if (!target) return Promise.resolve({ ok: false, error: 'dsh CLI not found' })
|
|
27
|
+
|
|
28
|
+
return new Promise((resolve) => {
|
|
29
|
+
let output = ''
|
|
30
|
+
let errorOutput = ''
|
|
31
|
+
let child
|
|
32
|
+
try {
|
|
33
|
+
child = spawn(target.file, [...target.prefix, '--profile', profile, '--dump-config'], {
|
|
34
|
+
env: { ...process.env, DSH_HOME: home },
|
|
35
|
+
windowsHide: true,
|
|
36
|
+
shell: process.platform === 'win32' && /\.(cmd|bat)$/i.test(target.file),
|
|
37
|
+
})
|
|
38
|
+
} catch (error) {
|
|
39
|
+
resolve({ ok: false, error: error.message })
|
|
40
|
+
return
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const timer = setTimeout(() => {
|
|
44
|
+
try {
|
|
45
|
+
child.kill()
|
|
46
|
+
} catch {
|
|
47
|
+
// ignore
|
|
48
|
+
}
|
|
49
|
+
resolve({ ok: false, error: `dump-config timed out after ${timeoutMs}ms`, stdout: output })
|
|
50
|
+
}, timeoutMs)
|
|
51
|
+
timer.unref?.()
|
|
52
|
+
|
|
53
|
+
child.stdout?.on('data', (chunk) => {
|
|
54
|
+
output += chunk.toString()
|
|
55
|
+
})
|
|
56
|
+
child.stderr?.on('data', (chunk) => {
|
|
57
|
+
errorOutput += chunk.toString()
|
|
58
|
+
})
|
|
59
|
+
child.on('error', (error) => {
|
|
60
|
+
clearTimeout(timer)
|
|
61
|
+
resolve({ ok: false, error: error.message })
|
|
62
|
+
})
|
|
63
|
+
child.on('close', (code) => {
|
|
64
|
+
clearTimeout(timer)
|
|
65
|
+
if (code === 0) resolve({ ok: true, stdout: output, stderr: errorOutput })
|
|
66
|
+
else resolve({ ok: false, code, error: errorOutput.trim() || `exit ${code}`, stdout: output })
|
|
67
|
+
})
|
|
68
|
+
})
|
|
69
|
+
}
|
package/src/graph.js
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import path from 'node:path'
|
|
2
|
+
import { readJson, readPatch } from './load.js'
|
|
3
|
+
import { profilePackageJson, profilePatch, homePatch } from './paths.js'
|
|
4
|
+
import {
|
|
5
|
+
collectDeclaredPorts,
|
|
6
|
+
defaultProbePort,
|
|
7
|
+
detectAllConflicts,
|
|
8
|
+
} from './detectors.js'
|
|
9
|
+
import { detectSourceConflicts, scanBundleSources } from './sourcescan.js'
|
|
10
|
+
import { scanLogs } from './logscan.js'
|
|
11
|
+
|
|
12
|
+
async function resolveInstalledVersion(profileDir, bundleDir, dep) {
|
|
13
|
+
for (const base of [profileDir, bundleDir]) {
|
|
14
|
+
const pkg = await readJson(
|
|
15
|
+
path.join(base, 'node_modules', ...dep.split('/'), 'package.json'),
|
|
16
|
+
)
|
|
17
|
+
if (pkg?.version) return pkg.version
|
|
18
|
+
}
|
|
19
|
+
return null
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async function loadBundleInfo({ name, profileDir }) {
|
|
23
|
+
const bundleDir = path.join(profileDir, 'node_modules', ...name.split('/'))
|
|
24
|
+
const pkg = await readJson(path.join(bundleDir, 'package.json'))
|
|
25
|
+
const info = {
|
|
26
|
+
name,
|
|
27
|
+
dir: bundleDir,
|
|
28
|
+
version: pkg?.version ?? null,
|
|
29
|
+
patchFile: null,
|
|
30
|
+
patch: null,
|
|
31
|
+
peers: [],
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (!pkg) return info
|
|
35
|
+
|
|
36
|
+
const patchRel = pkg?.dsh?.bundle?.patch
|
|
37
|
+
if (typeof patchRel === 'string') {
|
|
38
|
+
const patchFile = path.resolve(bundleDir, patchRel)
|
|
39
|
+
info.patchFile = patchFile
|
|
40
|
+
try {
|
|
41
|
+
const patch = await readPatch(patchFile)
|
|
42
|
+
if (patch.exists) info.patch = patch
|
|
43
|
+
} catch (error) {
|
|
44
|
+
// 第三方 patch 可能含 !!js 等自定义标签;解析失败只跳过该 bundle,不能让 check 崩掉。
|
|
45
|
+
info.patchError = error.message
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const peerDependencies =
|
|
50
|
+
pkg.peerDependencies && typeof pkg.peerDependencies === 'object'
|
|
51
|
+
? pkg.peerDependencies
|
|
52
|
+
: {}
|
|
53
|
+
|
|
54
|
+
for (const [dep, range] of Object.entries(peerDependencies)) {
|
|
55
|
+
info.peers.push({
|
|
56
|
+
name: dep,
|
|
57
|
+
range: String(range),
|
|
58
|
+
installed: await resolveInstalledVersion(profileDir, bundleDir, dep),
|
|
59
|
+
})
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return info
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async function probeBusyPorts(rows, probePort) {
|
|
66
|
+
const declared = collectDeclaredPorts(rows)
|
|
67
|
+
const ports = [...declared.keys()].slice(0, 32)
|
|
68
|
+
const results = await Promise.all(
|
|
69
|
+
ports.map(async (port) => {
|
|
70
|
+
try {
|
|
71
|
+
return [port, await probePort(port)]
|
|
72
|
+
} catch {
|
|
73
|
+
return [port, false]
|
|
74
|
+
}
|
|
75
|
+
}),
|
|
76
|
+
)
|
|
77
|
+
return new Set(results.filter(([, busy]) => busy).map(([port]) => port))
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export async function loadGraph({
|
|
81
|
+
home,
|
|
82
|
+
profile,
|
|
83
|
+
dir,
|
|
84
|
+
probePort = defaultProbePort,
|
|
85
|
+
scanPorts = true,
|
|
86
|
+
scanLogs: scanLogFiles = true,
|
|
87
|
+
scanSources = true,
|
|
88
|
+
logFile,
|
|
89
|
+
}) {
|
|
90
|
+
const packageJsonPath = profilePackageJson(dir)
|
|
91
|
+
const packageJson = await readJson(packageJsonPath)
|
|
92
|
+
|
|
93
|
+
const bundles = Array.isArray(packageJson?.dsh?.profile?.bundles)
|
|
94
|
+
? packageJson.dsh.profile.bundles
|
|
95
|
+
: []
|
|
96
|
+
|
|
97
|
+
// bundle 自身的 patch 是更早的层;同一 bundle 重复出现只扫描一次,
|
|
98
|
+
// 重复本身由 bundle 冲突单独报告。
|
|
99
|
+
const layers = []
|
|
100
|
+
const bundleInfo = []
|
|
101
|
+
for (const name of [...new Set(bundles)]) {
|
|
102
|
+
const info = await loadBundleInfo({ name, profileDir: dir })
|
|
103
|
+
bundleInfo.push(info)
|
|
104
|
+
if (info.patch) layers.push({ ...info.patch, source: `bundle:${name}` })
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const profilePatchData = await readPatch(profilePatch(dir))
|
|
108
|
+
if (profilePatchData.exists) {
|
|
109
|
+
layers.push({ ...profilePatchData, source: 'profile' })
|
|
110
|
+
}
|
|
111
|
+
const homePatchData = await readPatch(homePatch(home))
|
|
112
|
+
if (homePatchData.exists) {
|
|
113
|
+
layers.push({ ...homePatchData, source: 'home' })
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const rows = []
|
|
117
|
+
for (const layer of layers) {
|
|
118
|
+
for (const row of layer.rows) {
|
|
119
|
+
rows.push({ ...row, file: layer.file, source: layer.source })
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const busyPorts = scanPorts ? await probeBusyPorts(rows, probePort) : new Set()
|
|
124
|
+
const sourceConflicts = scanSources
|
|
125
|
+
? detectSourceConflicts(await scanBundleSources(bundleInfo))
|
|
126
|
+
: []
|
|
127
|
+
const conflicts = [
|
|
128
|
+
...detectAllConflicts({ rows, bundles, bundleInfo, busyPorts }),
|
|
129
|
+
...sourceConflicts,
|
|
130
|
+
]
|
|
131
|
+
const logResult = scanLogFiles
|
|
132
|
+
? await scanLogs({ home, logFile })
|
|
133
|
+
: { file: logFile ?? null, issues: [] }
|
|
134
|
+
|
|
135
|
+
return {
|
|
136
|
+
home,
|
|
137
|
+
profile,
|
|
138
|
+
dir,
|
|
139
|
+
packageJsonPath,
|
|
140
|
+
packageJson,
|
|
141
|
+
bundles,
|
|
142
|
+
bundleInfo,
|
|
143
|
+
patches: layers,
|
|
144
|
+
rows,
|
|
145
|
+
busyPorts: [...busyPorts],
|
|
146
|
+
conflicts,
|
|
147
|
+
logFile: logResult.file,
|
|
148
|
+
logIssues: logResult.issues,
|
|
149
|
+
}
|
|
150
|
+
}
|