mirror of
https://github.com/YuJunZhiXue/dsh-purge.git
synced 2026-11-28 10:10:22 +08:00
fix: cordis.patch.yml 为 [] 时替换而非追加,修复宿主启动解析失败(#191)
宿主生成的 profile 层 cordis.patch.yml 可能整体只有一个空 flow 序列 [],
那本身就是一个完整 YAML 文档。旧实现无条件在文件尾追加 `- insert:`,
结果是第二个文档,宿主启动解析报
"end of the stream or a document separator is expected" 直接崩掉
(macOS DSH Desktop 0.2.1-rc.4)。
- 新增 classifyPatchBody() 判断数据部分形态:empty / empty-flow /
block-seq / flow-seq / unknown,只看最后一个文档的根节点
- 空 flow 序列([] 或 [ ],可带 --- 前缀)→ 把整行替换成 insert 块,
绝不追加成第二个文档
- 空文件 / 只有注释和空行 → 写 insert 块,注释保留
- 正常块序列 → 沿用旧的追加行为,前导空行规则不变
- 非空 flow 序列(宿主/用户内容)以及认不准的形态 → 不动,
返回 { ok: false, wrote: false },宁可留着也别写坏
- postinstall 脚本区分「跳过」和「已有」,跳过时打印说明
- profileHasPurgeBodyInsert() 检测逻辑未动
- 新增 9 条用例(形态分类 / 空文件 / 只有注释 / 注释+[] / ---+[] /
块序列追加 / 非空 flow 不写 / 已有本体行零写入 / 替换后幂等),
版本 1.1.83
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
1 parent
f41ecc238c
commit
0ea7872505
5 files changed
+222
-2
No files matched your search
+67
-1
@@ -1,8 +1,11 @@
|
||||
/**
|
||||
* 用户层 profile/cordis.patch.yml 的本体 insert(#154 / #159)。
|
||||
* 用户层 profile/cordis.patch.yml 的本体 insert(#154 / #159 / #191)。
|
||||
*
|
||||
* 包内 overlay 只改已入队行的 inject/config,不能再 insert 一次(Hub 已入队会
|
||||
* already queued)。postinstall 和演练台子行都走这里:行不存在就写一条。
|
||||
*
|
||||
* #191:文件本身是一个完整文档时(宿主生成的空 flow 序列 `[]`),不能再追加,
|
||||
* 追加会变成第二个文档,宿主启动解析失败。
|
||||
*/
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
@@ -47,6 +50,52 @@ export function profileHasPurgeBodyInsert(text) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断 cordis.patch.yml 的数据部分形态(#191)。
|
||||
*
|
||||
* 宿主生成的 profile 层 patch 文件可能整体只有一个空 flow 序列 `[]`,
|
||||
* 那是**一个完整 YAML 文档**。往文件尾追加 `- insert:` 会变成第二个文档,
|
||||
* 宿主启动解析报 "end of the stream or a document separator is expected"。
|
||||
*
|
||||
* 只看最后一个文档的根节点类型(追加内容落在最后一个文档里):
|
||||
* - "empty" 没有数据行(空文件 / 只有注释和空行)
|
||||
* - "empty-flow" 数据部分恰好是空 flow 序列 `[]`(可带 `---` 前缀)
|
||||
* - "block-seq" 正常块序列(顶层 `- ...`),沿用旧的追加行为
|
||||
* - "flow-seq" 非空 flow 序列(宿主/用户内容,动不得)
|
||||
* - "unknown" 其它形态(顶层映射等),认不准就不写
|
||||
*/
|
||||
export function classifyPatchBody(text) {
|
||||
const lines = String(text || "").replace(/\r\n/g, "\n").split("\n");
|
||||
const dataLines = [];
|
||||
for (let i = 0; i < lines.length; i += 1) {
|
||||
const trimmed = lines[i].trim();
|
||||
if (!trimmed) continue; // 空行
|
||||
if (trimmed.startsWith("#")) continue; // 注释行
|
||||
dataLines.push({ index: i, trimmed });
|
||||
}
|
||||
// 按 `---` 切成多个文档,只看最后一个(追加内容会落在最后一个文档里)
|
||||
const lastDoc = [];
|
||||
for (const item of dataLines) {
|
||||
if (item.trimmed === "---") {
|
||||
lastDoc.length = 0;
|
||||
continue;
|
||||
}
|
||||
lastDoc.push(item);
|
||||
}
|
||||
if (lastDoc.length === 0) return { kind: "empty", lines };
|
||||
// 空 flow 序列:`[]` / `[ ]`,允许行尾注释
|
||||
if (lastDoc.length === 1 && /^\[\s*\]\s*(#.*)?$/.test(lastDoc[0].trimmed)) {
|
||||
return { kind: "empty-flow", lines, emptyFlowIndex: lastDoc[0].index };
|
||||
}
|
||||
if (lastDoc[0].trimmed.startsWith("[")) return { kind: "flow-seq", lines };
|
||||
if (/^-(\s|$)/.test(lastDoc[0].trimmed)) {
|
||||
// 块序列里混进顶层 flow 序列 → 本身就是坏 YAML,认不准就不动
|
||||
if (lastDoc.slice(1).some((item) => item.trimmed.startsWith("["))) return { kind: "unknown", lines };
|
||||
return { kind: "block-seq", lines };
|
||||
}
|
||||
return { kind: "unknown", lines };
|
||||
}
|
||||
|
||||
export function ensureProfileBodyInsert(profileDir) {
|
||||
const fp = path.join(profileDir, "cordis.patch.yml");
|
||||
let text = "";
|
||||
@@ -54,6 +103,23 @@ export function ensureProfileBodyInsert(profileDir) {
|
||||
if (profileHasPurgeBodyInsert(text)) {
|
||||
return { ok: true, wrote: false, path: fp };
|
||||
}
|
||||
const shape = classifyPatchBody(text);
|
||||
if (shape.kind === "empty-flow") {
|
||||
// `[]` 本身是一个完整文档:把整行替换成 insert 块,绝不追加成第二个文档(#191)
|
||||
const blockLines = INSERT_BLOCK.replace(/\n$/, "").split("\n");
|
||||
const out = [
|
||||
...shape.lines.slice(0, shape.emptyFlowIndex),
|
||||
...blockLines,
|
||||
...shape.lines.slice(shape.emptyFlowIndex + 1),
|
||||
].join("\n");
|
||||
fs.writeFileSync(fp, `${out.endsWith("\n") ? out : `${out}\n`}`, "utf8");
|
||||
return { ok: true, wrote: true, path: fp };
|
||||
}
|
||||
if (shape.kind === "flow-seq" || shape.kind === "unknown") {
|
||||
// 宿主 / 用户自己的内容,宁可不动也别写坏(#191)
|
||||
return { ok: false, wrote: false, path: fp };
|
||||
}
|
||||
// 空文件 / 只有注释 → 直接写 insert 块;正常块序列 → 沿用旧的追加行为
|
||||
const prefix = text && !text.endsWith("\n") ? "\n" : "";
|
||||
fs.writeFileSync(fp, `${text}${prefix}${text ? "\n" : ""}${INSERT_BLOCK}`, "utf8");
|
||||
return { ok: true, wrote: true, path: fp };
|
||||
|
||||
Reference in new issue
Block a user