1.1.80: 修复 bug,修复稳定性(#168 #169 #170 #172 #176)

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
叫我小杨同学andCursor committed 2026-10-27 23:34:55 +08:00
1 parent 24f4a7f5df
commit 9a8e8481e8
10 files changed
+178 -40

No files matched your search

+87 -13
View File
@@ -105,24 +105,98 @@ export function ensureProfileBundles(profileDir) {
return { ok: true, wrote: true, path: fp };
}
function readPurgeTarball() {
try {
const pkg = JSON.parse(fs.readFileSync(new URL("../package.json", import.meta.url), "utf8"));
return String(pkg?.dsh?.install?.tarball || "").trim();
} catch {
return "https://codeload.github.com/YuJunZhiXue/dsh-purge/tar.gz/refs/heads/master";
}
}
/** pnpm 12 会给 tarball 单独写一行 `dsh-purge@https://...: set this to true or false`。只写包名不够。 */
export function purgePnpmBuildSpecs() {
const specs = ["dsh-purge"];
const tarball = readPurgeTarball();
if (tarball) specs.push(`${PURGE_NAME}@${tarball}`);
return specs;
}
function isPurgeAllowBuildKey(key) {
const name = String(key || "").replace(/^['"]|['"]$/g, "");
return name === PURGE_NAME || name.startsWith(`${PURGE_NAME}@`);
}
function parseAllowBuildLine(line) {
const m = String(line || "").match(/^[ \t]+(?:"([^"]+)"|'([^']+)'|(\S+?))[ \t]*:[ \t]*(.*?)\s*$/);
if (!m) return null;
return { key: m[1] || m[2] || m[3], value: String(m[4] || "").trim() };
}
function formatAllowBuildLine(key) {
return ` ${key}: true`;
}
/** 把 dsh-purge 以及 `dsh-purge@tarball` 都写成 true,避免装别的插件还报 ERR_PNPM_IGNORED_BUILDS(#168)。 */
export function rewritePnpmAllowBuilds(raw) {
const wanted = purgePnpmBuildSpecs();
const text = String(raw || "").replace(/\r\n/g, "\n");
if (!text.trim()) {
return `allowBuilds:\n${wanted.map(formatAllowBuildLine).join("\n")}\n`;
}
const lines = text.split(/\r?\n/);
let start = -1;
for (let i = 0; i < lines.length; i += 1) {
if (/^allowBuilds[ \t]*:/.test(lines[i])) {
start = i;
break;
}
}
if (start < 0) {
const prefix = text.endsWith("\n") ? "" : "\n";
return `${text}${prefix}\nallowBuilds:\n${wanted.map(formatAllowBuildLine).join("\n")}\n`;
}
let end = lines.length;
const kept = [];
const seen = new Set();
for (let i = start + 1; i < lines.length; i += 1) {
const line = lines[i];
if (!line.trim()) {
end = i;
break;
}
if (/^\S/.test(line)) {
end = i;
break;
}
const parsed = parseAllowBuildLine(line);
if (!parsed) {
kept.push(line);
continue;
}
if (isPurgeAllowBuildKey(parsed.key)) {
kept.push(formatAllowBuildLine(parsed.key));
seen.add(parsed.key);
continue;
}
kept.push(line);
}
for (const key of wanted) {
if (!seen.has(key)) kept.push(formatAllowBuildLine(key));
}
const next = [...lines.slice(0, start + 1), ...kept, ...lines.slice(end)].join("\n");
return next.endsWith("\n") ? next : `${next}\n`;
}
/** pnpm 11/12 默认拦 postinstall(ERR_PNPM_IGNORED_BUILDS)。写进 profile 的 allowBuilds,下次 add 能跑脚本。 */
export function ensurePnpmAllowBuild(profileDir) {
const fp = path.join(profileDir, "pnpm-workspace.yaml");
let text = "";
try { text = fs.readFileSync(fp, "utf8"); } catch { text = ""; }
if (/(^|\n)[ \t]*dsh-purge[ \t]*:[ \t]*true\b/.test(text)) return { ok: true, wrote: false, path: fp };
if (!String(text).trim()) {
fs.writeFileSync(fp, "allowBuilds:\n dsh-purge: true\n", "utf8");
return { ok: true, wrote: true, path: fp };
}
if (/(^|\n)allowBuilds[ \t]*:/.test(text)) {
const next = text.replace(/(^|\n)(allowBuilds[ \t]*:[ \t]*\n)/, "$1$2 dsh-purge: true\n");
if (next === text) return { ok: true, wrote: false, path: fp };
fs.writeFileSync(fp, next, "utf8");
return { ok: true, wrote: true, path: fp };
}
const prefix = text.endsWith("\n") ? "" : "\n";
fs.writeFileSync(fp, `${text}${prefix}\nallowBuilds:\n dsh-purge: true\n`, "utf8");
const current = String(text || "").replace(/\r\n/g, "\n");
const next = rewritePnpmAllowBuilds(current);
if (next === current) return { ok: true, wrote: false, path: fp };
fs.writeFileSync(fp, next, "utf8");
return { ok: true, wrote: true, path: fp };
}