dsh-decision-engine 0.0.0-stage → 0.5.0

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.
@@ -0,0 +1,102 @@
1
+ /**
2
+ * 更名迁移助手:把 profile 里的旧包名换成 dsh-decision-engine。
3
+ *
4
+ * 为什么需要这一步:本项目从 `dsh-startlux-decision` 更名为 `dsh-decision-engine`。
5
+ * profile 的 `package.json` 里有两处按名字引用插件——
6
+ *
7
+ * dsh.profile.bundles[] 决定「加载哪些 bundle」
8
+ * dependencies{} 决定「从哪里装」
9
+ *
10
+ * 只改其中一处,DSH 就找不到插件(表现为插件从列表里消失,或启动报
11
+ * 无法解析模块)。包名与依赖 key 必须同时改,且 key 必须与新包名一致。
12
+ *
13
+ * 用法:
14
+ * node scripts/migrate-profile.mjs # 预览要改什么(默认,不写文件)
15
+ * node scripts/migrate-profile.mjs --write # 真正写入(会先备份)
16
+ *
17
+ * 只处理 `dsh.profile.bundles` 与 `dependencies`/`devDependencies` 里的
18
+ * `dsh-startlux-decision`,其它字段一概不动。
19
+ */
20
+
21
+ import { copyFile, readFile, writeFile } from "node:fs/promises";
22
+ import { existsSync } from "node:fs";
23
+ import { homedir } from "node:os";
24
+ import { join } from "node:path";
25
+
26
+ const OLD_NAME = "dsh-startlux-decision";
27
+ const NEW_NAME = "dsh-decision-engine";
28
+ const WRITE = process.argv.includes("--write");
29
+
30
+ /** 候选 profile 路径:优先 desktop,其次是配置里能找到的其它 profile。 */
31
+ function candidateProfiles() {
32
+ const base = join(homedir(), ".dsh", "profiles");
33
+ return ["desktop", "web", "cli"].map((name) => join(base, name, "package.json"));
34
+ }
35
+
36
+ /** 找到第一个存在的 profile package.json。 */
37
+ async function pickProfile() {
38
+ for (const path of candidateProfiles()) {
39
+ if (existsSync(path)) return path;
40
+ }
41
+ return undefined;
42
+ }
43
+
44
+ /**
45
+ * 计算改动。刻意不解析成 AST:profile 的 package.json 结构固定,
46
+ * 按文本替换既能保留原有格式与缩进,也不会把注释(JSONC)弄丢。
47
+ *
48
+ * @param text - package.json 原文。
49
+ * @returns `{ next, notes }`;notes 说明改了哪些位置。
50
+ */
51
+ export function migrateProfileJson(text) {
52
+ const notes = [];
53
+ let next = text;
54
+
55
+ // 1) bundles 数组里的一项
56
+ const bundleLine = new RegExp(`^(\\s*)"${OLD_NAME}"(,?)$`, "m");
57
+ if (bundleLine.test(next)) {
58
+ next = next.replace(bundleLine, `$1"${NEW_NAME}"$2`);
59
+ notes.push(`dsh.profile.bundles: "${OLD_NAME}" → "${NEW_NAME}"`);
60
+ }
61
+
62
+ // 2) 依赖键(值不变:仍指向同一份本地目录或同一版本)
63
+ const depLine = new RegExp(`^(\\s*)"${OLD_NAME}"(\\s*:\\s*)"([^"]*)"(,?)$`, "m");
64
+ if (depLine.test(next)) {
65
+ next = next.replace(depLine, `$1"${NEW_NAME}"$2"$3"$4`);
66
+ notes.push(`dependencies: "${OLD_NAME}" → "${NEW_NAME}"`);
67
+ }
68
+
69
+ if (notes.length === 0) {
70
+ notes.push(`没有找到 "${OLD_NAME}",profile 可能已经迁移过`);
71
+ }
72
+ return { next, notes };
73
+ }
74
+
75
+ const profilePath = await pickProfile();
76
+ if (profilePath === undefined) {
77
+ console.error(`未找到 profile 的 package.json(找过:${candidateProfiles().join(", ")})`);
78
+ process.exit(2);
79
+ }
80
+
81
+ const before = await readFile(profilePath, "utf8");
82
+ const { next, notes } = migrateProfileJson(before);
83
+
84
+ console.log(`profile: ${profilePath}`);
85
+ for (const note of notes) console.log(` - ${note}`);
86
+
87
+ if (next === before) {
88
+ console.log("\n无需改动。");
89
+ process.exit(0);
90
+ }
91
+
92
+ if (!WRITE) {
93
+ console.log("\n以上为预览。确认无误后加 --write 真正写入(会先备份 .bak)。");
94
+ process.exit(0);
95
+ }
96
+
97
+ const backup = `${profilePath}.bak-${new Date().toISOString().replace(/[:.]/g, "-")}`;
98
+ await copyFile(profilePath, backup);
99
+ await writeFile(profilePath, next, "utf8");
100
+ console.log(`\n已写入。原文件备份在:${backup}`);
101
+ console.log("接着执行:cd " + join(profilePath, "..") + " && pnpm install");
102
+ console.log("然后重启 DSH。配置(config.yml,旧名 startlux.yml 会被自动迁移)不需要重填。");