mirror of
https://github.com/YuJunZhiXue/dsh-purge.git
synced 2026-11-28 06:20:22 +08:00
1.1.79: 修复 bug,修复稳定性(#165 #166 #167)
This commit is contained in:
1 parent
add692e491
commit
82da559cff
17 files changed
+872
-185
No files matched your search
+49
-3
@@ -92,19 +92,65 @@ export function listProfileDirs(dshHome) {
|
||||
return out;
|
||||
}
|
||||
|
||||
export function ensureProfileBundles(profileDir) {
|
||||
const fp = path.join(profileDir, "package.json");
|
||||
const pkg = readJson(fp);
|
||||
if (!pkg) return { ok: false, wrote: false, path: fp };
|
||||
pkg.dsh = pkg.dsh && typeof pkg.dsh === "object" ? pkg.dsh : {};
|
||||
pkg.dsh.profile = pkg.dsh.profile && typeof pkg.dsh.profile === "object" ? pkg.dsh.profile : {};
|
||||
const bundles = Array.isArray(pkg.dsh.profile.bundles) ? pkg.dsh.profile.bundles : [];
|
||||
if (bundles.includes(PURGE_NAME)) return { ok: true, wrote: false, path: fp };
|
||||
pkg.dsh.profile.bundles = [...bundles, PURGE_NAME];
|
||||
fs.writeFileSync(fp, `${JSON.stringify(pkg, null, 2)}\n`, "utf8");
|
||||
return { ok: true, wrote: true, path: fp };
|
||||
}
|
||||
|
||||
/** 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");
|
||||
return { ok: true, wrote: true, path: fp };
|
||||
}
|
||||
|
||||
/** 已装进 bundles / node_modules 但缺 insert 的 profile,补上本体行。 */
|
||||
export function ensureAllProfileBodyInserts(dshHome) {
|
||||
return ensureAllProfileInstall(dshHome).map((item) => ({
|
||||
dir: item.dir,
|
||||
ok: item.ok,
|
||||
wrote: Boolean(item.insert?.wrote),
|
||||
path: item.insert?.path || path.join(item.dir || "", "cordis.patch.yml"),
|
||||
error: item.error,
|
||||
}));
|
||||
}
|
||||
|
||||
/** bundles + 本体 insert + pnpm allowBuilds。postinstall 被跳过、升级覆盖 overlay 之后靠这条把按钮找回来。 */
|
||||
export function ensureAllProfileInstall(dshHome) {
|
||||
const results = [];
|
||||
for (const dir of listProfileDirs(dshHome)) {
|
||||
if (!profileOwnsPurge(dir)) continue;
|
||||
try {
|
||||
results.push({ dir, ...ensureProfileBodyInsert(dir) });
|
||||
const bundles = ensureProfileBundles(dir);
|
||||
const insert = ensureProfileBodyInsert(dir);
|
||||
const pnpm = ensurePnpmAllowBuild(dir);
|
||||
results.push({ dir, ok: true, bundles, insert, pnpm });
|
||||
} catch (error) {
|
||||
results.push({
|
||||
dir,
|
||||
ok: false,
|
||||
wrote: false,
|
||||
path: path.join(dir, "cordis.patch.yml"),
|
||||
error: error && error.message ? error.message : String(error),
|
||||
});
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user