dsh-coexist-cli 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/bin/dsh-coexist.js +319 -0
- package/package.json +31 -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/launcher.js
ADDED
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
import fs from 'node:fs/promises'
|
|
2
|
+
import { existsSync, readdirSync, statSync } from 'node:fs'
|
|
3
|
+
import path from 'node:path'
|
|
4
|
+
import { createRequire } from 'node:module'
|
|
5
|
+
import { spawn } from 'node:child_process'
|
|
6
|
+
import { fileURLToPath, pathToFileURL } from 'node:url'
|
|
7
|
+
import { coexistDir, resolveHome, resolveProfile } from './paths.js'
|
|
8
|
+
import { loadGraph } from './graph.js'
|
|
9
|
+
import { buildPlan } from './plan.js'
|
|
10
|
+
import { applyPlan } from './apply.js'
|
|
11
|
+
|
|
12
|
+
export const RUN_HELP = `dsh-coexist run [launcher options] -- <dsh args...>
|
|
13
|
+
|
|
14
|
+
Boot DSH with the coexist preload installed before any plugin loads.
|
|
15
|
+
By default it auto-detects the DSH CLI and auto-detects duplicate tools.
|
|
16
|
+
|
|
17
|
+
Launcher options:
|
|
18
|
+
--config <file> optional config (default: $DSH_HOME/coexist/config.json)
|
|
19
|
+
--watch <names> debug override; default is auto-detect duplicates
|
|
20
|
+
--dsh-bin <file> debug override; default is auto-detect the DSH CLI
|
|
21
|
+
--home <dir> DSH_HOME
|
|
22
|
+
--yes apply the preflight repair plan before boot
|
|
23
|
+
--no-preflight skip the pre-boot scan/plan
|
|
24
|
+
-h, --help show this help
|
|
25
|
+
|
|
26
|
+
Everything after -- is passed to DSH.`
|
|
27
|
+
|
|
28
|
+
export function parseRunArgs(argv) {
|
|
29
|
+
const options = {
|
|
30
|
+
config: null,
|
|
31
|
+
dshBin: null,
|
|
32
|
+
watch: null,
|
|
33
|
+
home: null,
|
|
34
|
+
yes: false,
|
|
35
|
+
preflight: true,
|
|
36
|
+
help: false,
|
|
37
|
+
dshArgs: [],
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
let index = 0
|
|
41
|
+
for (; index < argv.length; index += 1) {
|
|
42
|
+
const arg = argv[index]
|
|
43
|
+
if (arg === '--') {
|
|
44
|
+
index += 1
|
|
45
|
+
break
|
|
46
|
+
}
|
|
47
|
+
if (arg === '--help' || arg === '-h') options.help = true
|
|
48
|
+
else if (arg === '--config') options.config = argv[++index]
|
|
49
|
+
else if (arg === '--dsh-bin') options.dshBin = argv[++index]
|
|
50
|
+
else if (arg === '--watch') options.watch = argv[++index]
|
|
51
|
+
else if (arg === '--home') options.home = argv[++index]
|
|
52
|
+
else if (arg === '--yes') options.yes = true
|
|
53
|
+
else if (arg === '--no-preflight') options.preflight = false
|
|
54
|
+
else options.dshArgs.push(arg)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
options.dshArgs.push(...argv.slice(index))
|
|
58
|
+
return options
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function normalizeWatch(value) {
|
|
62
|
+
if (Array.isArray(value)) {
|
|
63
|
+
return value.map((name) => String(name).trim()).filter(Boolean)
|
|
64
|
+
}
|
|
65
|
+
if (typeof value === 'string') {
|
|
66
|
+
return value.split(',').map((name) => name.trim()).filter(Boolean)
|
|
67
|
+
}
|
|
68
|
+
return []
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export async function loadConfig(home, configPath) {
|
|
72
|
+
const file = configPath
|
|
73
|
+
? path.resolve(configPath)
|
|
74
|
+
: path.join(coexistDir(home), 'config.json')
|
|
75
|
+
|
|
76
|
+
try {
|
|
77
|
+
const text = await fs.readFile(file, 'utf8')
|
|
78
|
+
return { file, config: JSON.parse(text) }
|
|
79
|
+
} catch (err) {
|
|
80
|
+
if (err.code === 'ENOENT') return { file, config: {} }
|
|
81
|
+
throw new Error(`Failed to read coexist config ${file}: ${err.message}`)
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export function findDshBin() {
|
|
86
|
+
const candidates = []
|
|
87
|
+
const push = (value) => {
|
|
88
|
+
if (value) candidates.push(value)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
push(process.env.DSH_BIN)
|
|
92
|
+
push(process.env.DSH_COEXIST_DSH_BIN)
|
|
93
|
+
|
|
94
|
+
try {
|
|
95
|
+
push(createRequire(import.meta.url).resolve('@deepseek-ai/dsh/lib/bin.js'))
|
|
96
|
+
} catch {
|
|
97
|
+
// The CLI may not be installed next to DSH; keep looking.
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (process.platform === 'win32' && process.env.APPDATA) {
|
|
101
|
+
push(
|
|
102
|
+
path.join(
|
|
103
|
+
process.env.APPDATA,
|
|
104
|
+
'npm',
|
|
105
|
+
'node_modules',
|
|
106
|
+
'@deepseek-ai',
|
|
107
|
+
'dsh',
|
|
108
|
+
'lib',
|
|
109
|
+
'bin.js',
|
|
110
|
+
),
|
|
111
|
+
)
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const npxRoot =
|
|
115
|
+
process.platform === 'win32'
|
|
116
|
+
? path.join(process.env.LOCALAPPDATA ?? '', 'npm-cache', '_npx')
|
|
117
|
+
: path.join(process.env.HOME ?? '', '.npm', '_npx')
|
|
118
|
+
|
|
119
|
+
try {
|
|
120
|
+
for (const entry of readdirSync(npxRoot, { withFileTypes: true })) {
|
|
121
|
+
if (!entry.isDirectory()) continue
|
|
122
|
+
push(
|
|
123
|
+
path.join(
|
|
124
|
+
npxRoot,
|
|
125
|
+
entry.name,
|
|
126
|
+
'node_modules',
|
|
127
|
+
'@deepseek-ai',
|
|
128
|
+
'dsh',
|
|
129
|
+
'lib',
|
|
130
|
+
'bin.js',
|
|
131
|
+
),
|
|
132
|
+
)
|
|
133
|
+
}
|
|
134
|
+
} catch {
|
|
135
|
+
// No npx cache on this machine.
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const existing = candidates.filter((candidate) => {
|
|
139
|
+
try {
|
|
140
|
+
return existsSync(candidate)
|
|
141
|
+
} catch {
|
|
142
|
+
return false
|
|
143
|
+
}
|
|
144
|
+
})
|
|
145
|
+
if (existing.length === 0) return null
|
|
146
|
+
|
|
147
|
+
existing.sort((left, right) => {
|
|
148
|
+
try {
|
|
149
|
+
return statSync(right).mtimeMs - statSync(left).mtimeMs
|
|
150
|
+
} catch {
|
|
151
|
+
return 0
|
|
152
|
+
}
|
|
153
|
+
})
|
|
154
|
+
return existing[0]
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export function resolveDshBin(explicit) {
|
|
158
|
+
const candidate = explicit ?? findDshBin()
|
|
159
|
+
if (!candidate) {
|
|
160
|
+
throw new Error(
|
|
161
|
+
'Could not find the DSH CLI. Install @deepseek-ai/dsh, or pass --dsh-bin as a debug override.',
|
|
162
|
+
)
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const resolved = path.resolve(candidate)
|
|
166
|
+
if (!existsSync(resolved)) {
|
|
167
|
+
throw new Error(`DSH CLI not found: ${resolved}`)
|
|
168
|
+
}
|
|
169
|
+
return resolved
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
export function resolveToolsEntry(dshBin) {
|
|
173
|
+
const requireFromDsh = createRequire(dshBin)
|
|
174
|
+
return requireFromDsh.resolve('@deepseek-ai/dsh-tools')
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function inferProfile(args = []) {
|
|
178
|
+
const index = args.indexOf('--profile')
|
|
179
|
+
if (index !== -1 && args[index + 1]) return args[index + 1]
|
|
180
|
+
const first = args.find((arg) => !arg.startsWith('-'))
|
|
181
|
+
if (first && /^[a-z][a-z0-9_-]*$/i.test(first)) return first
|
|
182
|
+
return 'web'
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
async function runPreflight(home, options) {
|
|
186
|
+
const profile = inferProfile(options.dshArgs)
|
|
187
|
+
try {
|
|
188
|
+
const graph = await loadGraph({
|
|
189
|
+
home,
|
|
190
|
+
...resolveProfile(home, profile),
|
|
191
|
+
scanPorts: false,
|
|
192
|
+
scanLogs: false,
|
|
193
|
+
scanSources: true,
|
|
194
|
+
})
|
|
195
|
+
const plan = buildPlan(graph)
|
|
196
|
+
const auto = plan.actions.filter((action) => action.kind !== 'manual')
|
|
197
|
+
|
|
198
|
+
if (graph.conflicts.length === 0) {
|
|
199
|
+
console.log('[coexist] preflight: no conflicts')
|
|
200
|
+
return
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
console.log(
|
|
204
|
+
`[coexist] preflight: ${graph.conflicts.length} conflict(s), ${auto.length} automatic repair(s)`,
|
|
205
|
+
)
|
|
206
|
+
if (options.yes && auto.length > 0) {
|
|
207
|
+
const result = await applyPlan({ home, graph, plan })
|
|
208
|
+
console.log(
|
|
209
|
+
`[coexist] preflight applied ${result.applied.length} action(s); backup ${result.backup}`,
|
|
210
|
+
)
|
|
211
|
+
} else {
|
|
212
|
+
console.log('[coexist] preflight is read-only; re-run with --yes to apply repairs')
|
|
213
|
+
}
|
|
214
|
+
} catch (error) {
|
|
215
|
+
console.warn(`[coexist] preflight skipped: ${error.message}`)
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
export async function runDsh(options = {}) {
|
|
220
|
+
const home = resolveHome(options.home)
|
|
221
|
+
const { file: configFile, config } = await loadConfig(home, options.config)
|
|
222
|
+
const watch = options.watch ? normalizeWatch(options.watch) : normalizeWatch(config.watch)
|
|
223
|
+
|
|
224
|
+
if (options.preflight !== false) {
|
|
225
|
+
await runPreflight(home, options)
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
const dshBin = resolveDshBin(options.dshBin)
|
|
229
|
+
const toolsEntry = resolveToolsEntry(dshBin)
|
|
230
|
+
const preloadPath = fileURLToPath(new URL('../preload.mjs', import.meta.url))
|
|
231
|
+
|
|
232
|
+
const env = {
|
|
233
|
+
...process.env,
|
|
234
|
+
DSH_COEXIST_WATCH: watch.join(','),
|
|
235
|
+
DSH_COEXIST_TOOLS_PATH: toolsEntry,
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
const child = spawn(
|
|
239
|
+
process.execPath,
|
|
240
|
+
['--import', pathToFileURL(preloadPath).href, dshBin, ...options.dshArgs],
|
|
241
|
+
{ stdio: 'inherit', env },
|
|
242
|
+
)
|
|
243
|
+
|
|
244
|
+
const forward = (signal) => {
|
|
245
|
+
if (!child.killed) child.kill(signal)
|
|
246
|
+
}
|
|
247
|
+
process.on('SIGINT', forward)
|
|
248
|
+
process.on('SIGTERM', forward)
|
|
249
|
+
|
|
250
|
+
return new Promise((resolve, reject) => {
|
|
251
|
+
child.on('error', reject)
|
|
252
|
+
child.on('exit', (code, signal) => {
|
|
253
|
+
process.off('SIGINT', forward)
|
|
254
|
+
process.off('SIGTERM', forward)
|
|
255
|
+
if (signal) {
|
|
256
|
+
resolve(signal === 'SIGINT' ? 130 : 143)
|
|
257
|
+
} else {
|
|
258
|
+
resolve(code ?? 0)
|
|
259
|
+
}
|
|
260
|
+
})
|
|
261
|
+
})
|
|
262
|
+
}
|
package/src/load.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import fs from 'node:fs/promises'
|
|
2
|
+
import YAML from 'yaml'
|
|
3
|
+
|
|
4
|
+
export async function readJson(file) {
|
|
5
|
+
try {
|
|
6
|
+
return JSON.parse(await fs.readFile(file, 'utf8'))
|
|
7
|
+
} catch (err) {
|
|
8
|
+
if (err.code === 'ENOENT') return null
|
|
9
|
+
throw err
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export async function readText(file) {
|
|
14
|
+
try {
|
|
15
|
+
return await fs.readFile(file, 'utf8')
|
|
16
|
+
} catch (err) {
|
|
17
|
+
if (err.code === 'ENOENT') return null
|
|
18
|
+
throw err
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function flattenPatch(doc) {
|
|
23
|
+
const rows = []
|
|
24
|
+
if (!Array.isArray(doc)) return rows
|
|
25
|
+
|
|
26
|
+
for (const item of doc) {
|
|
27
|
+
if (!item || typeof item !== 'object') continue
|
|
28
|
+
|
|
29
|
+
const operation = Array.isArray(item.insert) || item.insert
|
|
30
|
+
? 'insert'
|
|
31
|
+
: Array.isArray(item.replace) || item.replace
|
|
32
|
+
? 'replace'
|
|
33
|
+
: null
|
|
34
|
+
|
|
35
|
+
if (operation) {
|
|
36
|
+
const list = operation === 'insert' ? item.insert : item.replace
|
|
37
|
+
const items = Array.isArray(list) ? list : [list]
|
|
38
|
+
for (const row of items) {
|
|
39
|
+
if (row && typeof row === 'object') {
|
|
40
|
+
rows.push({ op: operation, ...row })
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
continue
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (typeof item.id === 'string') {
|
|
47
|
+
rows.push({ op: 'row', ...item })
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return rows
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export async function readPatch(file) {
|
|
55
|
+
const text = await readText(file)
|
|
56
|
+
if (text == null) {
|
|
57
|
+
return { file, exists: false, text: '', rows: [] }
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
let doc
|
|
61
|
+
try {
|
|
62
|
+
doc = YAML.parse(text)
|
|
63
|
+
} catch (err) {
|
|
64
|
+
throw new Error(`Failed to parse YAML: ${file}: ${err.message}`)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return { file, exists: true, text, rows: flattenPatch(doc) }
|
|
68
|
+
}
|
package/src/logscan.js
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import fs from 'node:fs/promises'
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
|
|
4
|
+
const MAX_BYTES = 2 * 1024 * 1024
|
|
5
|
+
const MAX_ISSUES = 100
|
|
6
|
+
|
|
7
|
+
/** 在 $DSH_HOME/logs 里找最近的 .log 文件。 */
|
|
8
|
+
export async function findLogFile(home) {
|
|
9
|
+
const dir = path.join(home, 'logs')
|
|
10
|
+
try {
|
|
11
|
+
const names = await fs.readdir(dir)
|
|
12
|
+
const files = []
|
|
13
|
+
for (const name of names) {
|
|
14
|
+
if (!name.endsWith('.log')) continue
|
|
15
|
+
const file = path.join(dir, name)
|
|
16
|
+
const stat = await fs.stat(file).catch(() => null)
|
|
17
|
+
if (stat?.isFile()) files.push({ file, mtime: stat.mtimeMs })
|
|
18
|
+
}
|
|
19
|
+
files.sort((left, right) => right.mtime - left.mtime)
|
|
20
|
+
return files[0]?.file ?? null
|
|
21
|
+
} catch {
|
|
22
|
+
return null
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function matchLine(line) {
|
|
27
|
+
const service = /service "([^"]+)" has been registered at <([^>]+)>/.exec(line)
|
|
28
|
+
if (service) {
|
|
29
|
+
return {
|
|
30
|
+
type: 'service',
|
|
31
|
+
key: service[1],
|
|
32
|
+
owners: [service[2]],
|
|
33
|
+
detail: line.trim(),
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const tool = /tool "([^"]+)" is already registered/.exec(line)
|
|
38
|
+
if (tool) {
|
|
39
|
+
return {
|
|
40
|
+
type: 'tool',
|
|
41
|
+
key: tool[1],
|
|
42
|
+
owners: [],
|
|
43
|
+
detail: line.trim(),
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const loader = /duplicate loader entry id[^\n]*?["']?([A-Za-z0-9_.-]+)/i.exec(line)
|
|
48
|
+
if (loader) {
|
|
49
|
+
return {
|
|
50
|
+
type: 'loader-entry',
|
|
51
|
+
key: loader[1],
|
|
52
|
+
owners: [],
|
|
53
|
+
detail: line.trim(),
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (/EADDRINUSE/i.test(line)) {
|
|
58
|
+
const port =
|
|
59
|
+
/address already in use[^\n]*:(\d{2,5})\b/i.exec(line) ??
|
|
60
|
+
/EADDRINUSE[^\n]*:(\d{2,5})\b/i.exec(line) ??
|
|
61
|
+
/port\s+(\d{2,5})\b/i.exec(line)
|
|
62
|
+
return {
|
|
63
|
+
type: 'port',
|
|
64
|
+
key: port ? port[1] : '(unknown)',
|
|
65
|
+
owners: [],
|
|
66
|
+
detail: line.trim(),
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (/(ERESOLVE|peer dependency|peerDependencies|peer dep)/i.test(line)) {
|
|
71
|
+
return {
|
|
72
|
+
type: 'peer',
|
|
73
|
+
key: '(peer)',
|
|
74
|
+
owners: [],
|
|
75
|
+
detail: line.trim(),
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return null
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* 扫描 DSH 运行日志里的已知冲突关键词。
|
|
84
|
+
* 只作为证据展示,不直接生成修复计划。
|
|
85
|
+
*/
|
|
86
|
+
export async function scanLogs({ home, logFile } = {}) {
|
|
87
|
+
const file = logFile ?? (await findLogFile(home))
|
|
88
|
+
if (!file) return { file: null, issues: [] }
|
|
89
|
+
|
|
90
|
+
let text
|
|
91
|
+
try {
|
|
92
|
+
text = await fs.readFile(file, 'utf8')
|
|
93
|
+
} catch {
|
|
94
|
+
return { file, issues: [] }
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (text.length > MAX_BYTES) text = text.slice(-MAX_BYTES)
|
|
98
|
+
|
|
99
|
+
const issues = []
|
|
100
|
+
const seen = new Set()
|
|
101
|
+
const lines = text.split(/\r?\n/)
|
|
102
|
+
|
|
103
|
+
for (let index = 0; index < lines.length && issues.length < MAX_ISSUES; index += 1) {
|
|
104
|
+
const issue = matchLine(lines[index])
|
|
105
|
+
if (!issue) continue
|
|
106
|
+
const key = `${issue.type}:${issue.key}:${issue.detail}`
|
|
107
|
+
if (seen.has(key)) continue
|
|
108
|
+
seen.add(key)
|
|
109
|
+
issues.push({ ...issue, file, line: index + 1 })
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return { file, issues }
|
|
113
|
+
}
|
package/src/paths.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import os from 'node:os'
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
|
|
4
|
+
export function resolveHome(home) {
|
|
5
|
+
return path.resolve(home ?? process.env.DSH_HOME ?? path.join(os.homedir(), '.dsh'))
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export function resolveProfile(home, profile = 'web') {
|
|
9
|
+
return {
|
|
10
|
+
home,
|
|
11
|
+
profile,
|
|
12
|
+
dir: path.join(home, 'profiles', profile),
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function profilePackageJson(dir) {
|
|
17
|
+
return path.join(dir, 'package.json')
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function profilePatch(dir) {
|
|
21
|
+
return path.join(dir, 'cordis.patch.yml')
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function homePatch(home) {
|
|
25
|
+
return path.join(home, 'cordis.patch.yml')
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function coexistDir(home) {
|
|
29
|
+
return path.join(home, 'coexist')
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function backupsDir(home) {
|
|
33
|
+
return path.join(coexistDir(home), 'backups')
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function reportFile(home) {
|
|
37
|
+
return path.join(coexistDir(home), 'report.json')
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function auditFile(home) {
|
|
41
|
+
return path.join(coexistDir(home), 'audit.jsonl')
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function stateFile(home) {
|
|
45
|
+
return path.join(coexistDir(home), 'state.json')
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function graphFile(home) {
|
|
49
|
+
return path.join(coexistDir(home), 'graph.json')
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function plansDir(home) {
|
|
53
|
+
return path.join(coexistDir(home), 'plans')
|
|
54
|
+
}
|
package/src/plan.js
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import YAML from 'yaml'
|
|
2
|
+
import { collectDeclaredPorts } from './detectors.js'
|
|
3
|
+
import { profilePatch } from './paths.js'
|
|
4
|
+
|
|
5
|
+
function isEditableSource(source) {
|
|
6
|
+
return source === 'profile' || source === 'home'
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function lastEditablePatchLayer(graph) {
|
|
10
|
+
const layers = (graph.patches ?? []).filter((layer) => isEditableSource(layer.source))
|
|
11
|
+
return layers[layers.length - 1] ?? null
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function nextFreePort(base, used) {
|
|
15
|
+
for (let offset = 1; offset <= 100; offset += 1) {
|
|
16
|
+
const candidate = base + offset
|
|
17
|
+
if (candidate > 65535) break
|
|
18
|
+
if (used.has(candidate)) continue
|
|
19
|
+
return candidate
|
|
20
|
+
}
|
|
21
|
+
return null
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function manualHint(conflict) {
|
|
25
|
+
switch (conflict.resource.type) {
|
|
26
|
+
case 'patch-override':
|
|
27
|
+
return 'DSH 的 patch 是整行替换;在更后层显式写完整 config,或调整 patch 顺序'
|
|
28
|
+
case 'patch-dangling-replace':
|
|
29
|
+
return 'replace 的目标 id 在更早层不存在;检查拼写,或改成 insert'
|
|
30
|
+
case 'port':
|
|
31
|
+
return '改掉其中一个插件的端口,或先结束占用该端口的进程'
|
|
32
|
+
case 'peer':
|
|
33
|
+
return '对齐已安装版本;显式传入 --allow-peer-override 可写入受控 override(之后需要重新安装)'
|
|
34
|
+
case 'command':
|
|
35
|
+
return '运行期会自动改成 /name__coexistN;也可以在 profile patch 里停用其中一个命令'
|
|
36
|
+
case 'provider':
|
|
37
|
+
return '运行期会把后注册的 provider 改成 provider__coexistN;设置页可能仍共用命名空间'
|
|
38
|
+
case 'ui-slot':
|
|
39
|
+
return 'UI 插槽在浏览器客户端注册,host 插件无法改名;需要停用其中一个或等客户端侧支持'
|
|
40
|
+
case 'file-lock':
|
|
41
|
+
return '文件锁没有统一注册 API;需要改配置、换存储目录或做进程隔离'
|
|
42
|
+
default:
|
|
43
|
+
return null
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function buildPlan(graph, options = {}) {
|
|
48
|
+
const actions = []
|
|
49
|
+
|
|
50
|
+
for (const conflict of graph.conflicts) {
|
|
51
|
+
if (conflict.severity === 'info') continue
|
|
52
|
+
|
|
53
|
+
if (conflict.resource.type === 'loader-entry') {
|
|
54
|
+
const rows = conflict.rows ?? []
|
|
55
|
+
const kept = rows[0]
|
|
56
|
+
if (!kept) continue
|
|
57
|
+
|
|
58
|
+
const blocked = rows.slice(1).filter((row) => !isEditableSource(row.source))
|
|
59
|
+
if (blocked.length > 0) {
|
|
60
|
+
const layer = lastEditablePatchLayer(graph)
|
|
61
|
+
const file = layer?.file ?? profilePatch(graph.dir)
|
|
62
|
+
actions.push({
|
|
63
|
+
kind: 'disable-loader-row',
|
|
64
|
+
file,
|
|
65
|
+
oldId: conflict.resource.key,
|
|
66
|
+
owner: blocked[0].name ?? null,
|
|
67
|
+
conflict,
|
|
68
|
+
})
|
|
69
|
+
for (const row of blocked) {
|
|
70
|
+
actions.push({
|
|
71
|
+
kind: 'clone-loader-row',
|
|
72
|
+
file,
|
|
73
|
+
oldId: conflict.resource.key,
|
|
74
|
+
newId: nextId(conflict.resource.key, graph, actions),
|
|
75
|
+
name: row.name ?? null,
|
|
76
|
+
config: row.config,
|
|
77
|
+
disabled: row.disabled,
|
|
78
|
+
owner: row.name ?? null,
|
|
79
|
+
conflict,
|
|
80
|
+
})
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const occurrenceByFile = new Map()
|
|
85
|
+
const blockedSet = new Set(blocked)
|
|
86
|
+
for (const row of rows) {
|
|
87
|
+
const occurrence = (occurrenceByFile.get(row.file) ?? 0) + 1
|
|
88
|
+
occurrenceByFile.set(row.file, occurrence)
|
|
89
|
+
|
|
90
|
+
if (row === kept) continue
|
|
91
|
+
if (blockedSet.has(row)) continue
|
|
92
|
+
|
|
93
|
+
actions.push({
|
|
94
|
+
kind: 'rename-loader-id',
|
|
95
|
+
file: row.file,
|
|
96
|
+
oldId: conflict.resource.key,
|
|
97
|
+
newId: nextId(conflict.resource.key, graph, actions),
|
|
98
|
+
occurrence,
|
|
99
|
+
owner: row.name,
|
|
100
|
+
kept: kept.name,
|
|
101
|
+
})
|
|
102
|
+
}
|
|
103
|
+
} else if (conflict.resource.type === 'bundle') {
|
|
104
|
+
actions.push({
|
|
105
|
+
kind: 'dedupe-bundles',
|
|
106
|
+
file: graph.packageJsonPath,
|
|
107
|
+
key: conflict.resource.key,
|
|
108
|
+
remove: Math.max(0, conflict.owners.length - 1),
|
|
109
|
+
})
|
|
110
|
+
} else if (conflict.resource.type === 'port') {
|
|
111
|
+
const rows = conflict.rows ?? []
|
|
112
|
+
const editable = rows.filter((row) => row.id && isEditableSource(row.source))
|
|
113
|
+
const busyOnly = (conflict.owners ?? []).includes('其它进程')
|
|
114
|
+
const base = Number(conflict.resource.key)
|
|
115
|
+
|
|
116
|
+
if (editable.length === rows.length && editable.length > 0 && Number.isInteger(base)) {
|
|
117
|
+
const used = new Set([
|
|
118
|
+
...collectDeclaredPorts(graph.rows ?? []).keys(),
|
|
119
|
+
...(graph.busyPorts ?? []),
|
|
120
|
+
])
|
|
121
|
+
// 保留第一个声明者,其余全部换端口;单行且被系统占用时直接换它。
|
|
122
|
+
const targets = busyOnly ? editable : editable.slice(1)
|
|
123
|
+
let changed = 0
|
|
124
|
+
|
|
125
|
+
for (const target of targets) {
|
|
126
|
+
const newPort = nextFreePort(base, used)
|
|
127
|
+
if (!newPort) break
|
|
128
|
+
used.add(newPort)
|
|
129
|
+
actions.push({
|
|
130
|
+
kind: 'change-port',
|
|
131
|
+
file: target.file,
|
|
132
|
+
id: target.id,
|
|
133
|
+
owner: target.name ?? target.id,
|
|
134
|
+
oldPort: base,
|
|
135
|
+
newPort,
|
|
136
|
+
})
|
|
137
|
+
changed += 1
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (changed === 0) {
|
|
141
|
+
actions.push({ kind: 'manual', conflict, hint: manualHint(conflict) })
|
|
142
|
+
}
|
|
143
|
+
} else {
|
|
144
|
+
actions.push({ kind: 'manual', conflict, hint: manualHint(conflict) })
|
|
145
|
+
}
|
|
146
|
+
} else if (conflict.resource.type === 'patch-override') {
|
|
147
|
+
const suggested = conflict.suggestedPatch
|
|
148
|
+
const layer = lastEditablePatchLayer(graph)
|
|
149
|
+
|
|
150
|
+
if (suggested && layer) {
|
|
151
|
+
actions.push({
|
|
152
|
+
kind: 'append-patch',
|
|
153
|
+
file: layer.file,
|
|
154
|
+
targetId: suggested.id,
|
|
155
|
+
yaml: YAML.stringify([{ replace: [suggested] }]),
|
|
156
|
+
reason: 'patch-override',
|
|
157
|
+
})
|
|
158
|
+
} else {
|
|
159
|
+
actions.push({ kind: 'manual', conflict, hint: manualHint(conflict) })
|
|
160
|
+
}
|
|
161
|
+
} else if (conflict.resource.type === 'peer') {
|
|
162
|
+
if (options.allowPeerOverride && conflict.suggestedVersion) {
|
|
163
|
+
actions.push({
|
|
164
|
+
kind: 'peer-override',
|
|
165
|
+
file: graph.packageJsonPath,
|
|
166
|
+
dep: conflict.resource.key,
|
|
167
|
+
version: conflict.suggestedVersion,
|
|
168
|
+
})
|
|
169
|
+
} else {
|
|
170
|
+
actions.push({ kind: 'manual', conflict, hint: manualHint(conflict) })
|
|
171
|
+
}
|
|
172
|
+
} else {
|
|
173
|
+
actions.push({
|
|
174
|
+
kind: 'manual',
|
|
175
|
+
conflict,
|
|
176
|
+
hint: manualHint(conflict),
|
|
177
|
+
})
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return {
|
|
182
|
+
home: graph.home,
|
|
183
|
+
profile: graph.profile,
|
|
184
|
+
conflicts: graph.conflicts,
|
|
185
|
+
actions,
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function nextId(base, graph, actions) {
|
|
190
|
+
const used = new Set(graph.rows.map((row) => row.id).filter(Boolean))
|
|
191
|
+
for (const action of actions) {
|
|
192
|
+
if (action.newId) used.add(action.newId)
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
for (const suffix of 'bcdefghijklmnopqrstuvwxyz') {
|
|
196
|
+
const candidate = `${base}-${suffix}`
|
|
197
|
+
if (!used.has(candidate)) return candidate
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
let index = 2
|
|
201
|
+
while (used.has(`${base}-${index}`)) index += 1
|
|
202
|
+
return `${base}-${index}`
|
|
203
|
+
}
|