@dsh-plugin/dsh-pack-plugin 0.0.0-stage → 0.0.1

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.
Files changed (76) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +178 -3
  3. package/cordis.patch.yml +9 -0
  4. package/icons/folder-zip-line.svg +1 -0
  5. package/lib/ball.js +597 -0
  6. package/lib/client.js +2934 -0
  7. package/lib/host.js +51745 -0
  8. package/lib/migrate-helper.js +35346 -0
  9. package/lib/progress-main.cjs +79 -0
  10. package/lib/progress-window.ps1 +114 -0
  11. package/lib/progress.html +126 -0
  12. package/lib/publish-to-github/SKILL.md +132 -0
  13. package/package.json +77 -4
  14. package/src/ball-entry.js +5 -0
  15. package/src/ball-host.js +99 -0
  16. package/src/ball-settings.js +352 -0
  17. package/src/ball.js +418 -0
  18. package/src/channel.js +12 -0
  19. package/src/client-plugin.js +58 -0
  20. package/src/client-rpc.js +14 -0
  21. package/src/client.js +5 -0
  22. package/src/core/discovery.js +192 -0
  23. package/src/core/dspack.js +93 -0
  24. package/src/core/extension-files.js +102 -0
  25. package/src/core/extension-install.js +295 -0
  26. package/src/core/extension-patch.js +369 -0
  27. package/src/core/extension-skills.js +164 -0
  28. package/src/core/extension-validate.js +127 -0
  29. package/src/core/extensions.js +207 -0
  30. package/src/core/home-store.js +32 -0
  31. package/src/core/host.js +149 -0
  32. package/src/core/index.js +59 -0
  33. package/src/core/inspect.js +162 -0
  34. package/src/core/install.js +775 -0
  35. package/src/core/managed-install.js +84 -0
  36. package/src/core/manifest.js +717 -0
  37. package/src/core/market.js +268 -0
  38. package/src/core/mcp-composition.js +189 -0
  39. package/src/core/mcp-install.js +112 -0
  40. package/src/core/mcp-transport.js +116 -0
  41. package/src/core/pack.js +371 -0
  42. package/src/core/plugin-metadata.js +355 -0
  43. package/src/core/registry.js +316 -0
  44. package/src/core/repo.js +339 -0
  45. package/src/core/scan.js +78 -0
  46. package/src/core/security.js +101 -0
  47. package/src/core/special.js +50 -0
  48. package/src/core/tar.js +103 -0
  49. package/src/core/update-config.js +234 -0
  50. package/src/core/update.js +792 -0
  51. package/src/core/vendored.js +715 -0
  52. package/src/core/workspace.js +95 -0
  53. package/src/endpoints.js +455 -0
  54. package/src/ensure-manager.js +186 -0
  55. package/src/host-node.js +625 -0
  56. package/src/host.js +17 -0
  57. package/src/index.d.ts +7 -0
  58. package/src/index.js +39 -0
  59. package/src/junction.js +142 -0
  60. package/src/migrate-helper.js +225 -0
  61. package/src/migrate.js +56 -0
  62. package/src/packaged.js +9 -0
  63. package/src/profiles.js +106 -0
  64. package/src/progress-main.cjs +79 -0
  65. package/src/progress-window.ps1 +114 -0
  66. package/src/progress.html +126 -0
  67. package/src/progress.js +132 -0
  68. package/src/rpc.js +157 -0
  69. package/src/runtime.js +43 -0
  70. package/src/section-gate.js +50 -0
  71. package/src/settings-nav-icon.js +188 -0
  72. package/src/settings.js +1611 -0
  73. package/src/skills/index.js +90 -0
  74. package/src/skills/publish-to-github/SKILL.md +132 -0
  75. package/src/tasks.js +145 -0
  76. package/src/v6-dialogs.js +312 -0
@@ -0,0 +1,186 @@
1
+ // 切换时把管理器 @dsh-plugin/dsh-pack-plugin 迁装进目标 profile。
2
+ //
3
+ // 管理器是「住在 profile 里的 bundle」,而切换 = 换掉整个 profile,管理器会被一起换走、
4
+ // 导致切过去就切不回来(单程票)。按不变量:管理器只在「装了自己的 profile」里运行,
5
+ // 所以从当前激活 profile(来源)复制它到目标即可,npm 发布前无需联网。
6
+ //
7
+ // 安装来源两种:
8
+ // - npm(切换默认):从 registry 拉最新版 tarball 解包(只落这一个包,不碰目标其它依赖)。
9
+ // - copy:从当前激活 profile 复制管理器本体,离线、快。
10
+ // 拉取最新失败(未发布 / 网络不通)时次之回退 copy —— 管理器只在自己 profile 里运行,来源必有,
11
+ // 离线也能迁装,不让切换硬失败。
12
+ //
13
+ // 管理器是自包含 bundle(host 侧已由 scripts/bundle-host.mjs 用 esbuild 内联
14
+ // fflate / proxy-agent / proxy-from-env 及其传递依赖),迁装只需搬管理器目录本身,
15
+ // 无需再手工同步任何「顶层 node_modules 依赖清单」——那正是 f91f375 引入 proxy-agent 后
16
+ // 清单漏更、导致切换后 failed to import 的根因。
17
+
18
+ import fsp from 'node:fs/promises';
19
+ import os from 'node:os';
20
+ import path from 'node:path';
21
+ import { ACTIVE_NAME } from './runtime.js';
22
+ import { getHost } from './host.js';
23
+ import { untar } from './core/tar.js';
24
+
25
+ const MANAGER = '@dsh-plugin/dsh-pack-plugin';
26
+ const LEGACY_MANAGER = '@dsh-packforge/dsh-pack-plugin';
27
+ const NPM_REGISTRY = 'https://registry.npmjs.org';
28
+
29
+ async function exists(p) {
30
+ try {
31
+ await fsp.access(p);
32
+ return true;
33
+ } catch {
34
+ return false;
35
+ }
36
+ }
37
+
38
+ async function readManifest(profileDir) {
39
+ return JSON.parse(await fsp.readFile(path.join(profileDir, 'package.json'), 'utf8'));
40
+ }
41
+
42
+ async function writeManifest(profileDir, manifest) {
43
+ await fsp.writeFile(path.join(profileDir, 'package.json'), `${JSON.stringify(manifest, null, 2)}\n`);
44
+ }
45
+
46
+ function managerDir(profileDir, name = MANAGER) {
47
+ return path.join(profileDir, 'node_modules', ...name.split('/'));
48
+ }
49
+
50
+ async function installedManager(profileDir, manifest) {
51
+ const bundles = manifest.dsh?.profile?.bundles;
52
+ if (!Array.isArray(bundles)) {
53
+ return undefined;
54
+ }
55
+ for (const name of [MANAGER, LEGACY_MANAGER]) {
56
+ if (bundles.includes(name) && await exists(path.join(managerDir(profileDir, name), 'package.json'))) {
57
+ return name;
58
+ }
59
+ }
60
+ return undefined;
61
+ }
62
+
63
+ /**
64
+ * 把管理器从当前激活 profile 迁装进 target(幂等)。
65
+ * @param {object} runtime { profilesDir, home }
66
+ * @param {string} target 目标 profile 名
67
+ * @param {{source?: 'copy'|'npm'}} [opts] 安装来源;切换走 npm(拉取最新,失败次之回退 copy),
68
+ * 创建空整合包走默认 copy(离线)。
69
+ * @returns {Promise<{installed:boolean, spec:string, source:string}>}
70
+ */
71
+ export async function ensureManagerInProfile(runtime, target, { source = 'copy' } = {}) {
72
+ const sourceDir = path.join(runtime.profilesDir, ACTIVE_NAME);
73
+ const targetDir = path.join(runtime.profilesDir, target);
74
+
75
+ const targetManifest = await readManifest(targetDir);
76
+ const existing = await installedManager(targetDir, targetManifest);
77
+ if (existing) {
78
+ return { installed: false, spec: targetManifest.dependencies?.[existing], source: 'installed' };
79
+ }
80
+
81
+ // npm(拉取最新)失败时次之回退 copy(复制当前 profile 的管理器,离线可靠)。
82
+ let spec;
83
+ let used = source;
84
+ if (source === 'npm') {
85
+ try {
86
+ spec = await npmPullManager(runtime, targetDir);
87
+ } catch {
88
+ spec = await copyManager(sourceDir, targetDir, target);
89
+ used = 'copy';
90
+ }
91
+ } else {
92
+ spec = await copyManager(sourceDir, targetDir, target);
93
+ }
94
+
95
+ // 登记进目标 package.json:dependencies + bundles。
96
+ targetManifest.dependencies ??= {};
97
+ targetManifest.dependencies[MANAGER] = spec;
98
+ targetManifest.dsh ??= {};
99
+ targetManifest.dsh.profile ??= {};
100
+ const bundles = targetManifest.dsh.profile.bundles ??= [];
101
+ const oldIndex = bundles.indexOf(LEGACY_MANAGER);
102
+ if (oldIndex !== -1) {
103
+ // 旧坐标已登记但包缺失时,保留其层栈位置,避免同时加载同一个 Cordis 条目。
104
+ delete targetManifest.dependencies[LEGACY_MANAGER];
105
+ bundles.splice(oldIndex, 1, ...(bundles.includes(MANAGER) ? [] : [MANAGER]));
106
+ } else if (!bundles.includes(MANAGER)) {
107
+ bundles.push(MANAGER);
108
+ }
109
+ await writeManifest(targetDir, targetManifest);
110
+
111
+ return { installed: true, spec, source: used };
112
+ }
113
+
114
+ /** 来源 profile 复制(离线):管理器本体(自包含 bundle,零外部依赖)。 */
115
+ async function copyManager(sourceDir, targetDir, target) {
116
+ const sourceManager = managerDir(sourceDir);
117
+ if (!(await exists(path.join(sourceManager, 'package.json')))) {
118
+ throw new Error(`当前 profile 里没有 ${MANAGER},无法迁装到「${target}」(请先在本 profile 安装管理器)`);
119
+ }
120
+ const sourceManifest = await readManifest(sourceDir);
121
+ const { version } = await readManifest(sourceManager);
122
+ const spec = sourceManifest.dependencies?.[MANAGER] ?? version;
123
+ if (!spec) {
124
+ throw new Error(`${MANAGER} 的本机安装缺少版本`);
125
+ }
126
+
127
+ await fsp.mkdir(path.dirname(managerDir(targetDir)), { recursive: true });
128
+ await fsp.cp(sourceManager, managerDir(targetDir), { recursive: true, force: true });
129
+ return spec;
130
+ }
131
+
132
+ /** 从 NPM registry 拉最新版:解析元数据 → 下载 tarball → 解包到目标(自包含,不读来源 profile)。 */
133
+ async function npmPullManager(runtime, targetDir) {
134
+ const host = getHost();
135
+ const esc = MANAGER.replace('/', '%2F');
136
+ const metaUrl = `${NPM_REGISTRY}/${esc}/latest`;
137
+ const tmp = await fsp.mkdtemp(path.join(os.tmpdir(), 'dspack-npm-'));
138
+
139
+ try {
140
+ const metaPath = path.join(tmp, 'meta.json');
141
+ await host.download(metaUrl, metaPath);
142
+ const metaText = await host.readTextFile(metaPath);
143
+ if (!metaText) throw new Error(`无法读取 ${MANAGER} 的 NPM 元数据`);
144
+ const meta = JSON.parse(metaText);
145
+ const version = meta?.version;
146
+ const tarball = meta?.dist?.tarball;
147
+ if (!version || !tarball) {
148
+ throw new Error(`${MANAGER} 尚未发布到 NPM(拉取最新失败)`);
149
+ }
150
+
151
+ const tgzPath = path.join(tmp, 'pkg.tgz');
152
+ await host.download(tarball, tgzPath);
153
+ const bytes = await host.readFile(tgzPath);
154
+ if (!bytes) throw new Error(`下载 ${MANAGER} 的 NPM 包失败`);
155
+
156
+ const entries = untar(bytes);
157
+ const dest = managerDir(targetDir);
158
+ let written = 0;
159
+ for (const [entryPath, data] of Object.entries(entries)) {
160
+ const rel = entryPath.startsWith('package/') ? entryPath.slice('package/'.length) : null;
161
+ if (!rel || rel === '' || rel.endsWith('/')) continue;
162
+ await fsp.mkdir(path.dirname(path.join(dest, rel)), { recursive: true });
163
+ await fsp.writeFile(path.join(dest, rel), data);
164
+ written += 1;
165
+ }
166
+ if (written === 0) throw new Error(`${MANAGER} 的 NPM 包内容为空`);
167
+
168
+ return `^${version}`;
169
+ } finally {
170
+ await fsp.rm(tmp, { recursive: true, force: true }).catch(() => {});
171
+ }
172
+ }
173
+
174
+ /**
175
+ * 只读检测目标 profile 是否已装管理器(确认弹窗的预检用,不安装)。
176
+ * @returns {Promise<boolean>}
177
+ */
178
+ export async function checkManagerInProfile(runtime, target) {
179
+ const targetDir = path.join(runtime.profilesDir, target);
180
+ try {
181
+ const manifest = await readManifest(targetDir);
182
+ return Boolean(await installedManager(targetDir, manifest));
183
+ } catch {
184
+ return false;
185
+ }
186
+ }