@ran-sh/dsh-crew 0.3.1 → 0.3.2
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.
- package/.claude-plugin/marketplace.json +17 -17
- package/.claude-plugin/plugin.json +8 -8
- package/.mcp.json +8 -8
- package/LICENSE +21 -21
- package/README.de.md +359 -359
- package/README.es.md +359 -359
- package/README.fr.md +359 -359
- package/README.hi.md +359 -359
- package/README.id.md +359 -359
- package/README.ja.md +359 -359
- package/README.ko.md +359 -359
- package/README.md +140 -140
- package/README.pt.md +359 -359
- package/README.ru.md +359 -359
- package/README.th.md +359 -359
- package/README.tr.md +359 -359
- package/README.vi.md +359 -359
- package/README.zh-TW.md +359 -359
- package/README.zh.md +140 -140
- package/agents/ds-flash.md +26 -26
- package/agents/ds-pro.md +32 -32
- package/agents/ds-reviewer.md +23 -23
- package/agents/ds-worker.md +22 -22
- package/codex/agents/ds-flash.toml +30 -30
- package/codex/agents/ds-pro.toml +31 -31
- package/codex/agents/ds-reviewer.toml +28 -28
- package/codex/agents/ds-worker.toml +28 -28
- package/codex/prompts/dsh-config.md +3 -3
- package/codex/prompts/dsh-status.md +1 -1
- package/commands/config.md +11 -11
- package/commands/off.md +5 -5
- package/commands/on.md +5 -5
- package/commands/status.md +5 -5
- package/cordis.patch.yml +4 -4
- package/lib/client.js +2765 -2765
- package/package.json +127 -127
- package/scripts/build-client.mjs +28 -28
- package/scripts/live-crew-smoke.mjs +39 -39
- package/scripts/live-policy-matrix.mjs +177 -177
- package/scripts/policy-probe.mjs +101 -101
- package/scripts/setup.mjs +295 -294
- package/scripts/smoke-real.mjs +110 -110
- package/scripts/smoke.mjs +78 -78
- package/scripts/verify-installer-fix.mjs +26 -26
- package/scripts/verify-npm-install.mjs +297 -276
- package/src/adaptive-routing.mjs +260 -260
- package/src/client/activation-summary.tsx +64 -64
- package/src/client/entry.tsx +236 -236
- package/src/client/index.tsx +1120 -1120
- package/src/config-readiness.mjs +59 -59
- package/src/delivery.mjs +205 -205
- package/src/dsh-cli-runtime.mjs +435 -251
- package/src/failure-classification.mjs +172 -172
- package/src/hub/entry.mjs +98 -98
- package/src/hub-client.mjs +132 -132
- package/src/hub-compatibility.mjs +49 -49
- package/src/i18n.mjs +19 -19
- package/src/install/cli.mjs +28 -28
- package/src/install/install-legacy.mjs +460 -460
- package/src/install/install.mjs +451 -451
- package/src/mcp-runtime.mjs +257 -257
- package/src/model-catalog.mjs +173 -173
- package/src/model-routing.mjs +391 -391
- package/src/policy.mjs +197 -197
- package/src/readiness-matrix.mjs +169 -169
- package/src/runtime-controls.mjs +90 -90
- package/src/runtime-identity.mjs +108 -108
- package/src/server.mjs +477 -477
- package/src/status-shard.mjs +52 -52
- package/src/structured-error-code.mjs +38 -38
- package/src/vision-route.mjs +138 -138
- package/src/workflow-runtime.mjs +573 -567
- package/src/workflow.mjs +160 -160
- package/src/workspace-audit.mjs +231 -231
- package/src/workspace-isolation.mjs +365 -306
- package/statusline/statusline.sh +14 -14
- package/statusline/worker-segment.sh +35 -35
- package/worker.cordis.yml +77 -77
package/src/dsh-cli-runtime.mjs
CHANGED
|
@@ -1,251 +1,435 @@
|
|
|
1
|
-
// DSH CLI/runtime resolution for Crew-owned installs.
|
|
2
|
-
//
|
|
3
|
-
// The resolver is deliberately independent from the official ~/.dsh state.
|
|
4
|
-
// A reusable CLI may live under the Crew DSH_HOME, while global dsh/npx remain
|
|
5
|
-
// compatibility fallbacks. Status and uninstall callers can resolve without
|
|
6
|
-
// allowing a download.
|
|
7
|
-
|
|
8
|
-
import { spawnSync } from 'node:child_process';
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function
|
|
50
|
-
return
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
function
|
|
54
|
-
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
return
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
export function
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
};
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
});
|
|
207
|
-
const
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
1
|
+
// DSH CLI/runtime resolution for Crew-owned installs.
|
|
2
|
+
//
|
|
3
|
+
// The resolver is deliberately independent from the official ~/.dsh state.
|
|
4
|
+
// A reusable CLI may live under the Crew DSH_HOME, while global dsh/npx remain
|
|
5
|
+
// compatibility fallbacks. Status and uninstall callers can resolve without
|
|
6
|
+
// allowing a download.
|
|
7
|
+
|
|
8
|
+
import { spawnSync } from 'node:child_process';
|
|
9
|
+
import { createRequire } from 'node:module';
|
|
10
|
+
import {
|
|
11
|
+
existsSync,
|
|
12
|
+
lstatSync,
|
|
13
|
+
mkdirSync,
|
|
14
|
+
readFileSync,
|
|
15
|
+
realpathSync,
|
|
16
|
+
symlinkSync,
|
|
17
|
+
unlinkSync,
|
|
18
|
+
writeFileSync,
|
|
19
|
+
} from 'node:fs';
|
|
20
|
+
import { dirname, extname, isAbsolute, join, relative, resolve, sep } from 'node:path';
|
|
21
|
+
import { homedir } from 'node:os';
|
|
22
|
+
import { crewDshHome, crewProfileDir } from './install/install.mjs';
|
|
23
|
+
|
|
24
|
+
export const DSH_CLI_PACKAGE = '@deepseek-ai/dsh';
|
|
25
|
+
export const CREW_DSH_RUNTIME_DIRNAME = 'runtime';
|
|
26
|
+
const CREW_PROFILE_DEFAULT_BUNDLES = ['@deepseek-ai/dsh-base'];
|
|
27
|
+
const PROFILE_PATCH_TEMPLATE = '[]\n';
|
|
28
|
+
const PROFILE_PNPM_WORKSPACE = 'packages:\n - .\n\nnodeLinker: hoisted\nautoInstallPeers: false\n';
|
|
29
|
+
|
|
30
|
+
function defaultFindCommand(name) {
|
|
31
|
+
const probe = process.platform === 'win32' ? 'where.exe' : 'which';
|
|
32
|
+
const result = spawnSync(probe, [name], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] });
|
|
33
|
+
if (result.status !== 0) return null;
|
|
34
|
+
return String(result.stdout ?? '').split(/\r?\n/).map((line) => line.trim()).find(Boolean) ?? null;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function defaultExists(path) {
|
|
38
|
+
try { return existsSync(path); } catch { return false; }
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function packageVersion(entry, read = readFileSync) {
|
|
42
|
+
try {
|
|
43
|
+
const packageFile = join(entry, '..', '..', 'package.json');
|
|
44
|
+
const parsed = JSON.parse(read(packageFile, 'utf8'));
|
|
45
|
+
return typeof parsed.version === 'string' ? parsed.version : null;
|
|
46
|
+
} catch { return null; }
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function crewDshRuntimeRoot({ home = homedir() } = {}) {
|
|
50
|
+
return join(crewDshHome({ home }), CREW_DSH_RUNTIME_DIRNAME);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function crewDshRuntimeEntry({ home = homedir(), platform = process.platform } = {}) {
|
|
54
|
+
const suffix = platform === 'win32' ? '.cmd' : '';
|
|
55
|
+
return join(crewDshRuntimeRoot({ home }), 'node_modules', '.bin', `dsh${suffix}`);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function crewDshRuntimeModule({ home = homedir() } = {}) {
|
|
59
|
+
return join(crewDshRuntimeRoot({ home }), 'node_modules', '@deepseek-ai', 'dsh', 'lib', 'bin.js');
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function isPathLike(value) {
|
|
63
|
+
return typeof value === 'string' && (value.includes('/') || value.includes('\\') || extname(value) === '.cmd' || extname(value) === '.exe');
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function descriptor({ kind, command, args = [], source, version = null, reusable = false }) {
|
|
67
|
+
return Object.freeze({ kind, command, args: [...args], source, version, reusable });
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function explicitDescriptor(value, { exists = defaultExists, platform = process.platform } = {}) {
|
|
71
|
+
if (typeof value !== 'string' || !value.trim()) return null;
|
|
72
|
+
const trimmed = value.trim();
|
|
73
|
+
if (isPathLike(trimmed) && !exists(trimmed)) return null;
|
|
74
|
+
if (extname(trimmed).toLowerCase() === '.js') {
|
|
75
|
+
return descriptor({ kind: 'explicit-node', command: process.execPath, args: [trimmed], source: 'explicit', reusable: true });
|
|
76
|
+
}
|
|
77
|
+
return descriptor({ kind: platform === 'win32' && extname(trimmed).toLowerCase() === '.cmd' ? 'explicit-cmd' : 'explicit', command: trimmed, source: 'explicit', reusable: true });
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Resolve a CLI without reading DSH credentials or profile state.
|
|
82
|
+
* `allowDownload` only affects the final npx descriptor; resolution itself
|
|
83
|
+
* never starts a network operation.
|
|
84
|
+
*/
|
|
85
|
+
export function resolveDshCli({
|
|
86
|
+
home = homedir(),
|
|
87
|
+
env = process.env,
|
|
88
|
+
platform = process.platform,
|
|
89
|
+
exists = defaultExists,
|
|
90
|
+
findCommand = defaultFindCommand,
|
|
91
|
+
allowDownload = false,
|
|
92
|
+
includeCompatibility = true,
|
|
93
|
+
read = readFileSync,
|
|
94
|
+
} = {}) {
|
|
95
|
+
const explicit = explicitDescriptor(env.DSH_CREW_DSH_CLI ?? env.DSH_CLI, { exists, platform });
|
|
96
|
+
if (explicit) return explicit;
|
|
97
|
+
|
|
98
|
+
const moduleEntry = crewDshRuntimeModule({ home });
|
|
99
|
+
if (exists(moduleEntry)) {
|
|
100
|
+
return descriptor({
|
|
101
|
+
kind: 'crew-runtime',
|
|
102
|
+
command: process.execPath,
|
|
103
|
+
args: [moduleEntry],
|
|
104
|
+
source: 'crew-runtime',
|
|
105
|
+
version: packageVersion(moduleEntry, read),
|
|
106
|
+
reusable: true,
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (!includeCompatibility) return null;
|
|
111
|
+
|
|
112
|
+
const global = findCommand('dsh');
|
|
113
|
+
if (global) return descriptor({ kind: 'global', command: global, source: 'global' });
|
|
114
|
+
|
|
115
|
+
const npx = findCommand('npx');
|
|
116
|
+
if (!npx) return null;
|
|
117
|
+
return descriptor({
|
|
118
|
+
kind: allowDownload ? 'npx-download' : 'npx-local',
|
|
119
|
+
command: npx,
|
|
120
|
+
args: allowDownload ? ['--yes', DSH_CLI_PACKAGE] : ['--no-install', DSH_CLI_PACKAGE],
|
|
121
|
+
source: allowDownload ? 'npx-download' : 'npx-local',
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function quoteWindowsArg(value) {
|
|
126
|
+
const input = String(value);
|
|
127
|
+
if (input.length > 0 && !/[\s"]/u.test(input)) return input;
|
|
128
|
+
let output = '"';
|
|
129
|
+
let slashes = 0;
|
|
130
|
+
for (const char of input) {
|
|
131
|
+
if (char === '\\') { slashes += 1; continue; }
|
|
132
|
+
if (char === '"') {
|
|
133
|
+
output += '\\'.repeat(slashes * 2 + 1) + '"';
|
|
134
|
+
} else {
|
|
135
|
+
output += '\\'.repeat(slashes) + char;
|
|
136
|
+
}
|
|
137
|
+
slashes = 0;
|
|
138
|
+
}
|
|
139
|
+
output += '\\'.repeat(slashes * 2) + '"';
|
|
140
|
+
return output;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export { quoteWindowsArg };
|
|
144
|
+
|
|
145
|
+
/** Build a shell-free invocation, including deterministic Windows .cmd handling. */
|
|
146
|
+
export function buildDshInvocation(cli, args = [], { platform = process.platform, comspec = process.env.ComSpec ?? 'cmd.exe' } = {}) {
|
|
147
|
+
const allArgs = [...(cli?.args ?? []), ...args].map(String);
|
|
148
|
+
const command = String(cli?.command ?? '');
|
|
149
|
+
if (platform === 'win32' && /\.(?:cmd|bat)$/iu.test(command)) {
|
|
150
|
+
return {
|
|
151
|
+
command: comspec,
|
|
152
|
+
args: ['/d', '/s', '/c', [quoteWindowsArg(command), ...allArgs.map(quoteWindowsArg)].join(' ')],
|
|
153
|
+
shell: false,
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
return { command, args: allArgs, shell: false };
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export function runResolvedDsh(cli, args = [], {
|
|
160
|
+
home = homedir(),
|
|
161
|
+
env = process.env,
|
|
162
|
+
runner = spawnSync,
|
|
163
|
+
platform = process.platform,
|
|
164
|
+
comspec = process.env.ComSpec ?? 'cmd.exe',
|
|
165
|
+
} = {}) {
|
|
166
|
+
if (!cli) return { ok: false, status: -1, stdout: '', stderr: 'DSH CLI unavailable' };
|
|
167
|
+
const invocation = buildDshInvocation(cli, args, { platform, comspec });
|
|
168
|
+
const result = runner(invocation.command, invocation.args, {
|
|
169
|
+
encoding: 'utf8',
|
|
170
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
171
|
+
shell: invocation.shell,
|
|
172
|
+
env: { ...env, DSH_HOME: crewDshHome({ home }) },
|
|
173
|
+
});
|
|
174
|
+
return {
|
|
175
|
+
ok: result.status === 0,
|
|
176
|
+
status: result.status ?? -1,
|
|
177
|
+
stdout: result.stdout ?? '',
|
|
178
|
+
stderr: result.stderr ?? '',
|
|
179
|
+
invocation,
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Install a reusable DSH CLI into Crew-owned state. This is the only helper
|
|
185
|
+
* that may invoke a package manager, and callers must explicitly opt into it.
|
|
186
|
+
*/
|
|
187
|
+
export function ensureCrewDshRuntime({
|
|
188
|
+
home = homedir(),
|
|
189
|
+
packageSpec = DSH_CLI_PACKAGE,
|
|
190
|
+
npmCommand = null,
|
|
191
|
+
pnpmCommand = null,
|
|
192
|
+
findCommand = defaultFindCommand,
|
|
193
|
+
exists = defaultExists,
|
|
194
|
+
runner = spawnSync,
|
|
195
|
+
platform = process.platform,
|
|
196
|
+
comspec = process.env.ComSpec ?? 'cmd.exe',
|
|
197
|
+
env = process.env,
|
|
198
|
+
} = {}) {
|
|
199
|
+
const existing = resolveDshCli({ home, env, platform, exists, findCommand, includeCompatibility: false });
|
|
200
|
+
if (existing?.kind === 'crew-runtime') return { ok: true, cli: existing, reused: true };
|
|
201
|
+
|
|
202
|
+
const pnpm = pnpmCommand ?? findCommand('pnpm');
|
|
203
|
+
const npm = npmCommand ?? findCommand('npm');
|
|
204
|
+
if (!pnpm && !npm) return { ok: false, code: 'DSH_RUNTIME_INSTALLER_NOT_FOUND', error: 'pnpm/npm unavailable' };
|
|
205
|
+
const runtimeRoot = crewDshRuntimeRoot({ home });
|
|
206
|
+
mkdirSync(runtimeRoot, { recursive: true });
|
|
207
|
+
const packageManager = pnpm
|
|
208
|
+
? descriptor({ kind: 'pnpm', command: pnpm, source: 'pnpm' })
|
|
209
|
+
: descriptor({ kind: 'npm', command: npm, source: 'npm' });
|
|
210
|
+
const packageArgs = pnpm
|
|
211
|
+
? ['add', '--dir', runtimeRoot, '--ignore-scripts', packageSpec]
|
|
212
|
+
: ['install', '--prefix', runtimeRoot, '--no-package-lock', '--ignore-scripts', '--omit=dev', packageSpec];
|
|
213
|
+
const invocation = buildDshInvocation(packageManager, packageArgs, { platform, comspec });
|
|
214
|
+
const result = runner(invocation.command, invocation.args, {
|
|
215
|
+
encoding: 'utf8',
|
|
216
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
217
|
+
shell: invocation.shell,
|
|
218
|
+
env: { ...env },
|
|
219
|
+
});
|
|
220
|
+
const cli = resolveDshCli({ home, env, platform, exists, findCommand, includeCompatibility: false });
|
|
221
|
+
if (result.status !== 0 || !cli || cli.kind !== 'crew-runtime') {
|
|
222
|
+
return { ok: false, code: 'DSH_RUNTIME_INSTALL_FAILED', error: 'Crew-owned DSH runtime install failed', status: result.status ?? -1 };
|
|
223
|
+
}
|
|
224
|
+
return { ok: true, cli, reused: false, version: cli.version, runtimeRoot };
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
export function describeDshCli(cli) {
|
|
228
|
+
if (!cli) return 'unavailable';
|
|
229
|
+
const version = cli.version ? `@${cli.version}` : '';
|
|
230
|
+
return `${cli.kind}${version}`;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function isObject(value) {
|
|
234
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function isSafePackageName(name) {
|
|
238
|
+
return typeof name === 'string'
|
|
239
|
+
&& /^(?:@[^/\s]+\/)?[^/\s]+$/u.test(name)
|
|
240
|
+
&& !name.split('/').some((part) => part === '.' || part === '..');
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function isWithin(root, candidate) {
|
|
244
|
+
const rel = relative(root, candidate);
|
|
245
|
+
return rel === '' || (rel !== '..' && !rel.startsWith(`..${sep}`) && !isAbsolute(rel));
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
function fileSpec(target) {
|
|
249
|
+
return `link:${target.replace(/\\/g, '/')}`;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function profileLinkPath(profileRoot, name) {
|
|
253
|
+
return join(profileRoot, 'node_modules', ...name.split('/'));
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function readPluginRoot(root, expectedName) {
|
|
257
|
+
if (typeof root !== 'string' || !isAbsolute(root)) return { ok: false, code: 'INVALID_CREW_PLUGIN_ROOT' };
|
|
258
|
+
let targetRoot;
|
|
259
|
+
try { targetRoot = realpathSync(root); } catch { return { ok: false, code: 'CREW_PLUGIN_ROOT_NOT_FOUND' }; }
|
|
260
|
+
const packageFile = join(targetRoot, 'package.json');
|
|
261
|
+
let pkg;
|
|
262
|
+
try { pkg = JSON.parse(readFileSync(packageFile, 'utf8')); } catch { return { ok: false, code: 'CREW_PLUGIN_MANIFEST_INVALID' }; }
|
|
263
|
+
if (!isObject(pkg) || !isSafePackageName(pkg.name) || (expectedName !== undefined && pkg.name !== expectedName)) {
|
|
264
|
+
return { ok: false, code: 'CREW_PLUGIN_MANIFEST_INVALID' };
|
|
265
|
+
}
|
|
266
|
+
const patchSpec = pkg.dsh?.bundle?.patch;
|
|
267
|
+
if (typeof patchSpec !== 'string' || !patchSpec.trim()) return { ok: false, code: 'CREW_PLUGIN_BUNDLE_INVALID' };
|
|
268
|
+
const patchPath = resolve(targetRoot, patchSpec);
|
|
269
|
+
if (!isWithin(targetRoot, patchPath) || !existsSync(patchPath)) return { ok: false, code: 'CREW_PLUGIN_BUNDLE_INVALID' };
|
|
270
|
+
return { ok: true, targetRoot, packageFile, packageName: pkg.name, packageVersion: pkg.version ?? null, patchPath, pkg };
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
function readProfileManifestForRegistration(profileRoot) {
|
|
274
|
+
const profileManifest = join(profileRoot, 'package.json');
|
|
275
|
+
if (!existsSync(profileManifest)) {
|
|
276
|
+
return {
|
|
277
|
+
profileManifest,
|
|
278
|
+
manifest: {
|
|
279
|
+
name: `dsh-profile-${profileRoot.split(/[\\/]/u).at(-1)}`,
|
|
280
|
+
private: true,
|
|
281
|
+
dependencies: {},
|
|
282
|
+
dsh: { profile: { bundles: [...CREW_PROFILE_DEFAULT_BUNDLES] } },
|
|
283
|
+
},
|
|
284
|
+
created: true,
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
let manifest;
|
|
288
|
+
try { manifest = JSON.parse(readFileSync(profileManifest, 'utf8')); } catch { return { ok: false, code: 'CREW_PROFILE_METADATA_INVALID' }; }
|
|
289
|
+
if (!isObject(manifest)) return { ok: false, code: 'CREW_PROFILE_METADATA_INVALID' };
|
|
290
|
+
if (manifest.dependencies !== undefined && !isObject(manifest.dependencies)) return { ok: false, code: 'CREW_PROFILE_METADATA_INVALID' };
|
|
291
|
+
if (manifest.dependencies && Object.values(manifest.dependencies).some((value) => typeof value !== 'string')) {
|
|
292
|
+
return { ok: false, code: 'CREW_PROFILE_METADATA_INVALID' };
|
|
293
|
+
}
|
|
294
|
+
if (manifest.dsh !== undefined && !isObject(manifest.dsh)) return { ok: false, code: 'CREW_PROFILE_METADATA_INVALID' };
|
|
295
|
+
if (manifest.dsh?.profile !== undefined && !isObject(manifest.dsh.profile)) return { ok: false, code: 'CREW_PROFILE_METADATA_INVALID' };
|
|
296
|
+
const bundles = manifest.dsh?.profile?.bundles;
|
|
297
|
+
if (bundles !== undefined && (!Array.isArray(bundles) || bundles.some((name) => !isSafePackageName(name)))) {
|
|
298
|
+
return { ok: false, code: 'CREW_PROFILE_METADATA_INVALID' };
|
|
299
|
+
}
|
|
300
|
+
return { profileManifest, manifest, created: false };
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
function ensureProfileScaffold(profileRoot) {
|
|
304
|
+
mkdirSync(profileRoot, { recursive: true });
|
|
305
|
+
let changed = false;
|
|
306
|
+
const patchFile = join(profileRoot, 'cordis.patch.yml');
|
|
307
|
+
if (!existsSync(patchFile)) { writeFileSync(patchFile, PROFILE_PATCH_TEMPLATE); changed = true; }
|
|
308
|
+
const workspaceFile = join(profileRoot, 'pnpm-workspace.yaml');
|
|
309
|
+
if (!existsSync(workspaceFile)) { writeFileSync(workspaceFile, PROFILE_PNPM_WORKSPACE); changed = true; }
|
|
310
|
+
return changed;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
function ensureDirectoryLink(linkPath, targetRoot) {
|
|
314
|
+
let stat;
|
|
315
|
+
try { stat = lstatSync(linkPath); } catch { stat = null; }
|
|
316
|
+
if (stat && !stat.isSymbolicLink()) return { ok: false, code: 'CREW_PLUGIN_LINK_CONFLICT' };
|
|
317
|
+
if (stat) {
|
|
318
|
+
try {
|
|
319
|
+
if (realpathSync(linkPath) === targetRoot) return { ok: true, changed: false };
|
|
320
|
+
} catch {}
|
|
321
|
+
unlinkSync(linkPath);
|
|
322
|
+
}
|
|
323
|
+
mkdirSync(dirname(linkPath), { recursive: true });
|
|
324
|
+
symlinkSync(targetRoot, linkPath, process.platform === 'win32' ? 'junction' : 'dir');
|
|
325
|
+
return { ok: true, changed: true };
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* Register a local Crew plugin without invoking DSH's pnpm-forwarding plugin
|
|
330
|
+
* command. The profile manifest and one loader-visible directory link are the
|
|
331
|
+
* only state changed; no dependency resolution or policy bypass is attempted.
|
|
332
|
+
*/
|
|
333
|
+
export function ensureCrewPluginRegistration({ home = homedir(), root, name } = {}) {
|
|
334
|
+
const plugin = readPluginRoot(root, name);
|
|
335
|
+
if (!plugin.ok) return plugin;
|
|
336
|
+
const profileRoot = crewProfileDir({ home });
|
|
337
|
+
const profile = readProfileManifestForRegistration(profileRoot);
|
|
338
|
+
if (profile.ok === false) return profile;
|
|
339
|
+
const scaffoldChanged = ensureProfileScaffold(profileRoot);
|
|
340
|
+
const current = profile.manifest;
|
|
341
|
+
const dependencies = { ...(current.dependencies ?? {}) };
|
|
342
|
+
const currentBundles = current.dsh?.profile?.bundles ?? [...CREW_PROFILE_DEFAULT_BUNDLES];
|
|
343
|
+
const nextBundles = [];
|
|
344
|
+
let seen = false;
|
|
345
|
+
for (const bundle of currentBundles) {
|
|
346
|
+
if (bundle === plugin.packageName) {
|
|
347
|
+
if (!seen) { nextBundles.push(bundle); seen = true; }
|
|
348
|
+
} else nextBundles.push(bundle);
|
|
349
|
+
}
|
|
350
|
+
if (!seen) nextBundles.push(plugin.packageName);
|
|
351
|
+
dependencies[plugin.packageName] = fileSpec(plugin.targetRoot);
|
|
352
|
+
const next = {
|
|
353
|
+
...current,
|
|
354
|
+
dependencies,
|
|
355
|
+
dsh: {
|
|
356
|
+
...(current.dsh ?? {}),
|
|
357
|
+
profile: { ...(current.dsh?.profile ?? {}), bundles: nextBundles },
|
|
358
|
+
},
|
|
359
|
+
};
|
|
360
|
+
const manifestChanged = JSON.stringify(current) !== JSON.stringify(next) || profile.created;
|
|
361
|
+
const linkPath = profileLinkPath(profileRoot, plugin.packageName);
|
|
362
|
+
let link;
|
|
363
|
+
try { link = ensureDirectoryLink(linkPath, plugin.targetRoot); } catch { return { ok: false, code: 'CREW_PLUGIN_LINK_FAILED' }; }
|
|
364
|
+
if (!link.ok) return link;
|
|
365
|
+
try {
|
|
366
|
+
if (manifestChanged) writeFileSync(profile.profileManifest, JSON.stringify(next, null, 2) + '\n');
|
|
367
|
+
// Resolve the intended package entry from the validated checkout. Node's
|
|
368
|
+
// package-name resolution cache can retain the old target after a stale
|
|
369
|
+
// junction is replaced in the same process, so loader visibility is proven
|
|
370
|
+
// by the link target plus a normal package entry resolution from that
|
|
371
|
+
// target rather than by trusting a cached package-name lookup.
|
|
372
|
+
const resolvedEntry = createRequire(join(plugin.targetRoot, 'package.json')).resolve('.');
|
|
373
|
+
const resolvedRoot = realpathSync(linkPath);
|
|
374
|
+
if (resolvedRoot !== plugin.targetRoot || !isWithin(plugin.targetRoot, realpathSync(resolvedEntry))) {
|
|
375
|
+
return { ok: false, code: 'CREW_PLUGIN_NOT_LOADABLE' };
|
|
376
|
+
}
|
|
377
|
+
return {
|
|
378
|
+
ok: true,
|
|
379
|
+
changed: scaffoldChanged || manifestChanged || link.changed,
|
|
380
|
+
profileRoot,
|
|
381
|
+
profileManifest: profile.profileManifest,
|
|
382
|
+
linkPath,
|
|
383
|
+
targetRoot: plugin.targetRoot,
|
|
384
|
+
packageName: plugin.packageName,
|
|
385
|
+
packageVersion: plugin.packageVersion,
|
|
386
|
+
patchPath: plugin.patchPath,
|
|
387
|
+
resolvedEntry,
|
|
388
|
+
};
|
|
389
|
+
} catch { return { ok: false, code: 'CREW_PLUGIN_NOT_LOADABLE' }; }
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/**
|
|
393
|
+
* Remove one Crew plugin registration without invoking a package manager.
|
|
394
|
+
* This is intentionally limited to the derived Crew profile directory so a
|
|
395
|
+
* status/uninstall probe cannot touch the official web profile or download
|
|
396
|
+
* anything merely to remove a stale registration.
|
|
397
|
+
*/
|
|
398
|
+
export function removeCrewPluginRegistration({ home = homedir(), name, profileRoot = crewProfileDir({ home }) } = {}) {
|
|
399
|
+
if (!isSafePackageName(name)) {
|
|
400
|
+
return { ok: false, code: 'INVALID_CREW_PLUGIN_NAME' };
|
|
401
|
+
}
|
|
402
|
+
const packageFile = join(profileRoot, 'package.json');
|
|
403
|
+
if (!existsSync(packageFile)) return { ok: true, removed: false };
|
|
404
|
+
let pkg;
|
|
405
|
+
try { pkg = JSON.parse(readFileSync(packageFile, 'utf8')); } catch { return { ok: false, code: 'CREW_PROFILE_METADATA_INVALID' }; }
|
|
406
|
+
if (!isObject(pkg)
|
|
407
|
+
|| (pkg.dependencies !== undefined && !isObject(pkg.dependencies))
|
|
408
|
+
|| (pkg.dsh !== undefined && !isObject(pkg.dsh))
|
|
409
|
+
|| (pkg.dsh?.profile !== undefined && !isObject(pkg.dsh.profile))
|
|
410
|
+
|| (pkg.dsh?.profile?.bundles !== undefined
|
|
411
|
+
&& (!Array.isArray(pkg.dsh.profile.bundles)
|
|
412
|
+
|| pkg.dsh.profile.bundles.some((item) => !isSafePackageName(item))))) {
|
|
413
|
+
return { ok: false, code: 'CREW_PROFILE_METADATA_INVALID' };
|
|
414
|
+
}
|
|
415
|
+
const hadDependency = Boolean(pkg.dependencies?.[name]);
|
|
416
|
+
const bundles = Array.isArray(pkg.dsh?.profile?.bundles) ? pkg.dsh.profile.bundles : [];
|
|
417
|
+
const hadBundle = bundles.includes(name);
|
|
418
|
+
if (!hadDependency && !hadBundle) return { ok: true, removed: false };
|
|
419
|
+
const linkPath = profileLinkPath(profileRoot, name);
|
|
420
|
+
let linkStat;
|
|
421
|
+
try { linkStat = lstatSync(linkPath); } catch { linkStat = null; }
|
|
422
|
+
if (linkStat && !linkStat.isSymbolicLink()) return { ok: false, code: 'CREW_PLUGIN_LINK_CONFLICT' };
|
|
423
|
+
const next = { ...pkg };
|
|
424
|
+
if (hadDependency) {
|
|
425
|
+
next.dependencies = { ...pkg.dependencies };
|
|
426
|
+
delete next.dependencies[name];
|
|
427
|
+
if (Object.keys(next.dependencies).length === 0) delete next.dependencies;
|
|
428
|
+
}
|
|
429
|
+
if (hadBundle) {
|
|
430
|
+
next.dsh = { ...pkg.dsh, profile: { ...pkg.dsh.profile, bundles: bundles.filter((item) => item !== name) } };
|
|
431
|
+
}
|
|
432
|
+
writeFileSync(packageFile, JSON.stringify(next, null, 2) + '\n');
|
|
433
|
+
if (linkStat) unlinkSync(linkPath);
|
|
434
|
+
return { ok: true, removed: true };
|
|
435
|
+
}
|