dsh-context-kit 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/README.md +120 -3
- package/bin/dsh-context-kit.mjs +138 -0
- package/client/panel.js +302 -0
- package/cordis.patch.yml +21 -0
- package/icon.svg +13 -0
- package/index.js +385 -0
- package/lib/index/events.cjs +90 -0
- package/lib/index/render.cjs +177 -0
- package/lib/index/sources.cjs +286 -0
- package/lib/index/tokens.cjs +48 -0
- package/lib/kit-ops.mjs +230 -0
- package/lib/remote.mjs +55 -0
- package/lib/settings.mjs +69 -0
- package/lib/tuner/sync.mjs +140 -0
- package/lib/tuner/tuner.mjs +647 -0
- package/package.json +65 -6
- package/tuner.js +149 -0
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 把“读上游 + 生成覆盖 + 安全落盘”串成一个可复用流程。
|
|
3
|
+
* 这里不碰 ctx,只接收已解析好的路径与配置,便于单测。
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { existsSync, readFileSync } from 'node:fs'
|
|
7
|
+
import { join } from 'node:path'
|
|
8
|
+
import {
|
|
9
|
+
buildOverride,
|
|
10
|
+
checkPatchShape,
|
|
11
|
+
discoverPresets,
|
|
12
|
+
extractDeclaration,
|
|
13
|
+
findForeignRows,
|
|
14
|
+
parseManagedBlocks,
|
|
15
|
+
renderBlock,
|
|
16
|
+
resolveAppSources,
|
|
17
|
+
spliceBlocks,
|
|
18
|
+
writePatchFile,
|
|
19
|
+
PATCH_FILE,
|
|
20
|
+
} from './tuner.mjs'
|
|
21
|
+
|
|
22
|
+
const isBlank = (line) => line.trim() === ''
|
|
23
|
+
|
|
24
|
+
/** 删除顶层 `- id: preset-<id>` 行(含其后续缩进行),用于 force+replace 接管外来覆盖。 */
|
|
25
|
+
export function stripForeignRows(text, presetIds) {
|
|
26
|
+
const wanted = new Set(presetIds.map((id) => `preset-${id}`))
|
|
27
|
+
const lines = String(text ?? '').split(/\r?\n/)
|
|
28
|
+
const managed = parseManagedBlocks(text)
|
|
29
|
+
const insideManaged = new Set()
|
|
30
|
+
for (const block of managed) for (let i = block.start; i <= block.end; i += 1) insideManaged.add(i)
|
|
31
|
+
|
|
32
|
+
const drop = new Set()
|
|
33
|
+
for (let i = 0; i < lines.length; i += 1) {
|
|
34
|
+
if (insideManaged.has(i)) continue
|
|
35
|
+
const match = /^-\s+id:\s*(\S+)\s*$/.exec(lines[i])
|
|
36
|
+
if (!match || !wanted.has(match[1])) continue
|
|
37
|
+
// 该行起,到下一个顶层行/顶层注释为止。中间允许空行(preset 声明里的
|
|
38
|
+
// 块标量本身就有空行,遇到空行就停会把后半截留成孤儿续行)。
|
|
39
|
+
let lastContent = i
|
|
40
|
+
let j = i + 1
|
|
41
|
+
while (j < lines.length) {
|
|
42
|
+
if (isBlank(lines[j])) {
|
|
43
|
+
j += 1
|
|
44
|
+
continue
|
|
45
|
+
}
|
|
46
|
+
if (lines[j].startsWith('#') || lines[j].startsWith('-')) break
|
|
47
|
+
lastContent = j
|
|
48
|
+
j += 1
|
|
49
|
+
}
|
|
50
|
+
for (let k = i; k <= lastContent; k += 1) drop.add(k)
|
|
51
|
+
}
|
|
52
|
+
if (drop.size === 0) return text
|
|
53
|
+
return lines.filter((_, index) => !drop.has(index)).join('\n')
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* 只做计算不落盘:为每个配置里的预设生成覆盖块,并报告上游漂移/外来冲突。
|
|
58
|
+
* @returns {{blocks: Array, entries: Array, problems: Array<string>, missing: Array<string>}}
|
|
59
|
+
*/
|
|
60
|
+
export function planSync({ sources, presetTweaks, patchText, env = process.env, execPath = process.execPath }) {
|
|
61
|
+
const resolvedSources = sources ?? resolveAppSources({ config: { appRoot: env.DSH_APP_ROOT }, execPath })
|
|
62
|
+
const sourceErrors = []
|
|
63
|
+
const upstream = discoverPresets(resolvedSources, { onError: (message) => sourceErrors.push(message) })
|
|
64
|
+
const blocks = []
|
|
65
|
+
const entries = []
|
|
66
|
+
const problems = []
|
|
67
|
+
const missing = []
|
|
68
|
+
|
|
69
|
+
for (const [presetId, tweaks] of Object.entries(presetTweaks ?? {})) {
|
|
70
|
+
const found = upstream.get(presetId)
|
|
71
|
+
if (!found) {
|
|
72
|
+
missing.push(presetId)
|
|
73
|
+
problems.push(
|
|
74
|
+
`上游找不到预设 "${presetId}"(可能拼写有误,或该 DSH 版本没有这个预设)` +
|
|
75
|
+
(sourceErrors.length > 0 ? `;读取细节:${sourceErrors.slice(0, 3).join(' | ')}` : ''),
|
|
76
|
+
)
|
|
77
|
+
continue
|
|
78
|
+
}
|
|
79
|
+
try {
|
|
80
|
+
const declaration = extractDeclaration(found.text, presetId)
|
|
81
|
+
const lines = buildOverride(declaration.lines, tweaks)
|
|
82
|
+
const rendered = renderBlock({
|
|
83
|
+
presetId,
|
|
84
|
+
upstreamSha: found.sha,
|
|
85
|
+
upstreamPath: found.internalPath,
|
|
86
|
+
declarationLines: lines,
|
|
87
|
+
})
|
|
88
|
+
blocks.push(rendered)
|
|
89
|
+
const existingBlock = parseManagedBlocks(patchText).find((block) => block.presetId === presetId)
|
|
90
|
+
entries.push({
|
|
91
|
+
presetId,
|
|
92
|
+
upstreamSha: found.sha,
|
|
93
|
+
previousSha: existingBlock?.sha,
|
|
94
|
+
drifted: Boolean(existingBlock && existingBlock.sha && existingBlock.sha !== found.sha),
|
|
95
|
+
pluginCount: (found.text.match(/^\s+-\s+id:/gm) ?? []).length,
|
|
96
|
+
upstreamPath: found.internalPath,
|
|
97
|
+
})
|
|
98
|
+
} catch (error) {
|
|
99
|
+
problems.push(`${presetId}: ${error.message}`)
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return { blocks, entries, problems, missing, upstream }
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* 落盘。任何校验不通过都不写文件(失败即放弃,宁可没效果也不弄坏启动)。
|
|
108
|
+
* @returns {{changed: boolean, path: string, backup: string|null, removedForeign: number}}
|
|
109
|
+
*/
|
|
110
|
+
export function commitSync({ profileDir, blocks, presetIds, patchText, force = false, mode = 'append' }) {
|
|
111
|
+
const file = join(profileDir, PATCH_FILE)
|
|
112
|
+
let text = patchText
|
|
113
|
+
if (text === undefined) text = existsSync(file) ? readFileSync(file, 'utf8') : ''
|
|
114
|
+
|
|
115
|
+
const foreign = presetIds.flatMap((id) => findForeignRows(text, id))
|
|
116
|
+
if (foreign.length > 0 && !force) {
|
|
117
|
+
throw new Error(
|
|
118
|
+
`${PATCH_FILE} 里已有手写的 preset 覆盖(第 ${foreign.map((n) => n + 1).join(', ')} 行),` +
|
|
119
|
+
'为避免吞掉你的内容,本次没有写入。先手工删除那几行,或传 force 并选择 mode="replace" 由插件接管。',
|
|
120
|
+
)
|
|
121
|
+
}
|
|
122
|
+
if (foreign.length > 0 && force && mode !== 'replace') {
|
|
123
|
+
throw new Error('force 只配合 mode="replace" 使用:否则会留下两条同 id 的覆盖行')
|
|
124
|
+
}
|
|
125
|
+
let removedForeign = 0
|
|
126
|
+
if (foreign.length > 0 && force) {
|
|
127
|
+
removedForeign = foreign.length
|
|
128
|
+
text = stripForeignRows(text, presetIds)
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const next = spliceBlocks(text, blocks)
|
|
132
|
+
const shapeProblems = checkPatchShape(next)
|
|
133
|
+
if (shapeProblems.length > 0) {
|
|
134
|
+
throw new Error(`生成的补丁没有通过结构校验,已放弃写入:${shapeProblems.join('; ')}`)
|
|
135
|
+
}
|
|
136
|
+
const result = writePatchFile(profileDir, next)
|
|
137
|
+
return { ...result, removedForeign }
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export { PATCH_FILE }
|