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
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import fs from 'node:fs/promises'
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
|
|
4
|
+
const MAX_FILES = 400
|
|
5
|
+
const MAX_FILE_SIZE = 2 * 1024 * 1024
|
|
6
|
+
const SOURCE_RE = /\.(m?js|cjs|ts)$/i
|
|
7
|
+
|
|
8
|
+
const COMMAND_RE =
|
|
9
|
+
/commands\s*\.\s*register\s*\(\s*\{[\s\S]{0,500}?\bname\s*:\s*['"]([a-z][a-z0-9_-]*)['"]/g
|
|
10
|
+
const ADAPTER_ROUTES_RE = /registerAdapter\s*\(\s*\[([^\]]{0,500})\]/g
|
|
11
|
+
const CONFIGURABLE_RE = /registerConfigurableProviders\s*\(\s*\[([\s\S]{0,3000}?)\]/g
|
|
12
|
+
const QUOTED_RE = /['"]([A-Za-z0-9@/_.:-]+)['"]/g
|
|
13
|
+
const CONFIGURABLE_PROVIDER_RE = /\bprovider\s*:\s*['"]([A-Za-z0-9@/_.:-]+)['"]/g
|
|
14
|
+
const SLOT_BLOCK_RE = /\bslots?\s*\.\s*register\s*\(\s*\{([\s\S]{0,600}?)\}\s*,/g
|
|
15
|
+
const SLOT_NAME_RE = /\bname\s*:\s*['"]([A-Za-z0-9_.:-]+)['"]/
|
|
16
|
+
const SLOT_ID_RE = /\bid\s*:\s*['"]([A-Za-z0-9_.:-]+)['"]/
|
|
17
|
+
const LOCK_FILE_RE = /['"]([A-Za-z0-9_.-]+\.lock)['"]/g
|
|
18
|
+
const LOCK_DEP_RE = /['"](proper-lockfile|lockfile|file-lock|single-instance|lockfile-lite)['"]/g
|
|
19
|
+
|
|
20
|
+
async function listSourceFiles(root) {
|
|
21
|
+
const files = []
|
|
22
|
+
const stack = [root]
|
|
23
|
+
while (stack.length > 0 && files.length < MAX_FILES) {
|
|
24
|
+
const dir = stack.pop()
|
|
25
|
+
let entries
|
|
26
|
+
try {
|
|
27
|
+
entries = await fs.readdir(dir, { withFileTypes: true })
|
|
28
|
+
} catch {
|
|
29
|
+
continue
|
|
30
|
+
}
|
|
31
|
+
for (const entry of entries) {
|
|
32
|
+
if (entry.name === 'node_modules' || entry.name === '.git') continue
|
|
33
|
+
const full = path.join(dir, entry.name)
|
|
34
|
+
if (entry.isDirectory()) stack.push(full)
|
|
35
|
+
else if (SOURCE_RE.test(entry.name)) files.push(full)
|
|
36
|
+
if (files.length >= MAX_FILES) break
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return files
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function collect(regex, text, sink) {
|
|
43
|
+
regex.lastIndex = 0
|
|
44
|
+
let match
|
|
45
|
+
while ((match = regex.exec(text))) {
|
|
46
|
+
if (match[1]) sink.add(match[1])
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function addResource(resources, type, key, owner, file) {
|
|
51
|
+
if (!key) return
|
|
52
|
+
const bucket = resources[type] ?? (resources[type] = new Map())
|
|
53
|
+
const owners = bucket.get(key) ?? []
|
|
54
|
+
if (!owners.some((item) => item.owner === owner)) owners.push({ owner, file })
|
|
55
|
+
bucket.set(key, owners)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* 扫描第三方插件源码里“会争抢同名资源”的注册语句。
|
|
60
|
+
* 这是保守的静态证据:只认字符串字面量,变量/动态名字会漏;漏报比误报安全。
|
|
61
|
+
*/
|
|
62
|
+
export async function scanBundleSources(bundleInfo) {
|
|
63
|
+
const resources = { command: new Map(), provider: new Map(), 'ui-slot': new Map(), 'file-lock': new Map() }
|
|
64
|
+
|
|
65
|
+
for (const bundle of bundleInfo) {
|
|
66
|
+
if (!bundle?.dir) continue
|
|
67
|
+
const files = await listSourceFiles(bundle.dir)
|
|
68
|
+
for (const file of files) {
|
|
69
|
+
let text
|
|
70
|
+
try {
|
|
71
|
+
const stat = await fs.stat(file)
|
|
72
|
+
if (stat.size > MAX_FILE_SIZE) continue
|
|
73
|
+
text = await fs.readFile(file, 'utf8')
|
|
74
|
+
} catch {
|
|
75
|
+
continue
|
|
76
|
+
}
|
|
77
|
+
const relative = path.relative(bundle.dir, file)
|
|
78
|
+
|
|
79
|
+
const commands = new Set()
|
|
80
|
+
collect(COMMAND_RE, text, commands)
|
|
81
|
+
for (const name of commands) addResource(resources, 'command', name, bundle.name, relative)
|
|
82
|
+
|
|
83
|
+
const providers = new Set()
|
|
84
|
+
for (const match of text.matchAll(ADAPTER_ROUTES_RE)) {
|
|
85
|
+
const list = new Set()
|
|
86
|
+
collect(QUOTED_RE, match[1], list)
|
|
87
|
+
for (const provider of list) providers.add(provider)
|
|
88
|
+
}
|
|
89
|
+
for (const match of text.matchAll(CONFIGURABLE_RE)) {
|
|
90
|
+
const list = new Set()
|
|
91
|
+
collect(CONFIGURABLE_PROVIDER_RE, match[1], list)
|
|
92
|
+
for (const provider of list) providers.add(provider)
|
|
93
|
+
}
|
|
94
|
+
for (const provider of providers) {
|
|
95
|
+
addResource(resources, 'provider', provider, bundle.name, relative)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const slots = new Set()
|
|
99
|
+
for (const match of text.matchAll(SLOT_BLOCK_RE)) {
|
|
100
|
+
const name = SLOT_NAME_RE.exec(match[1])?.[1]
|
|
101
|
+
const id = SLOT_ID_RE.exec(match[1])?.[1]
|
|
102
|
+
// 没有显式 id 时宿主会生成唯一 id,不构成冲突。
|
|
103
|
+
if (name && id) slots.add(`${name}#${id}`)
|
|
104
|
+
}
|
|
105
|
+
for (const slot of slots) addResource(resources, 'ui-slot', slot, bundle.name, relative)
|
|
106
|
+
|
|
107
|
+
const locks = new Set()
|
|
108
|
+
collect(LOCK_FILE_RE, text, locks)
|
|
109
|
+
collect(LOCK_DEP_RE, text, locks)
|
|
110
|
+
for (const lock of locks) addResource(resources, 'file-lock', lock, bundle.name, relative)
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return resources
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const HINTS = {
|
|
118
|
+
command: '运行期会自动改成 /name__coexistN;也可在 profile patch 里停用其中一个命令',
|
|
119
|
+
provider: '运行期会把后注册的 provider 改成 provider__coexistN;设置页可能仍共用命名空间',
|
|
120
|
+
'ui-slot': 'UI 插槽在浏览器客户端注册,host 插件无法改名;需要停用其中一个或等客户端侧支持',
|
|
121
|
+
'file-lock': '文件锁没有统一注册 API;需要改配置、换存储目录或做进程隔离',
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* 把扫描到的资源按 owner 聚合;只保留两个及以上插件争抢的项。
|
|
126
|
+
* UI 插槽和文件锁只做提示(info),因为它们不能由 host 插件在运行期安全改名。
|
|
127
|
+
*/
|
|
128
|
+
export function detectSourceConflicts(resources) {
|
|
129
|
+
const conflicts = []
|
|
130
|
+
for (const [type, bucket] of Object.entries(resources)) {
|
|
131
|
+
for (const [key, owners] of bucket) {
|
|
132
|
+
if (owners.length < 2) continue
|
|
133
|
+
const runtimeHandled = type === 'command' || type === 'provider'
|
|
134
|
+
conflicts.push({
|
|
135
|
+
resource: { type, key },
|
|
136
|
+
owners: owners.map((item) => item.owner),
|
|
137
|
+
evidence: owners.map((item) => `${item.owner}:${item.file}`),
|
|
138
|
+
strategies: runtimeHandled ? ['runtime-alias'] : ['manual'],
|
|
139
|
+
recommended: runtimeHandled ? 'runtime-alias' : 'manual',
|
|
140
|
+
severity: runtimeHandled ? 'conflict' : 'info',
|
|
141
|
+
detail: HINTS[type],
|
|
142
|
+
hint: HINTS[type],
|
|
143
|
+
})
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return conflicts
|
|
147
|
+
}
|
package/src/state.js
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import crypto from 'node:crypto'
|
|
2
|
+
import fs from 'node:fs/promises'
|
|
3
|
+
import path from 'node:path'
|
|
4
|
+
import { graphFile, plansDir, stateFile } from './paths.js'
|
|
5
|
+
|
|
6
|
+
async function readJson(file) {
|
|
7
|
+
try {
|
|
8
|
+
return JSON.parse(await fs.readFile(file, 'utf8'))
|
|
9
|
+
} catch (err) {
|
|
10
|
+
if (err.code === 'ENOENT') return null
|
|
11
|
+
throw err
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
async function writeJson(file, value) {
|
|
16
|
+
await fs.mkdir(path.dirname(file), { recursive: true })
|
|
17
|
+
const tmp = `${file}.tmp-${process.pid}-${Date.now()}`
|
|
18
|
+
await fs.writeFile(tmp, `${JSON.stringify(value, null, 2)}\n`, 'utf8')
|
|
19
|
+
try {
|
|
20
|
+
await fs.rename(tmp, file)
|
|
21
|
+
} catch (err) {
|
|
22
|
+
if (err.code === 'EEXIST' || err.code === 'EPERM') {
|
|
23
|
+
await fs.rm(file, { force: true })
|
|
24
|
+
await fs.rename(tmp, file)
|
|
25
|
+
return
|
|
26
|
+
}
|
|
27
|
+
await fs.rm(tmp, { force: true })
|
|
28
|
+
throw err
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function planIdFor(plan) {
|
|
33
|
+
const hash = crypto
|
|
34
|
+
.createHash('sha1')
|
|
35
|
+
.update(JSON.stringify(plan.actions ?? []))
|
|
36
|
+
.digest('hex')
|
|
37
|
+
.slice(0, 8)
|
|
38
|
+
return `plan-${Date.now().toString(36)}-${hash}`
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export async function saveGraph(home, graph) {
|
|
42
|
+
const file = graphFile(home)
|
|
43
|
+
await writeJson(file, {
|
|
44
|
+
generatedAt: new Date().toISOString(),
|
|
45
|
+
home: graph.home,
|
|
46
|
+
profile: graph.profile,
|
|
47
|
+
bundles: graph.bundles,
|
|
48
|
+
conflicts: graph.conflicts,
|
|
49
|
+
logFile: graph.logFile,
|
|
50
|
+
logIssues: graph.logIssues,
|
|
51
|
+
})
|
|
52
|
+
return file
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export async function readGraph(home) {
|
|
56
|
+
return readJson(graphFile(home))
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function validateState(state) {
|
|
60
|
+
const errors = []
|
|
61
|
+
if (!state || typeof state !== 'object') errors.push('state must be an object')
|
|
62
|
+
else {
|
|
63
|
+
if (state.version !== 1) errors.push('state.version must be 1')
|
|
64
|
+
if (state.profile !== undefined && (typeof state.profile !== 'string' || state.profile.length === 0)) {
|
|
65
|
+
errors.push('state.profile must be a non-empty string when set')
|
|
66
|
+
}
|
|
67
|
+
if (state.plan !== undefined) {
|
|
68
|
+
if (!state.plan || typeof state.plan !== 'object') {
|
|
69
|
+
errors.push('state.plan must be an object')
|
|
70
|
+
} else if (state.plan.actions !== undefined && !Array.isArray(state.plan.actions)) {
|
|
71
|
+
errors.push('state.plan.actions must be an array when set')
|
|
72
|
+
} else if (state.plan.id === undefined && state.plan.actions === undefined) {
|
|
73
|
+
errors.push('state.plan must include id or actions')
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return { ok: errors.length === 0, errors }
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export async function readState(home) {
|
|
81
|
+
const file = stateFile(home)
|
|
82
|
+
const state = await readJson(file)
|
|
83
|
+
if (!state) return { file, exists: false, state: null, validation: { ok: true, errors: [] } }
|
|
84
|
+
return { file, exists: true, state, validation: validateState(state) }
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export async function writeState(home, patch) {
|
|
88
|
+
const current = (await readState(home)).state ?? { version: 1 }
|
|
89
|
+
const next = {
|
|
90
|
+
...current,
|
|
91
|
+
...patch,
|
|
92
|
+
version: 1,
|
|
93
|
+
updatedAt: new Date().toISOString(),
|
|
94
|
+
}
|
|
95
|
+
const validation = validateState(next)
|
|
96
|
+
if (!validation.ok) {
|
|
97
|
+
throw new Error(`invalid coexist state: ${validation.errors.join('; ')}`)
|
|
98
|
+
}
|
|
99
|
+
const file = stateFile(home)
|
|
100
|
+
await writeJson(file, next)
|
|
101
|
+
return { file, state: next }
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export async function savePlan(home, plan) {
|
|
105
|
+
const stored = {
|
|
106
|
+
...plan,
|
|
107
|
+
id: plan.id ?? planIdFor(plan),
|
|
108
|
+
createdAt: plan.createdAt ?? new Date().toISOString(),
|
|
109
|
+
}
|
|
110
|
+
const file = path.join(plansDir(home), `${stored.id}.json`)
|
|
111
|
+
await writeJson(file, stored)
|
|
112
|
+
await writeState(home, { plan: { id: stored.id, createdAt: stored.createdAt } })
|
|
113
|
+
return { file, plan: stored }
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export async function loadPlan(home, id) {
|
|
117
|
+
if (!id) {
|
|
118
|
+
const state = (await readState(home)).state
|
|
119
|
+
id = state?.plan?.id
|
|
120
|
+
if (!id) return null
|
|
121
|
+
}
|
|
122
|
+
const file = path.join(plansDir(home), `${id}.json`)
|
|
123
|
+
const plan = await readJson(file)
|
|
124
|
+
return plan ? { file, plan } : null
|
|
125
|
+
}
|
package/README.md
DELETED