fix: Apply 不再覆盖 npm 全局 dsh 软链;pnpm/plugin add 补本体 insert(#157 #154 #155)

This commit is contained in:
叫我小杨同学 committed 2026-10-24 20:38:05 +08:00
1 parent 540b929e11
commit 2a3c7cef6a
7 files changed
+251 -36

No files matched your search

+78 -18
View File
@@ -1,13 +1,24 @@
#!/usr/bin/env node
/**
* pnpm add 只写 dependencies,不会把插件写进 dsh.profile.bundles。
* 装进 DSH profile 时补上这一笔;在插件仓库里跑 pnpm install 则跳过。
* 装进 DSH profile 时补两笔(#154):
* 1. dsh.profile.bundles
* 2. 用户层 cordis.patch.yml 的本体 insert
* pnpm add / dsh plugin add 都不会自己写 insert;市场会写,已有则跳过。
* 在插件仓库里跑 pnpm install 则整段跳过。
*/
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
const NAME = "dsh-purge";
const INSERT_BLOCK = [
"- insert:",
" - id: dsh-purge",
" name: dsh-purge",
"",
].join("\n");
function readJson(fp) {
try {
return JSON.parse(fs.readFileSync(fp, "utf8"));
@@ -36,22 +47,71 @@ function findProfileDir() {
return "";
}
const profileDir = findProfileDir();
if (!profileDir) process.exit(0);
export function profileHasPurgeBodyInsert(text) {
const lines = String(text || "").split(/\r?\n/);
let insertIndent = -1;
for (const line of lines) {
if (/^-\s*insert:\s*$/.test(line) || /^\s+-\s*insert:\s*$/.test(line)) {
insertIndent = (line.match(/^[ \t]*/) || [""])[0].length;
continue;
}
if (insertIndent < 0) continue;
const indent = (line.match(/^[ \t]*/) || [""])[0].length;
const trimmed = line.trim();
if (trimmed && indent <= insertIndent && /^-\s/.test(trimmed)) {
insertIndent = -1;
if (/^-\s*insert:\s*$/.test(trimmed)) {
insertIndent = indent;
}
continue;
}
if (/^-\s*id:\s*dsh-purge\s*$/.test(trimmed)) return true;
}
return false;
}
const fp = path.join(profileDir, "package.json");
const pkg = readJson(fp);
if (!pkg) {
process.stderr.write(`dsh-purge: cannot read ${fp}\n`);
process.exit(1);
function ensureProfileInsert(profileDir) {
const fp = path.join(profileDir, "cordis.patch.yml");
let text = "";
try { text = fs.readFileSync(fp, "utf8"); } catch { text = ""; }
if (profileHasPurgeBodyInsert(text)) {
process.stdout.write(`dsh-purge: insert already in ${fp}\n`);
return false;
}
const prefix = text && !text.endsWith("\n") ? "\n" : "";
fs.writeFileSync(fp, `${text}${prefix}${text ? "\n" : ""}${INSERT_BLOCK}`, "utf8");
process.stdout.write(`dsh-purge: wrote body insert (${fp})\n`);
return true;
}
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(NAME)) {
process.stdout.write(`dsh-purge: already in bundles (${profileDir})\n`);
process.exit(0);
function main() {
const profileDir = findProfileDir();
if (!profileDir) return;
const fp = path.join(profileDir, "package.json");
const pkg = readJson(fp);
if (!pkg) {
process.stderr.write(`dsh-purge: cannot read ${fp}\n`);
process.exit(1);
}
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(NAME)) {
pkg.dsh.profile.bundles = [...bundles, NAME];
fs.writeFileSync(fp, `${JSON.stringify(pkg, null, 2)}\n`, "utf8");
process.stdout.write(`dsh-purge: appended to bundles (${profileDir})\n`);
} else {
process.stdout.write(`dsh-purge: already in bundles (${profileDir})\n`);
}
try {
ensureProfileInsert(profileDir);
} catch (e) {
process.stderr.write(`dsh-purge: insert failed: ${e && e.message ? e.message : e}\n`);
process.exit(1);
}
}
pkg.dsh.profile.bundles = [...bundles, NAME];
fs.writeFileSync(fp, `${JSON.stringify(pkg, null, 2)}\n`, "utf8");
process.stdout.write(`dsh-purge: appended to bundles (${profileDir})\n`);
const invoked = process.argv[1] ? path.resolve(process.argv[1]) : "";
if (invoked === fileURLToPath(import.meta.url)) main();