create-clis 1.0.0 → 1.1.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/README.md +22 -14
- package/README.zh-CN.md +22 -14
- package/assets/manifest.json +7 -2
- package/assets/template/.github/ISSUE_TEMPLATE/bug_report.yml +15 -29
- package/assets/template/.github/ISSUE_TEMPLATE/feature_request.yml +6 -12
- package/assets/template/.github/PULL_REQUEST_TEMPLATE.md +3 -3
- package/assets/template/.github/workflows/ci.yml +69 -7
- package/assets/template/.github/workflows/release.yml +23 -23
- package/assets/template/.gitignorerc.json +1 -8
- package/assets/template/.oxlintrc.json +1 -6
- package/assets/template/.prettierignore +0 -1
- package/assets/template/.releaserc.json +6 -1
- package/assets/template/.vscode/launch.json +1 -1
- package/assets/template/.vscode/settings.json +1 -0
- package/assets/template/.vscode/tasks.json +45 -0
- package/assets/template/AGENTS.md +4 -4
- package/assets/template/CONTRIBUTING.md +6 -4
- package/assets/template/CONTRIBUTING.zh-CN.md +6 -4
- package/assets/template/_gitignore +7 -405
- package/assets/template/bunfig.toml +2 -11
- package/assets/template/docs/README.md +6 -6
- package/assets/template/docs/adrs/README.md +4 -2
- package/assets/template/docs/development.md +15 -9
- package/assets/template/lefthook.yml +20 -30
- package/assets/template/package.json +5 -6
- package/assets/template/packages/{{NAME}}/README.md +4 -4
- package/assets/template/packages/{{NAME}}/README.zh-CN.md +2 -2
- package/assets/template/packages/{{NAME}}/package.json +3 -1
- package/assets/template/packages/{{NAME}}/scripts/build.ts +50 -14
- package/assets/template/packages/{{NAME}}/scripts/verify-release.test.ts +140 -15
- package/assets/template/packages/{{NAME}}/scripts/verify-release.ts +27 -14
- package/assets/template/packages/{{NAME}}/tsconfig.build.json +1 -1
- package/assets/template/packages/{{NAME}}-cli/README.md +6 -6
- package/assets/template/packages/{{NAME}}-cli/README.zh-CN.md +2 -2
- package/assets/template/packages/{{NAME}}-cli/bin/bunfig.toml +3 -0
- package/assets/template/packages/{{NAME}}-cli/bin/{{BIN_NAME}} +43 -21
- package/assets/template/packages/{{NAME}}-cli/bin/{{BIN_NAME}}.cmd +8 -17
- package/assets/template/packages/{{NAME}}-cli/bin/{{BIN_NAME}}.d.mts +6 -3
- package/assets/template/packages/{{NAME}}-cli/bin/{{BIN_NAME}}.mjs +189 -107
- package/assets/template/packages/{{NAME}}-cli/package.json +3 -2
- package/assets/template/packages/{{NAME}}-cli/scripts/release-artifact.ts +24 -73
- package/assets/template/packages/{{NAME}}-cli/scripts/release.fixtures.ts +8 -3
- package/assets/template/packages/{{NAME}}-cli/scripts/verify-release.test.ts +16 -10
- package/assets/template/packages/{{NAME}}-cli/scripts/verify-release.ts +8 -52
- package/assets/template/packages/{{NAME}}-cli/scripts/write-dist-manifest.test.ts +3 -3
- package/assets/template/packages/{{NAME}}-cli/scripts/write-dist-manifest.ts +8 -68
- package/assets/template/scripts/ci.ts +285 -64
- package/assets/template/scripts/install-git-hooks.mjs +10 -0
- package/assets/template/scripts/install-git-hooks.test.ts +1 -1
- package/assets/template/scripts/lib/npm-trust-parse.ts +169 -0
- package/assets/template/scripts/lib/release-cli.ts +299 -0
- package/assets/template/scripts/lib/release-verify.ts +148 -15
- package/assets/template/scripts/lib/workspace.test.ts +63 -1
- package/assets/template/scripts/lib/workspace.ts +132 -42
- package/assets/template/scripts/lint-dependency-direction.test.ts +131 -0
- package/assets/template/scripts/lint-dependency-direction.ts +130 -0
- package/assets/template/scripts/lint-doc-examples.test.ts +45 -9
- package/assets/template/scripts/lint-doc-examples.ts +227 -50
- package/assets/template/scripts/lint-doc-references.test.ts +36 -0
- package/assets/template/scripts/lint-doc-references.ts +142 -57
- package/assets/template/scripts/lint-doc-shared.ts +26 -23
- package/assets/template/scripts/lint-stray-backups.test.ts +3 -1
- package/assets/template/scripts/lint-stray-backups.ts +7 -3
- package/assets/template/scripts/npm-trust-guard.test.ts +56 -8
- package/assets/template/scripts/npm-trust-guard.ts +116 -200
- package/assets/template/scripts/safe-install.ts +28 -23
- package/assets/template/scripts/transcription/api-harness.ts +28 -206
- package/assets/template/scripts/transcription/compare.test.ts +84 -0
- package/assets/template/scripts/transcription/compare.ts +4 -4
- package/assets/template/scripts/transcription/corpus.ts +9 -3
- package/assets/template/scripts/transcription/fixture.ts +2 -4
- package/assets/template/scripts/transcription/make-mutants.ts +116 -37
- package/assets/template/scripts/transcription/run-conformance.ts +57 -34
- package/assets/template/scripts/transcription/validate-coverage.test.ts +11 -14
- package/assets/template/scripts/transcription/validate-coverage.ts +8 -10
- package/assets/template/scripts/transcription/validate.ts +18 -3
- package/assets/template/tsconfig.json +2 -20
- package/assets/template/turbo.jsonc +7 -52
- package/bin/bunfig.toml +3 -0
- package/bin/create-clis.mjs +165 -104
- package/dist/create-clis.js +48 -87681
- package/dist/manifest.json +3 -3
- package/package.json +9 -5
- package/assets/template/packages/{{NAME}}/docs/README.md +0 -21
- package/assets/template/packages/{{NAME}}-cli/docs/README.md +0 -21
- package/bin/create-clis +0 -35
- package/bin/create-clis.cmd +0 -32
|
@@ -1,29 +1,51 @@
|
|
|
1
1
|
#!/bin/sh
|
|
2
|
-
# {{BIN_NAME}}
|
|
3
|
-
# 经 ~/.local/bin/{{BIN_NAME}} 软链调用; readlink
|
|
4
|
-
#
|
|
5
|
-
#
|
|
6
|
-
# 本入口的 command -v 与 exec 都按 shell 的 PATH 查找; shell 层无法过滤 PATH 中的空 / 相对
|
|
7
|
-
# 条目 (两者在 POSIX 下均意为当前目录), 该边界属已知前提: npm 分发入口 (bin/{{BIN_NAME}}.mjs)
|
|
8
|
-
# 已按 PATH 解析绝对路径并跳过这两类条目, 防御落在那一侧。对照实测与本条的证据面见
|
|
9
|
-
# src/launcher.runtime-resolution.test.ts; win32 侧的对应处理见 bin/{{BIN_NAME}}.cmd 与
|
|
10
|
-
# bin/{{BIN_NAME}}.mjs 的文件头。
|
|
2
|
+
# {{BIN_NAME}} 启动器 (POSIX): 挑选运行时 (Bun 优先, 其次 Node) 后 exec 真实入口。
|
|
3
|
+
# 经 ~/.local/bin/{{BIN_NAME}} 软链调用; 循环 readlink 解析多级符号链接 (40 跳上限防环)。
|
|
4
|
+
# 按 `:` 仅扫描 PATH 中以 `/` 开头的绝对目录, 跳过空段与相对目录防当前目录劫持。
|
|
5
|
+
# 选中 Bun 时强制附加 --config="$root/bin/bunfig.toml" --no-env-file 并透传 CLIS_BUN_ISOLATED=1。
|
|
11
6
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
7
|
+
target=$0
|
|
8
|
+
hops=0
|
|
9
|
+
while [ -L "$target" ]; do
|
|
10
|
+
hops=$((hops + 1))
|
|
11
|
+
if [ "$hops" -gt 40 ]; then
|
|
12
|
+
echo "{{BIN_NAME}}: 符号链接解析超过 40 跳上限, 已中止" >&2
|
|
13
|
+
exit 1
|
|
14
|
+
fi
|
|
15
|
+
link=$(readlink "$target" 2>/dev/null) || break
|
|
16
|
+
case "$link" in
|
|
17
|
+
/*) target=$link ;;
|
|
18
|
+
*) target="$(dirname "$target")/$link" ;;
|
|
19
|
+
esac
|
|
20
|
+
done
|
|
21
|
+
root=$(CDPATH= cd -- "$(dirname "$target")/.." && pwd)
|
|
20
22
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
+
find_abs_exec() {
|
|
24
|
+
cmd=$1
|
|
25
|
+
rest="${PATH:-}:"
|
|
26
|
+
while [ -n "$rest" ]; do
|
|
27
|
+
dir=${rest%%:*}
|
|
28
|
+
rest=${rest#*:}
|
|
29
|
+
case "$dir" in
|
|
30
|
+
/*)
|
|
31
|
+
cand="${dir%/}/$cmd"
|
|
32
|
+
if [ -f "$cand" ] && [ -x "$cand" ]; then
|
|
33
|
+
printf '%s\n' "$cand"
|
|
34
|
+
return 0
|
|
35
|
+
fi
|
|
36
|
+
;;
|
|
37
|
+
esac
|
|
38
|
+
done
|
|
39
|
+
return 1
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if bun_bin=$(find_abs_exec bun); then
|
|
43
|
+
export CLIS_BUN_ISOLATED=1
|
|
44
|
+
exec "$bun_bin" --config="$root/bin/bunfig.toml" --no-env-file "$root/src/cli.ts" "$@"
|
|
23
45
|
fi
|
|
24
46
|
|
|
25
|
-
if
|
|
26
|
-
exec
|
|
47
|
+
if node_bin=$(find_abs_exec node); then
|
|
48
|
+
exec "$node_bin" "$root/src/cli.ts" "$@"
|
|
27
49
|
fi
|
|
28
50
|
|
|
29
51
|
echo "{{BIN_NAME}}: 未找到 bun 或 node, 请至少安装其一" >&2
|
|
@@ -1,30 +1,21 @@
|
|
|
1
1
|
@echo off
|
|
2
2
|
rem {{BIN_NAME}} 启动器 (Windows): 挑选运行时 (Bun 优先, 其次 Node) 后启动真实入口。
|
|
3
|
-
rem
|
|
4
|
-
rem
|
|
5
|
-
rem
|
|
6
|
-
rem 运行时解析防劫持 (安全审计项 runtime-resolution:bare-name-cwd-precedence-windows):
|
|
7
|
-
rem 裸名执行与 where 的默认搜索都含当前目录 (官方文档: by default, where searches the current
|
|
8
|
-
rem directory and the paths that are specified in the PATH environment variable; path 命令页:
|
|
9
|
-
rem the current directory is always searched before the directories specified in the command path),
|
|
10
|
-
rem 而本工具又在被扫目录里执行 —— 目录内放一个同名 bun.exe / bun.cmd 即可顶替真实运行时。
|
|
11
|
-
rem 故改用 for 的 PATH 展开修饰符 (官方 for 文档: Searches the directories listed in the PATH
|
|
12
|
-
rem environment variable and expands 变量 to the fully qualified name of the first directory
|
|
13
|
-
rem found): 只搜 PATH, 不搜当前目录, 也不派生任何子进程; 解析出的绝对路径交给末行执行, 带路径
|
|
14
|
-
rem 的命令不再触发搜索序。候选扩展名取 .com / .exe: 扩展名相对序 (.com 先于 .exe) 与 libuv 一致;
|
|
15
|
-
rem 搜索粒度则不同 —— 本实现扩展名优先 (.com 扫完全部 PATH 目录再试 .exe), npm 启动器
|
|
16
|
-
rem (bin/{{BIN_NAME}}.mjs, 走 Node 的 libuv 搜索) 目录优先 (逐目录内先 .com 再 .exe); 仅当多个 PATH 目录
|
|
17
|
-
rem 分别存在不同扩展名的 bun / node 时, 三入口可能选中不同二进制 (登记见 packages/{{NAME}}/docs/adrs/0007-platform-portability.md)。
|
|
3
|
+
rem 全程只用单行 if 判定 (不用标签与括号块), 防回退 Node 时误传 Bun 参数。
|
|
4
|
+
rem 使用 for 的 PATH 展开修饰符 (只搜 PATH, 不搜当前目录, 见 ADR 0007)。
|
|
18
5
|
setlocal
|
|
19
6
|
set "ROOT=%~dp0.."
|
|
20
7
|
set "ENTRY=%ROOT%\src\cli.ts"
|
|
8
|
+
set "BUNFIG=%ROOT%\bin\bunfig.toml"
|
|
21
9
|
set "RUNNER="
|
|
10
|
+
set "IS_BUN="
|
|
22
11
|
|
|
23
12
|
for %%I in (bun.com bun.exe) do if not defined RUNNER set "RUNNER=%%~$PATH:I"
|
|
13
|
+
if defined RUNNER set "IS_BUN=1"
|
|
24
14
|
if not defined RUNNER for %%I in (node.com node.exe) do if not defined RUNNER set "RUNNER=%%~$PATH:I"
|
|
25
15
|
|
|
26
|
-
if not defined RUNNER echo {{BIN_NAME}}:
|
|
16
|
+
if not defined RUNNER echo {{BIN_NAME}}: bun or node not found in PATH 1>&2
|
|
27
17
|
if not defined RUNNER exit /b 1
|
|
28
18
|
|
|
29
|
-
"%RUNNER%" "%ENTRY%" %*
|
|
19
|
+
if defined IS_BUN "%RUNNER%" "--config=%BUNFIG%" --no-env-file "%ENTRY%" %*
|
|
20
|
+
if not defined IS_BUN "%RUNNER%" "%ENTRY%" %*
|
|
30
21
|
exit /b %errorlevel%
|
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* bin/{{BIN_NAME}}.mjs 的类型投影: 分发入口是纯 JS (node 直跑, 不进 tsc 编译面), 此声明只为让
|
|
3
|
-
* src/launcher.runtime-resolution.test.ts 能带类型地 import
|
|
3
|
+
* src/launcher.runtime-resolution.test.ts 能带类型地 import 其中的导出。
|
|
4
4
|
* 签名与实现须同步; 实现本体与语义说明见 bin/{{BIN_NAME}}.mjs。
|
|
5
5
|
*/
|
|
6
|
-
export declare const
|
|
6
|
+
export declare const isSupportedNodeVersion: (rawVersion: unknown) => boolean;
|
|
7
|
+
|
|
8
|
+
export declare const resolveWin32Executables: (
|
|
7
9
|
name: string,
|
|
8
10
|
pathEnv: string,
|
|
9
11
|
fileProbe: (candidate: string) => boolean,
|
|
10
|
-
) => string
|
|
12
|
+
) => string[];
|
|
11
13
|
|
|
12
14
|
export declare const resolvePosixExecutables: (
|
|
13
15
|
name: string,
|
|
@@ -20,4 +22,5 @@ export declare const pickRuntime: (options?: {
|
|
|
20
22
|
pathEnv?: string;
|
|
21
23
|
fileProbe?: (candidate: string) => boolean;
|
|
22
24
|
probe?: (command: string) => boolean;
|
|
25
|
+
names?: readonly string[];
|
|
23
26
|
}) => string | null;
|
|
@@ -1,53 +1,62 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
/**
|
|
3
|
-
* npm 安装场景的启动器: 挑选运行时 (Bun 优先, 其次 Node) 后启动真实入口。
|
|
4
|
-
* 与 bin/{{BIN_NAME}} (sh) / bin/{{BIN_NAME}}.cmd (cmd) 职责同逻辑 (挑选运行时: Bun 优先, Node 回退),
|
|
5
|
-
* 差异在宿主与本文件多加的入口选择 (优先跑编译产物 dist/cli.js, 无产物则回退源码 src/cli.ts;
|
|
6
|
-
* 包内只有产物, 开发态通常无产物):
|
|
7
|
-
* 本文件同时承担 Unix 与 Windows 的 npm shim 目标 (shebang 必须是 node, 否则
|
|
8
|
-
* npm 的 cmd-shim 会按 shebang 解释器生成 Windows 上不存在的调用)。
|
|
3
|
+
* npm 安装场景的启动器: 挑选运行时 (Bun 优先, 其次 Node >= 22.18) 后启动真实入口。
|
|
9
4
|
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
* (
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
* dist/cli.js 之前必须与随附的 dist/manifest.json 对账 (形状版本受支持, 且清单记的 cliSha256
|
|
21
|
-
* 必须等于实测摘要), 清单缺失 / 损坏 / 版本不支持 / 摘要不符一律拒收产物并给出指引, 不静默回退
|
|
22
|
-
* 源码 (静默回退会把产物漂移藏起来, 包内也没有源码可回退); 拒收指引按宿主分流 (仓库检出态给
|
|
23
|
-
* 源码入口与重构建, 包态给重装本包)。工作树脏净不进本判定 (开发态常脏), 那是发布闸门
|
|
24
|
-
* scripts/verify-release.ts 的职责; 清单字段契约见 scripts/release-artifact.ts (包内无法
|
|
25
|
-
* 跨语言复用该模块, 两边口径由 src/npm-launcher.smoke.test.ts 的集成用例钉住)。
|
|
26
|
-
*
|
|
27
|
-
* 退出码原样透传; 两个运行时都缺席时给出可操作提示并非零退出。
|
|
5
|
+
* 安全防线与进程模型:
|
|
6
|
+
* 1. 运行时解析防劫持: win32 与 POSIX 均按 PATH 解析绝对路径并跳过空/相对条目
|
|
7
|
+
* (win32 严判盘符或 UNC 绝对路径, 拦截 Git Bash 注入的 `/usr/bin` 盘根相对路径),
|
|
8
|
+
* 绝不从当前被扫目录解析同名 bun/node;
|
|
9
|
+
* 2. Bun 工作目录配置隔离: 选中 Bun 时强制附加 --config=<pkg>/bin/bunfig.toml 与
|
|
10
|
+
* --no-env-file (隔离配置缺失即报错拒绝, 不静默退回无隔离态), 并透传 CLIS_BUN_ISOLATED=1;
|
|
11
|
+
* 3. 产物自证: 执行 dist/cli.js 前必先与 dist/manifest.json 校验 SHA-256;
|
|
12
|
+
* 4. 零多余派生 + 信号收口: 当前进程已是隔离 Bun 或满足 >= 22.18 的纯 Node 时直接同进程
|
|
13
|
+
* import 入口 (保留原始 argv[1]); 需跨运行时切换时以异步 spawn 派发, 运行期父进程忽略
|
|
14
|
+
* SIGINT 防杀父留孤儿, 子进程退出后解挂并透传信号或退出码。
|
|
28
15
|
*/
|
|
29
|
-
import { spawnSync } from 'node:child_process';
|
|
16
|
+
import { spawn, spawnSync } from 'node:child_process';
|
|
30
17
|
import { createHash } from 'node:crypto';
|
|
31
18
|
import { existsSync, readFileSync, realpathSync, statSync } from 'node:fs';
|
|
32
|
-
import { dirname, join, posix, win32 } from 'node:path';
|
|
33
|
-
import { fileURLToPath } from 'node:url';
|
|
19
|
+
import { basename, dirname, join, posix, win32 } from 'node:path';
|
|
20
|
+
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
34
21
|
|
|
35
|
-
/** 产物与清单的文件名 (与 scripts/release-artifact.ts 的 CLI_FILE / MANIFEST_FILE 同值) */
|
|
36
22
|
const CLI_FILE = 'cli.js';
|
|
37
23
|
const MANIFEST_FILE = 'manifest.json';
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* win32 的候选可执行扩展名, 顺序与 Node 实际走的 libuv 搜索一致 (.com 先于 .exe)。
|
|
41
|
-
* 不展开 PATHEXT: .bat / .cmd 无法被 Node 无 shell 直接执行, 展开只会把不可执行的命中当成功;
|
|
42
|
-
* 保持该面与 libuv 等价也意味着本次收紧不改动 win32 上「哪些安装形态能命中」的既有行为。
|
|
43
|
-
*/
|
|
44
24
|
const WIN32_EXEC_EXTS = ['.com', '.exe'];
|
|
25
|
+
const WIN32_ABS_DIR_RE = /^(?:[A-Za-z]:[/\\]|\\\\[^\\]+\\[^\\]+)/;
|
|
26
|
+
const MIN_NODE_MAJOR = 22;
|
|
27
|
+
const MIN_NODE_MINOR = 18;
|
|
28
|
+
|
|
29
|
+
/** 校验 Node.js 版本是否满足 >= 22.18.0 下限 */
|
|
30
|
+
export const isSupportedNodeVersion = (rawVersion) => {
|
|
31
|
+
if (typeof rawVersion !== 'string') return false;
|
|
32
|
+
const match = /^v?(\d+)\.(\d+)/.exec(rawVersion.trim());
|
|
33
|
+
if (match === null) return false;
|
|
34
|
+
const major = Number(match[1]);
|
|
35
|
+
const minor = Number(match[2]);
|
|
36
|
+
return (
|
|
37
|
+
major > MIN_NODE_MAJOR ||
|
|
38
|
+
(major === MIN_NODE_MAJOR && minor >= MIN_NODE_MINOR)
|
|
39
|
+
);
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
/** 判定解析出的运行时是否为 Bun */
|
|
43
|
+
const isBunRuntime = (runtime) => {
|
|
44
|
+
const name = basename(runtime).toLowerCase();
|
|
45
|
+
return name === 'bun' || name === 'bun.exe' || name === 'bun.com';
|
|
46
|
+
};
|
|
45
47
|
|
|
46
|
-
/** 探测命令是否可执行 (
|
|
47
|
-
const available = (command) =>
|
|
48
|
-
spawnSync(command, ['--version'], {
|
|
48
|
+
/** 探测命令是否可执行 (Bun 看退出码 0, Node 另校验 >= 22.18.0) */
|
|
49
|
+
const available = (command) => {
|
|
50
|
+
const res = spawnSync(command, ['--version'], {
|
|
51
|
+
encoding: 'utf8',
|
|
52
|
+
stdio: ['ignore', 'pipe', 'ignore'],
|
|
53
|
+
});
|
|
54
|
+
if (res.status !== 0) return false;
|
|
55
|
+
if (isBunRuntime(command)) return true;
|
|
56
|
+
return isSupportedNodeVersion(res.stdout);
|
|
57
|
+
};
|
|
49
58
|
|
|
50
|
-
/** 路径处是否为普通文件
|
|
59
|
+
/** 路径处是否为普通文件 */
|
|
51
60
|
const isFileAt = (path) => {
|
|
52
61
|
try {
|
|
53
62
|
return statSync(path).isFile();
|
|
@@ -56,35 +65,25 @@ const isFileAt = (path) => {
|
|
|
56
65
|
}
|
|
57
66
|
};
|
|
58
67
|
|
|
59
|
-
/**
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
* 「当前目录」从解析面整个摘掉 —— 空条目 (Windows 上表示当前目录) 与相对条目 (同样按当前
|
|
63
|
-
* 目录解析) 一律跳过, 只认绝对目录; 条目两侧的引号按 libuv 同款剥掉 (Windows PATH 允许
|
|
64
|
-
* 引号包裹含空格的目录)。注入 pathEnv / fileProbe 供测试在非 win32 宿主上覆盖本函数。
|
|
65
|
-
*/
|
|
66
|
-
export const resolveWin32Executable = (name, pathEnv, fileProbe) => {
|
|
68
|
+
/** 在 PATH 中解析命令的全部候选绝对路径 (win32 语义), 严格拦截空、相对与盘根相对条目 */
|
|
69
|
+
export const resolveWin32Executables = (name, pathEnv, fileProbe) => {
|
|
70
|
+
const found = [];
|
|
67
71
|
for (const raw of pathEnv.split(';')) {
|
|
68
|
-
const
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
72
|
+
const trimmed = raw.trim();
|
|
73
|
+
const unquoted =
|
|
74
|
+
trimmed.length > 1 && trimmed.startsWith('"') && trimmed.endsWith('"')
|
|
75
|
+
? trimmed.slice(1, -1).trim()
|
|
76
|
+
: trimmed;
|
|
77
|
+
if (unquoted === '' || !WIN32_ABS_DIR_RE.test(unquoted)) continue;
|
|
73
78
|
for (const ext of WIN32_EXEC_EXTS) {
|
|
74
|
-
const candidate = win32.join(
|
|
75
|
-
if (fileProbe(candidate))
|
|
79
|
+
const candidate = win32.join(unquoted, name + ext);
|
|
80
|
+
if (fileProbe(candidate)) found.push(candidate);
|
|
76
81
|
}
|
|
77
82
|
}
|
|
78
|
-
return
|
|
83
|
+
return found;
|
|
79
84
|
};
|
|
80
85
|
|
|
81
|
-
/**
|
|
82
|
-
* 在 PATH 中解析命令的全部候选绝对路径 (POSIX 语义, 保持 PATH 顺序), 跳过空条目与相对
|
|
83
|
-
* 条目 (两者在 POSIX 下都按当前目录解析, 属本工具要摘掉的解析面; 与 win32 分支同一理由,
|
|
84
|
-
* 见文件头「运行时解析防劫持」)。返回数组而非首命中: 同名文件可能不可执行, execvp 在
|
|
85
|
-
* 该情形会继续搜索, 候选交由调用方逐个探测以保持同一语义。注入 pathEnv / fileProbe 供
|
|
86
|
-
* 测试在任意宿主上覆盖本函数。
|
|
87
|
-
*/
|
|
86
|
+
/** 在 PATH 中解析命令的全部候选绝对路径 (POSIX 语义), 跳过空与相对条目 */
|
|
88
87
|
export const resolvePosixExecutables = (name, pathEnv, fileProbe) => {
|
|
89
88
|
const found = [];
|
|
90
89
|
for (const dir of pathEnv.split(':')) {
|
|
@@ -95,36 +94,27 @@ export const resolvePosixExecutables = (name, pathEnv, fileProbe) => {
|
|
|
95
94
|
return found;
|
|
96
95
|
};
|
|
97
96
|
|
|
98
|
-
/**
|
|
99
|
-
* 挑选运行时: Bun 优先, 其次 Node; 皆无则 null (由调用处报错退出)。
|
|
100
|
-
* 两个平台都先按 PATH 解析绝对路径再探测 (理由见文件头「运行时解析防劫持」): win32 取
|
|
101
|
-
* 首个解析命中, POSIX 按 PATH 顺序逐个候选探测 (保持 execvp 的继续搜索语义); 各参数
|
|
102
|
-
* 显式传入供测试注入, 生产调用不传。
|
|
103
|
-
*/
|
|
97
|
+
/** 挑选运行时: 缺省 Bun 优先、其次 Node (>= 22.18); 皆无则 null */
|
|
104
98
|
export const pickRuntime = ({
|
|
105
99
|
platform = process.platform,
|
|
106
|
-
pathEnv = process.env.PATH ?? '',
|
|
100
|
+
pathEnv = process.env.PATH ?? process.env.Path ?? '',
|
|
107
101
|
fileProbe = isFileAt,
|
|
108
102
|
probe = available,
|
|
103
|
+
names = ['bun', 'node'],
|
|
109
104
|
} = {}) => {
|
|
110
|
-
for (const name of
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
for (const candidate of resolvePosixExecutables(name, pathEnv, fileProbe)) {
|
|
105
|
+
for (const name of names) {
|
|
106
|
+
const candidates =
|
|
107
|
+
platform === 'win32'
|
|
108
|
+
? resolveWin32Executables(name, pathEnv, fileProbe)
|
|
109
|
+
: resolvePosixExecutables(name, pathEnv, fileProbe);
|
|
110
|
+
for (const candidate of candidates) {
|
|
117
111
|
if (probe(candidate)) return candidate;
|
|
118
112
|
}
|
|
119
113
|
}
|
|
120
114
|
return null;
|
|
121
115
|
};
|
|
122
116
|
|
|
123
|
-
/**
|
|
124
|
-
* 产物自证: 核对 dist/cli.js 与 dist/manifest.json 的摘要是否同源。
|
|
125
|
-
* 通过返回 null, 否则返回人话原因; 原因串全是本文件固定措辞, 不回显清单内容 (清单是外部
|
|
126
|
-
* 文件, 其文本不受控, 不落进输出面)。
|
|
127
|
-
*/
|
|
117
|
+
/** 产物自证: 核对 dist/cli.js 与 dist/manifest.json 的摘要是否同源 */
|
|
128
118
|
const artifactProblem = (distDir) => {
|
|
129
119
|
const manifestPath = join(distDir, MANIFEST_FILE);
|
|
130
120
|
if (!existsSync(manifestPath)) return `缺少产物清单 (dist/${MANIFEST_FILE})`;
|
|
@@ -134,8 +124,6 @@ const artifactProblem = (distDir) => {
|
|
|
134
124
|
} catch {
|
|
135
125
|
return `产物清单 (dist/${MANIFEST_FILE}) 损坏, 不是合法 JSON`;
|
|
136
126
|
}
|
|
137
|
-
// 形状版本先对: 异版本的字段语义无保证, 宁可拒收 (字面量 1 与 scripts/release-artifact.ts 的
|
|
138
|
-
// MANIFEST_SCHEMA_VERSION 同值, 改动须两处同步; 启动器随包分发, 包内无法 import 该模块)
|
|
139
127
|
if (manifest?.schemaVersion !== 1) {
|
|
140
128
|
return `产物清单 (dist/${MANIFEST_FILE}) 的 schemaVersion 不是受支持的版本 (期望 1)`;
|
|
141
129
|
}
|
|
@@ -151,40 +139,81 @@ const artifactProblem = (distDir) => {
|
|
|
151
139
|
} catch {
|
|
152
140
|
return `产物 (dist/${CLI_FILE}) 读不出来, 摘要算不出`;
|
|
153
141
|
}
|
|
154
|
-
if (recorded !== actual)
|
|
142
|
+
if (recorded !== actual) {
|
|
155
143
|
return `产物 (dist/${CLI_FILE}) 的摘要与清单记录不符`;
|
|
144
|
+
}
|
|
156
145
|
return null;
|
|
157
146
|
};
|
|
158
147
|
|
|
159
|
-
/**
|
|
160
|
-
* 本文件是否作为入口被直接执行。
|
|
161
|
-
* 经 realpath 比对而非字面路径: npm 在 Unix 上以符号链接 (或经 shim) 落 bin, 此时 argv[1] 是
|
|
162
|
-
* 链接路径而 import.meta.url 是解析后的真实路径。main 守卫只为让测试 import 本模块取函数时
|
|
163
|
-
* 不触发主流程, 判定失配的后果是入口静默不跑, 故除字面相等外只在 realpath 成功时采信。
|
|
164
|
-
*/
|
|
148
|
+
/** 本文件是否作为入口被直接执行 (双端 realpath.native 展开 win32 8.3 短路径 + 大小写归一) */
|
|
165
149
|
const isEntry = () => {
|
|
166
150
|
const entry = process.argv[1];
|
|
167
151
|
if (typeof entry !== 'string' || entry === '') return false;
|
|
168
152
|
const self = fileURLToPath(import.meta.url);
|
|
169
|
-
|
|
153
|
+
const norm = (value) =>
|
|
154
|
+
process.platform === 'win32' ? value.toLowerCase() : value;
|
|
155
|
+
if (norm(entry) === norm(self)) return true;
|
|
170
156
|
try {
|
|
171
|
-
|
|
157
|
+
const realpath = realpathSync.native ?? realpathSync;
|
|
158
|
+
const realSelf = realpath(self);
|
|
159
|
+
return norm(realpath(entry)) === norm(realSelf);
|
|
172
160
|
} catch {
|
|
173
161
|
return false;
|
|
174
162
|
}
|
|
175
163
|
};
|
|
176
164
|
|
|
177
|
-
|
|
165
|
+
/** 跨运行时异步派生子进程: 运行期父进程忽略 SIGINT 防孤儿, 退出后卸载监听并透传信号或退出码 */
|
|
166
|
+
const runChild = (command, args, extraEnv) =>
|
|
167
|
+
new Promise((resolve) => {
|
|
168
|
+
const ignoreSigint = () => {};
|
|
169
|
+
process.on('SIGINT', ignoreSigint);
|
|
170
|
+
const child = spawn(command, args, {
|
|
171
|
+
stdio: 'inherit',
|
|
172
|
+
env: extraEnv ? { ...process.env, ...extraEnv } : process.env,
|
|
173
|
+
});
|
|
174
|
+
const cleanup = () => {
|
|
175
|
+
process.off('SIGINT', ignoreSigint);
|
|
176
|
+
};
|
|
177
|
+
child.on('error', (error) => {
|
|
178
|
+
cleanup();
|
|
179
|
+
process.stderr.write(`{{BIN_NAME}}: 启动运行时失败 (${error.message})\n`);
|
|
180
|
+
process.exit(1);
|
|
181
|
+
});
|
|
182
|
+
child.on('exit', (code, signal) => {
|
|
183
|
+
cleanup();
|
|
184
|
+
if (signal) {
|
|
185
|
+
try {
|
|
186
|
+
process.kill(process.pid, signal);
|
|
187
|
+
} catch {
|
|
188
|
+
process.exit(1);
|
|
189
|
+
}
|
|
190
|
+
resolve();
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
process.exit(code ?? 1);
|
|
194
|
+
});
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
const main = async () => {
|
|
178
198
|
const root = dirname(dirname(fileURLToPath(import.meta.url)));
|
|
179
199
|
const distDir = join(root, 'dist');
|
|
180
200
|
const built = join(distDir, CLI_FILE);
|
|
201
|
+
const source = join(root, 'src', 'cli.ts');
|
|
181
202
|
const hasBuilt = existsSync(built);
|
|
182
|
-
|
|
203
|
+
const hasSource = existsSync(source);
|
|
204
|
+
|
|
205
|
+
if (!hasBuilt && !hasSource) {
|
|
206
|
+
process.stderr.write(
|
|
207
|
+
`{{BIN_NAME}}: 缺少可执行入口 (dist/${CLI_FILE} 与 src/cli.ts 均不存在)。\n` +
|
|
208
|
+
' 处置: 重装本包换一份完好分发 (npm install -g {{SCOPE}}/{{NAME}}-cli); 本仓开发态请先执行 bun run build。\n',
|
|
209
|
+
);
|
|
210
|
+
process.exit(1);
|
|
211
|
+
}
|
|
212
|
+
|
|
183
213
|
const problem = hasBuilt ? artifactProblem(distDir) : null;
|
|
184
214
|
if (problem !== null) {
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
? '用源码入口跑 (bun src/cli.ts 或 node src/cli.ts), 或重新构建 (bun run build)'
|
|
215
|
+
const advice = hasSource
|
|
216
|
+
? '用源码入口跑 (bun packages/{{NAME}}-cli/src/cli.ts 或 node packages/{{NAME}}-cli/src/cli.ts), 或重新构建 (bun run build)'
|
|
188
217
|
: '重装本包换一份完好分发 (npm install -g {{SCOPE}}/{{NAME}}-cli); 重装无效则说明该版本包有缺陷, 需发布者重新构建后再发';
|
|
189
218
|
process.stderr.write(
|
|
190
219
|
`{{BIN_NAME}}: ${problem}, 已拒绝执行该产物 (无法证明它与构建清单同源)。\n` +
|
|
@@ -192,21 +221,74 @@ const main = () => {
|
|
|
192
221
|
);
|
|
193
222
|
process.exit(1);
|
|
194
223
|
}
|
|
195
|
-
// npm 包内是编译产物 (node 拒绝对 node_modules 内的 TS 做类型剥离, 见 package.json 的 build 脚本);
|
|
196
|
-
// 仓库开发态无 dist, 回退直跑源码 (bun / node 皆可)
|
|
197
|
-
const entry = hasBuilt ? built : join(root, 'src', 'cli.ts');
|
|
198
|
-
const runtime = pickRuntime();
|
|
199
224
|
|
|
200
|
-
|
|
201
|
-
|
|
225
|
+
const entry = hasBuilt ? built : source;
|
|
226
|
+
const isolatedBunfig = join(root, 'bin', 'bunfig.toml');
|
|
227
|
+
const ensureIsolatedBunfig = () => {
|
|
228
|
+
if (existsSync(isolatedBunfig)) return;
|
|
229
|
+
process.stderr.write(
|
|
230
|
+
'{{BIN_NAME}}: 缺少启动器隔离配置 (bin/bunfig.toml), 为防工作目录 bunfig.toml 劫持已拒绝执行。\n',
|
|
231
|
+
);
|
|
202
232
|
process.exit(1);
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
// 当前已是 Bun: 隔离态直接同进程 import, 非隔离态直接用 process.execPath 重派隔离态 Bun (不扫 PATH)
|
|
236
|
+
if (typeof process.versions.bun === 'string') {
|
|
237
|
+
ensureIsolatedBunfig();
|
|
238
|
+
if (process.env.CLIS_BUN_ISOLATED === '1') {
|
|
239
|
+
await import(pathToFileURL(entry).href);
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
await runChild(
|
|
243
|
+
process.execPath,
|
|
244
|
+
[
|
|
245
|
+
`--config=${isolatedBunfig}`,
|
|
246
|
+
'--no-env-file',
|
|
247
|
+
entry,
|
|
248
|
+
...process.argv.slice(2),
|
|
249
|
+
],
|
|
250
|
+
{ CLIS_BUN_ISOLATED: '1' },
|
|
251
|
+
);
|
|
252
|
+
return;
|
|
203
253
|
}
|
|
204
254
|
|
|
205
|
-
const
|
|
206
|
-
|
|
207
|
-
|
|
255
|
+
const isCurrentNodeSupported = isSupportedNodeVersion(process.versions.node);
|
|
256
|
+
|
|
257
|
+
// 当前进程已是合格 Node 时仅探 Bun: 命中切 Bun, 未命中直接同进程 import (零额外派生)
|
|
258
|
+
const runtime = pickRuntime(
|
|
259
|
+
isCurrentNodeSupported ? { names: ['bun'] } : undefined,
|
|
260
|
+
);
|
|
261
|
+
if (runtime !== null && isBunRuntime(runtime)) {
|
|
262
|
+
ensureIsolatedBunfig();
|
|
263
|
+
await runChild(
|
|
264
|
+
runtime,
|
|
265
|
+
[
|
|
266
|
+
`--config=${isolatedBunfig}`,
|
|
267
|
+
'--no-env-file',
|
|
268
|
+
entry,
|
|
269
|
+
...process.argv.slice(2),
|
|
270
|
+
],
|
|
271
|
+
{ CLIS_BUN_ISOLATED: '1' },
|
|
272
|
+
);
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
if (isCurrentNodeSupported) {
|
|
277
|
+
await import(pathToFileURL(entry).href);
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
if (runtime !== null) {
|
|
282
|
+
await runChild(runtime, [entry, ...process.argv.slice(2)]);
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
208
285
|
|
|
209
|
-
process.
|
|
286
|
+
process.stderr.write(
|
|
287
|
+
`{{BIN_NAME}}: 未找到可用的 bun 或 node (>= ${MIN_NODE_MAJOR}.${MIN_NODE_MINOR}.0; 当前 node 为 ${process.versions.node ?? '未知'})\n`,
|
|
288
|
+
);
|
|
289
|
+
process.exit(1);
|
|
210
290
|
};
|
|
211
291
|
|
|
212
|
-
if (isEntry())
|
|
292
|
+
if (isEntry()) {
|
|
293
|
+
await main();
|
|
294
|
+
}
|
|
@@ -26,6 +26,7 @@
|
|
|
26
26
|
},
|
|
27
27
|
"files": [
|
|
28
28
|
"bin/{{BIN_NAME}}.mjs",
|
|
29
|
+
"bin/bunfig.toml",
|
|
29
30
|
"dist"
|
|
30
31
|
],
|
|
31
32
|
"engines": {
|
|
@@ -39,10 +40,10 @@
|
|
|
39
40
|
"{{SCOPE}}/{{NAME}}": "0.0.0"
|
|
40
41
|
},
|
|
41
42
|
"scripts": {
|
|
42
|
-
"build": "bun build src/cli.ts --target=node --outfile=dist/cli.js && bun scripts/write-dist-manifest.ts",
|
|
43
|
+
"build": "bun build src/cli.ts --target=node --minify-whitespace --minify-syntax --outfile=dist/cli.js && bun scripts/write-dist-manifest.ts",
|
|
43
44
|
"prepublishOnly": "npm run build && npm run verify:release",
|
|
44
45
|
"verify:release": "bun scripts/verify-release.ts",
|
|
45
|
-
"typecheck": "
|
|
46
|
+
"typecheck": "bun check",
|
|
46
47
|
"test": "bun test"
|
|
47
48
|
}
|
|
48
49
|
}
|