@iyowei/rim-cli 0.0.0-stage → 1.0.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.
- package/LICENSE +21 -0
- package/README.md +134 -2
- package/README.zh-CN.md +135 -0
- package/bin/bunfig.toml +5 -0
- package/bin/rim.mjs +433 -0
- package/dist/cli.js +163 -0
- package/dist/manifest.json +7 -0
- package/package.json +62 -4
package/bin/rim.mjs
ADDED
|
@@ -0,0 +1,433 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* CLI 启动器 (@iyowei/rim-cli): 承接 npm 分发 `dist/cli.js` / 仓内开发回退 `src/cli.ts` / 单测直导导出函数,
|
|
4
|
+
* 挑选运行时 (Bun 优先, 其次 Node >= 22.18) 后启动真实入口。
|
|
5
|
+
*
|
|
6
|
+
* 安全防线与进程模型:
|
|
7
|
+
* 1. 防 cwd 劫持: win32 与 POSIX 均只从 PATH 绝对路径解析可执行文件 (win32 严判盘符或 UNC 绝对路径并仅认
|
|
8
|
+
* `.com`/`.exe`, 拦截 Git Bash 注入的 `/usr/bin` 盘根相对路径), 绝不从当前工作目录解析同名 bun/node;
|
|
9
|
+
* 2. Bun 工作目录配置隔离: 选中 Bun 时强制附加 `--config=<pkg>/bin/bunfig.toml` 与 `--no-env-file`
|
|
10
|
+
* (隔离配置缺失即报错拒绝, 不静默退回无隔离态), 并透传 `CLIS_BUN_ISOLATED=1`;
|
|
11
|
+
* 3. 产物自证: `dist/cli.js` 与 `dist/manifest.json` 校验 `schemaVersion === 1` 与 SHA-256,
|
|
12
|
+
* 不符即拒执行;
|
|
13
|
+
* 4. 零多余派生 + 信号收口: 当前已是隔离 Bun 或合格 Node (且无 Bun 可切) 时直接同进程 `import` 入口;
|
|
14
|
+
* 跨运行时切换以异步 `spawn` 派发: Windows 忽略 SIGINT 交由控制台原生 CTRL_C_EVENT 收口,
|
|
15
|
+
* POSIX 透传 SIGTERM/SIGHUP 并按 TTY 防重发策略转发 SIGINT (防挂死与孤儿), 子进程退出后解挂并透传信号或退出码。
|
|
16
|
+
*/
|
|
17
|
+
import { spawn, spawnSync } from 'node:child_process';
|
|
18
|
+
import { createHash } from 'node:crypto';
|
|
19
|
+
import { existsSync, readFileSync, realpathSync, statSync } from 'node:fs';
|
|
20
|
+
import { basename, dirname, join, posix, resolve, win32 } from 'node:path';
|
|
21
|
+
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
22
|
+
|
|
23
|
+
const CLI_FILE = 'cli.js';
|
|
24
|
+
const MANIFEST_FILE = 'manifest.json';
|
|
25
|
+
const WIN32_EXEC_EXTS = ['.com', '.exe'];
|
|
26
|
+
const WIN32_ABS_DIR_RE =
|
|
27
|
+
/^(?:[A-Za-z]:[/\\]|\\\\(?![?.][/\\])[^\\/]+[/\\][^\\/]+)/;
|
|
28
|
+
const MIN_NODE_MAJOR = 22;
|
|
29
|
+
const MIN_NODE_MINOR = 18;
|
|
30
|
+
|
|
31
|
+
/** 校验 Node.js 版本是否满足 >= 22.18.0 下限 */
|
|
32
|
+
export const isSupportedNodeVersion = (rawVersion) => {
|
|
33
|
+
if (typeof rawVersion !== 'string') return false;
|
|
34
|
+
const match = /^v?(\d+)\.(\d+)/.exec(rawVersion.trim());
|
|
35
|
+
if (match === null) return false;
|
|
36
|
+
const major = Number(match[1]);
|
|
37
|
+
const minor = Number(match[2]);
|
|
38
|
+
return (
|
|
39
|
+
major > MIN_NODE_MAJOR ||
|
|
40
|
+
(major === MIN_NODE_MAJOR && minor >= MIN_NODE_MINOR)
|
|
41
|
+
);
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/** 判定解析出的运行时是否为 Bun */
|
|
45
|
+
const isBunRuntime = (runtime) => {
|
|
46
|
+
const name = basename(runtime).toLowerCase();
|
|
47
|
+
return name === 'bun' || name === 'bun.exe' || name === 'bun.com';
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const UNSAFE_BUN_ENV_RE = /^(?:bun_options|bun_inspect_preload)$/i;
|
|
51
|
+
const stripUnsafeBunEnv = (baseEnv) =>
|
|
52
|
+
Object.fromEntries(
|
|
53
|
+
Object.entries(baseEnv).filter(
|
|
54
|
+
([k, v]) => v !== undefined && !UNSAFE_BUN_ENV_RE.test(k),
|
|
55
|
+
),
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
/** 探测命令是否可执行 (Bun 看退出码 0, Node 另校验 >= 22.18.0) */
|
|
59
|
+
const available = (command) => {
|
|
60
|
+
const isBun = isBunRuntime(command);
|
|
61
|
+
const res = spawnSync(command, ['--version'], {
|
|
62
|
+
encoding: 'utf8',
|
|
63
|
+
stdio: ['ignore', 'pipe', 'ignore'],
|
|
64
|
+
...(isBun ? { env: stripUnsafeBunEnv(process.env) } : undefined),
|
|
65
|
+
});
|
|
66
|
+
return res.status === 0 && (isBun || isSupportedNodeVersion(res.stdout));
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
/** 路径处是否为普通文件 */
|
|
70
|
+
const isFileAt = (path) => {
|
|
71
|
+
try {
|
|
72
|
+
return statSync(path).isFile();
|
|
73
|
+
} catch {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
/** 在 PATH 中解析命令的全部候选绝对路径 (win32 语义), 严格拦截空、相对与盘根相对条目并去重 */
|
|
79
|
+
export const resolveWin32Executables = (name, pathEnv, fileProbe) => {
|
|
80
|
+
const found = [];
|
|
81
|
+
const seen = new Set();
|
|
82
|
+
for (const raw of pathEnv.split(';')) {
|
|
83
|
+
const trimmed = raw.trim();
|
|
84
|
+
const unquoted =
|
|
85
|
+
trimmed.length > 1 && trimmed.startsWith('"') && trimmed.endsWith('"')
|
|
86
|
+
? trimmed.slice(1, -1).trim()
|
|
87
|
+
: trimmed;
|
|
88
|
+
if (unquoted === '' || !WIN32_ABS_DIR_RE.test(unquoted)) continue;
|
|
89
|
+
for (const ext of WIN32_EXEC_EXTS) {
|
|
90
|
+
const candidate = win32.join(unquoted, name + ext);
|
|
91
|
+
const key = candidate.toLowerCase();
|
|
92
|
+
if (seen.has(key)) continue;
|
|
93
|
+
if (fileProbe(candidate)) {
|
|
94
|
+
seen.add(key);
|
|
95
|
+
found.push(candidate);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return found;
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
/** 在 PATH 中解析命令的全部候选绝对路径 (POSIX 语义), 跳过空与相对条目并去重 */
|
|
103
|
+
export const resolvePosixExecutables = (name, pathEnv, fileProbe) => {
|
|
104
|
+
const found = [];
|
|
105
|
+
const seen = new Set();
|
|
106
|
+
for (const dir of pathEnv.split(':')) {
|
|
107
|
+
if (dir === '' || !dir.startsWith('/')) continue;
|
|
108
|
+
const candidate = posix.join(dir, name);
|
|
109
|
+
if (seen.has(candidate)) continue;
|
|
110
|
+
if (fileProbe(candidate)) {
|
|
111
|
+
seen.add(candidate);
|
|
112
|
+
found.push(candidate);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return found;
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
const defaultPathEnv = (platform) => {
|
|
119
|
+
if (process.env.PATH !== undefined && process.env.PATH !== '') {
|
|
120
|
+
return process.env.PATH;
|
|
121
|
+
}
|
|
122
|
+
if (platform === 'win32' && process.env.Path !== undefined) {
|
|
123
|
+
return process.env.Path;
|
|
124
|
+
}
|
|
125
|
+
return '';
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
/** 挑选运行时: 缺省 Bun 优先、其次 Node (>= 22.18); 皆无则 null */
|
|
129
|
+
export const pickRuntime = ({
|
|
130
|
+
platform = process.platform,
|
|
131
|
+
pathEnv = defaultPathEnv(platform),
|
|
132
|
+
fileProbe = isFileAt,
|
|
133
|
+
probe = available,
|
|
134
|
+
names = ['bun', 'node'],
|
|
135
|
+
} = {}) => {
|
|
136
|
+
for (const name of names) {
|
|
137
|
+
const candidates =
|
|
138
|
+
platform === 'win32'
|
|
139
|
+
? resolveWin32Executables(name, pathEnv, fileProbe)
|
|
140
|
+
: resolvePosixExecutables(name, pathEnv, fileProbe);
|
|
141
|
+
for (const candidate of candidates) {
|
|
142
|
+
if (probe(candidate)) return candidate;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return null;
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
/** 产物自证: 核对 dist/${CLI_FILE} 与 dist/manifest.json 的摘要是否同源 */
|
|
149
|
+
const artifactProblem = (distDir) => {
|
|
150
|
+
const manifestPath = join(distDir, MANIFEST_FILE);
|
|
151
|
+
if (!existsSync(manifestPath)) return `缺少产物清单 (dist/${MANIFEST_FILE})`;
|
|
152
|
+
let manifest;
|
|
153
|
+
try {
|
|
154
|
+
manifest = JSON.parse(readFileSync(manifestPath, 'utf8'));
|
|
155
|
+
} catch {
|
|
156
|
+
return `产物清单 (dist/${MANIFEST_FILE}) 损坏, 不是合法 JSON`;
|
|
157
|
+
}
|
|
158
|
+
if (manifest?.schemaVersion !== 1) {
|
|
159
|
+
return `产物清单 (dist/${MANIFEST_FILE}) 的 schemaVersion 不是受支持的版本 (期望 1)`;
|
|
160
|
+
}
|
|
161
|
+
const recorded = manifest?.cliSha256;
|
|
162
|
+
if (typeof recorded !== 'string') {
|
|
163
|
+
return `产物清单 (dist/${MANIFEST_FILE}) 缺少 cliSha256 字段`;
|
|
164
|
+
}
|
|
165
|
+
let actual;
|
|
166
|
+
try {
|
|
167
|
+
actual = createHash('sha256')
|
|
168
|
+
.update(readFileSync(join(distDir, CLI_FILE)))
|
|
169
|
+
.digest('hex');
|
|
170
|
+
} catch {
|
|
171
|
+
return `产物 (dist/${CLI_FILE}) 读不出来, 摘要算不出`;
|
|
172
|
+
}
|
|
173
|
+
if (recorded !== actual) {
|
|
174
|
+
return `产物 (dist/${CLI_FILE}) 的摘要与清单记录不符`;
|
|
175
|
+
}
|
|
176
|
+
return null;
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
const realpath = realpathSync.native ?? realpathSync;
|
|
180
|
+
const WIN32_PATH_RE = /^(?:[A-Za-z]:[/\\]|\\\\)/;
|
|
181
|
+
|
|
182
|
+
const stripWin32ExtendedPrefix = (p) => {
|
|
183
|
+
if (p.startsWith('\\\\?\\UNC\\') || p.startsWith('\\\\?\\unc\\')) {
|
|
184
|
+
return '\\\\' + p.slice(8);
|
|
185
|
+
}
|
|
186
|
+
if (p.startsWith('\\\\?\\')) return p.slice(4);
|
|
187
|
+
return p;
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
const fileUrlToFsPath = (url, platform = process.platform) => {
|
|
191
|
+
const isWinUrl =
|
|
192
|
+
typeof url === 'string' &&
|
|
193
|
+
(/^file:\/\/\/[A-Za-z]:/.test(url) ||
|
|
194
|
+
(platform === 'win32' &&
|
|
195
|
+
url.startsWith('file://') &&
|
|
196
|
+
!url.startsWith('file:///')));
|
|
197
|
+
return fileURLToPath(url, { windows: isWinUrl });
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
const normalizeEntryPath = (p, platform = process.platform) => {
|
|
201
|
+
const input = platform === 'win32' ? stripWin32ExtendedPrefix(p) : p;
|
|
202
|
+
let normalized = resolve(input);
|
|
203
|
+
if (platform !== 'win32' && input.startsWith('/')) {
|
|
204
|
+
normalized = posix.resolve(input);
|
|
205
|
+
} else if (WIN32_PATH_RE.test(input)) {
|
|
206
|
+
normalized = win32.resolve(input);
|
|
207
|
+
}
|
|
208
|
+
if (platform === process.platform) {
|
|
209
|
+
try {
|
|
210
|
+
normalized = realpath(normalized);
|
|
211
|
+
} catch {
|
|
212
|
+
// realpath 不可用时保留规范绝对路径做字面比对
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
const clean =
|
|
216
|
+
platform === 'win32' ? stripWin32ExtendedPrefix(normalized) : normalized;
|
|
217
|
+
return platform === 'win32' ? clean.toLowerCase() : clean;
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
/** 本文件是否作为入口被直接执行 (双端 resolve + realpath.native 归一 + win32 大小写折叠) */
|
|
221
|
+
export const isEntry = (
|
|
222
|
+
entryArg = process.argv[1],
|
|
223
|
+
selfUrl = import.meta.url,
|
|
224
|
+
platform = process.platform,
|
|
225
|
+
) => {
|
|
226
|
+
if (typeof entryArg !== 'string' || entryArg === '') return false;
|
|
227
|
+
const self = fileUrlToFsPath(selfUrl, platform);
|
|
228
|
+
return (
|
|
229
|
+
normalizeEntryPath(entryArg, platform) ===
|
|
230
|
+
normalizeEntryPath(self, platform)
|
|
231
|
+
);
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
/** 跨运行时异步派生子进程: POSIX 转发 SIGTERM/SIGHUP 并按 TTY 防重发转发 SIGINT, Windows 交由控制台原生 CTRL_C_EVENT, settled 防重入 */
|
|
235
|
+
const runChild = (command, args, extraEnv) =>
|
|
236
|
+
new Promise((resolveExit) => {
|
|
237
|
+
// 透传入口路径供更新引擎识别源码软链装态; 派生 Bun 时清除 BUN_OPTIONS / BUN_INSPECT_PRELOAD
|
|
238
|
+
const entryArgv1 = process.env.CLIS_ENTRY_ARGV1 || process.argv[1] || '';
|
|
239
|
+
const isBunCmd = isBunRuntime(command);
|
|
240
|
+
const baseEnv = isBunCmd ? stripUnsafeBunEnv(process.env) : process.env;
|
|
241
|
+
const childEnv =
|
|
242
|
+
isBunCmd || entryArgv1 !== '' || extraEnv
|
|
243
|
+
? {
|
|
244
|
+
...baseEnv,
|
|
245
|
+
...(entryArgv1 !== ''
|
|
246
|
+
? { CLIS_ENTRY_ARGV1: entryArgv1 }
|
|
247
|
+
: undefined),
|
|
248
|
+
...extraEnv,
|
|
249
|
+
}
|
|
250
|
+
: process.env;
|
|
251
|
+
const child = spawn(command, args, {
|
|
252
|
+
stdio: 'inherit',
|
|
253
|
+
env: childEnv,
|
|
254
|
+
});
|
|
255
|
+
let settled = false;
|
|
256
|
+
let sigintTimer = null;
|
|
257
|
+
const isWin32 = process.platform === 'win32';
|
|
258
|
+
const clearSigintTimer = () => {
|
|
259
|
+
if (sigintTimer !== null) {
|
|
260
|
+
clearTimeout(sigintTimer);
|
|
261
|
+
sigintTimer = null;
|
|
262
|
+
}
|
|
263
|
+
};
|
|
264
|
+
const forwardSignal = (sig) => {
|
|
265
|
+
if (settled) return;
|
|
266
|
+
try {
|
|
267
|
+
if (child.exitCode === null && child.signalCode === null) {
|
|
268
|
+
child.kill(sig);
|
|
269
|
+
}
|
|
270
|
+
} catch {
|
|
271
|
+
// 子进程已退出或当前平台不支持该信号时忽略, 等待 close 事件统一收口
|
|
272
|
+
}
|
|
273
|
+
};
|
|
274
|
+
// Windows 上 child.kill('SIGINT') 会退化为 TerminateProcess 强杀, 改交由原生控制台 CTRL_C_EVENT;
|
|
275
|
+
// POSIX 前台 TTY 单次 Ctrl+C 时内核已广播 SIGINT 至整个进程组, 延后 100ms 仅在子进程未退出 (如 raw-mode 外部 kill -INT) 时补发
|
|
276
|
+
const onSigInt = () => {
|
|
277
|
+
if (isWin32 || settled) return;
|
|
278
|
+
if (process.stdin.isTTY !== true) {
|
|
279
|
+
forwardSignal('SIGINT');
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
if (sigintTimer !== null) return;
|
|
283
|
+
sigintTimer = setTimeout(() => {
|
|
284
|
+
sigintTimer = null;
|
|
285
|
+
forwardSignal('SIGINT');
|
|
286
|
+
}, 100);
|
|
287
|
+
sigintTimer.unref?.();
|
|
288
|
+
};
|
|
289
|
+
const onSigTerm = () => forwardSignal('SIGTERM');
|
|
290
|
+
const onSigHup = () => forwardSignal('SIGHUP');
|
|
291
|
+
process.on('SIGINT', onSigInt);
|
|
292
|
+
if (!isWin32) {
|
|
293
|
+
process.on('SIGTERM', onSigTerm);
|
|
294
|
+
process.on('SIGHUP', onSigHup);
|
|
295
|
+
}
|
|
296
|
+
const finish = (cb) => {
|
|
297
|
+
if (settled) return;
|
|
298
|
+
settled = true;
|
|
299
|
+
clearSigintTimer();
|
|
300
|
+
process.off('SIGINT', onSigInt);
|
|
301
|
+
if (!isWin32) {
|
|
302
|
+
process.off('SIGTERM', onSigTerm);
|
|
303
|
+
process.off('SIGHUP', onSigHup);
|
|
304
|
+
}
|
|
305
|
+
cb();
|
|
306
|
+
resolveExit();
|
|
307
|
+
};
|
|
308
|
+
child.once('error', (error) => {
|
|
309
|
+
finish(() => {
|
|
310
|
+
const msg = `rim: 启动运行时失败 (${error.message})\n`;
|
|
311
|
+
process.stderr.write(msg);
|
|
312
|
+
process.exit(1);
|
|
313
|
+
});
|
|
314
|
+
});
|
|
315
|
+
child.once('close', (code, signal) => {
|
|
316
|
+
finish(() => {
|
|
317
|
+
if (signal) {
|
|
318
|
+
try {
|
|
319
|
+
process.kill(process.pid, signal);
|
|
320
|
+
} catch {
|
|
321
|
+
process.exit(1);
|
|
322
|
+
}
|
|
323
|
+
return;
|
|
324
|
+
}
|
|
325
|
+
process.exit(code ?? 1);
|
|
326
|
+
});
|
|
327
|
+
});
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
/** 启动器主流程: 入口定位与产物自证 -> 运行时分流 (隔离 Bun / 合格 Node / 旧 Node 探 PATH) -> 执行真实入口 */
|
|
331
|
+
const main = async () => {
|
|
332
|
+
const root = dirname(dirname(fileURLToPath(import.meta.url)));
|
|
333
|
+
const distDir = join(root, 'dist');
|
|
334
|
+
const built = join(distDir, CLI_FILE);
|
|
335
|
+
const source = join(root, 'src', 'cli.ts');
|
|
336
|
+
const hasBuilt = existsSync(built);
|
|
337
|
+
const hasSource = existsSync(source);
|
|
338
|
+
|
|
339
|
+
if (!hasBuilt && !hasSource) {
|
|
340
|
+
process.stderr.write(
|
|
341
|
+
`rim: 缺少可执行入口 (dist/${CLI_FILE} 与 src/cli.ts 均不存在)。\n` +
|
|
342
|
+
' 处置: 重装本包换一份完好分发 (npm install -g @iyowei/rim-cli); 本仓开发态请先执行 bun run build。\n',
|
|
343
|
+
);
|
|
344
|
+
process.exit(1);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
const problem = hasBuilt ? artifactProblem(distDir) : null;
|
|
348
|
+
if (problem !== null) {
|
|
349
|
+
const advice = hasSource
|
|
350
|
+
? '用源码入口跑 (bun packages/rim-cli/src/cli.ts 或 node packages/rim-cli/src/cli.ts), 或重新构建 (bun run build)'
|
|
351
|
+
: '重装本包换一份完好分发 (npm install -g @iyowei/rim-cli); 重装无效则说明该版本包有缺陷, 需发布者重新构建后再发';
|
|
352
|
+
process.stderr.write(
|
|
353
|
+
`rim: ${problem}, 已拒绝执行该产物 (无法证明它与构建清单同源)。\n` +
|
|
354
|
+
` 处置: ${advice}。\n`,
|
|
355
|
+
);
|
|
356
|
+
process.exit(1);
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
const entry = hasBuilt ? built : source;
|
|
360
|
+
const isolatedBunfig = join(root, 'bin', 'bunfig.toml');
|
|
361
|
+
const ensureIsolatedBunfig = () => {
|
|
362
|
+
if (existsSync(isolatedBunfig)) return;
|
|
363
|
+
process.stderr.write(
|
|
364
|
+
'rim: 缺少启动器隔离配置 (bin/bunfig.toml), 为防工作目录 bunfig.toml 劫持已拒绝执行。\n',
|
|
365
|
+
);
|
|
366
|
+
process.exit(1);
|
|
367
|
+
};
|
|
368
|
+
|
|
369
|
+
const bunArgs = (extra) => (extra[0] === '--' ? ['--', ...extra] : extra);
|
|
370
|
+
|
|
371
|
+
// 当前已是 Bun: 隔离态且无残留 Bun 劫持变量直接同进程 import, 否则用 process.execPath 重派隔离态 Bun (不扫 PATH)
|
|
372
|
+
if (typeof process.versions.bun === 'string') {
|
|
373
|
+
ensureIsolatedBunfig();
|
|
374
|
+
const hasUnsafeBunEnv = Object.keys(process.env).some((k) =>
|
|
375
|
+
UNSAFE_BUN_ENV_RE.test(k),
|
|
376
|
+
);
|
|
377
|
+
if (process.env.CLIS_BUN_ISOLATED === '1' && !hasUnsafeBunEnv) {
|
|
378
|
+
await import(pathToFileURL(entry).href);
|
|
379
|
+
return;
|
|
380
|
+
}
|
|
381
|
+
await runChild(
|
|
382
|
+
process.execPath,
|
|
383
|
+
[
|
|
384
|
+
`--config=${isolatedBunfig}`,
|
|
385
|
+
'--no-env-file',
|
|
386
|
+
entry,
|
|
387
|
+
...bunArgs(process.argv.slice(2)),
|
|
388
|
+
],
|
|
389
|
+
{ CLIS_BUN_ISOLATED: '1' },
|
|
390
|
+
);
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
const isCurrentNodeSupported = isSupportedNodeVersion(process.versions.node);
|
|
395
|
+
|
|
396
|
+
// 当前进程已是合格 Node 时仅探 Bun: 命中切 Bun, 未命中直接同进程 import (零额外派生)
|
|
397
|
+
const runtime = pickRuntime(
|
|
398
|
+
isCurrentNodeSupported ? { names: ['bun'] } : undefined,
|
|
399
|
+
);
|
|
400
|
+
if (runtime !== null && isBunRuntime(runtime)) {
|
|
401
|
+
ensureIsolatedBunfig();
|
|
402
|
+
await runChild(
|
|
403
|
+
runtime,
|
|
404
|
+
[
|
|
405
|
+
`--config=${isolatedBunfig}`,
|
|
406
|
+
'--no-env-file',
|
|
407
|
+
entry,
|
|
408
|
+
...bunArgs(process.argv.slice(2)),
|
|
409
|
+
],
|
|
410
|
+
{ CLIS_BUN_ISOLATED: '1' },
|
|
411
|
+
);
|
|
412
|
+
return;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
if (isCurrentNodeSupported) {
|
|
416
|
+
await import(pathToFileURL(entry).href);
|
|
417
|
+
return;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
if (runtime !== null) {
|
|
421
|
+
await runChild(runtime, [entry, ...process.argv.slice(2)]);
|
|
422
|
+
return;
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
process.stderr.write(
|
|
426
|
+
`rim: 未找到可用的 bun 或 node (>= ${MIN_NODE_MAJOR}.${MIN_NODE_MINOR}.0; 当前 node 为 ${process.versions.node ?? '未知'})\n`,
|
|
427
|
+
);
|
|
428
|
+
process.exit(1);
|
|
429
|
+
};
|
|
430
|
+
|
|
431
|
+
if (isEntry()) {
|
|
432
|
+
await main();
|
|
433
|
+
}
|