@hamsterbase/evidra 0.9.0 → 0.9.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hamsterbase/evidra",
3
- "version": "0.9.0",
3
+ "version": "0.9.1",
4
4
  "bin": {
5
5
  "evidra": "bin/evidra.mjs"
6
6
  },
@@ -18,6 +18,9 @@ description: 按需启动或复用当前项目的 Evidra 报告,默认仅本
18
18
 
19
19
  用户查看报告地址时,可先运行 `status.sh`;若未运行再运行 `share.sh` 启动本机与局域网报告。默认模式不会创建公网隧道(若公网隧道已在运行则直接复用显示);`--public` 模式启动或复用公网隧道,连续 30 分钟无公网访问时自动关闭;`--lan` 模式立即关闭该项目的公网隧道。
20
20
 
21
+ 脚本优先使用仓库根目录的 Evidra 依赖或源码入口,否则查找未被 Git 忽略的子项目中声明并安装的 `@hamsterbase/evidra`。只有一个候选时自动使用;多个候选时可在命令末尾传入子项目目录(例如 `playground`)来选择。报告的 `--repo` 始终是 Git 仓库根目录,无需为了启动报告在根目录新增依赖。
22
+
21
23
  - 成功时,把脚本的标准输出原样发给用户,保留密码两侧的 Markdown 反引号。
22
24
  - 退出码为 2 时,告诉用户缺少哪些工具,询问是否安装。用户同意后,由你安装缺少的工具并重新运行脚本。
25
+ - 本机 HTTP 探测因权限失败时,不代表端口冲突;脚本会保留 curl 诊断。可按当前执行环境的权限机制,在允许本机网络访问的环境中重试,不要因此结束进程或更换端口。
23
26
  - 其他失败时,转告错误信息后停下。不要全局安装、升级或构建 Evidra,不要结束其他程序。
@@ -53,14 +53,51 @@ SKILL_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)
53
53
  TMP=$(mktemp -d)
54
54
  trap 'rm -rf "$TMP"' EXIT
55
55
 
56
- # Evidra:优先项目依赖;Evidra 源码仓库本身用 bin/evidra.mjs,不自动构建
56
+ # Evidra:优先根目录依赖或源码入口,再查找子项目依赖;报告始终指向 ROOT。
57
57
  if [[ -x $ROOT/node_modules/.bin/evidra ]]; then
58
58
  BIN=("$ROOT/node_modules/.bin/evidra")
59
59
  elif [[ $(node -p 'require(process.argv[1]).name' "$ROOT/package.json" 2>/dev/null) == @hamsterbase/evidra ]]; then
60
60
  [[ -f $ROOT/dist/service/cli.js ]] || die "缺少 $ROOT/dist/service/cli.js,请先构建"
61
61
  BIN=(node "$ROOT/bin/evidra.mjs")
62
62
  else
63
- die "项目未安装 Evidra(没有 $ROOT/node_modules/.bin/evidra)"
63
+ bin=$(node --input-type=module - "$ROOT" "${1:-.}" <<'NODE'
64
+ import { accessSync, constants, readFileSync, realpathSync } from 'node:fs';
65
+ import { execFileSync } from 'node:child_process';
66
+ import path from 'node:path';
67
+
68
+ const root = process.argv[2];
69
+ const directory = realpathSync(process.argv[3]);
70
+ const files = execFileSync('git', ['-C', root, 'ls-files', '-z', '--cached', '--others', '--exclude-standard', '--', 'package.json', '**/package.json'], { encoding: 'utf8' });
71
+ const candidates = [...new Set(files.split('\0').filter(Boolean))].flatMap((file) => {
72
+ // 不使用被意外纳入 Git 的依赖、临时文件或其他仓库中的安装。
73
+ if (file.split('/').some((part) => part === 'node_modules' || part.startsWith('.'))) return [];
74
+ const manifest = path.join(root, file);
75
+ const bin = path.join(path.dirname(manifest), 'node_modules/.bin/evidra');
76
+ try {
77
+ const pkg = JSON.parse(readFileSync(manifest, 'utf8'));
78
+ if (![pkg.dependencies, pkg.devDependencies, pkg.optionalDependencies].some((deps) => deps?.['@hamsterbase/evidra'])) return [];
79
+ accessSync(bin, constants.X_OK);
80
+ return [bin];
81
+ } catch { return []; }
82
+ }).sort();
83
+ // 从子项目内调用或显式传入子项目目录时,使用最近的已安装依赖。
84
+ let current = directory;
85
+ while (current !== root && current.startsWith(root + path.sep)) {
86
+ const bin = path.join(current, 'node_modules/.bin/evidra');
87
+ if (candidates.includes(bin)) { process.stdout.write(bin); process.exit(0); }
88
+ current = path.dirname(current);
89
+ }
90
+ if (candidates.length === 1) {
91
+ process.stdout.write(candidates[0]);
92
+ } else {
93
+ console.error(candidates.length
94
+ ? `错误:发现多个子项目安装了 Evidra,请传入要使用的子项目目录:\n${candidates.map((bin) => path.dirname(path.dirname(path.dirname(bin)))).join('\n')}`
95
+ : `错误:项目未安装 Evidra(已检查仓库根目录和未被 Git 忽略的子项目依赖):${root}`);
96
+ process.exit(1);
97
+ }
98
+ NODE
99
+ ) || exit 1
100
+ BIN=("$bin")
64
101
  fi
65
102
  help=$("${BIN[@]}" start --help 2>&1 || true)
66
103
  [[ $help == *--password* ]] || die "项目的 Evidra 版本不支持 --password"
@@ -138,7 +175,19 @@ start_report() {
138
175
  sleep 0.5
139
176
  done
140
177
  }
141
- status=$(curl --noproxy '*' -s -o "$TMP/project" -w '%{http_code}' --max-time 5 "$LOCAL/api/project" || true)
178
+ curl_exit=0
179
+ status=$(LC_ALL=C curl --noproxy '*' -sS -v -o "$TMP/project" -w '%{http_code}' --max-time 5 "$LOCAL/api/project" 2>"$TMP/curl-error") || curl_exit=$?
180
+ if (( curl_exit != 0 )); then
181
+ if grep -Eq 'Operation not permitted|Permission denied' "$TMP/curl-error"; then
182
+ cat "$TMP/curl-error" >&2
183
+ die "当前执行环境禁止访问 ${LOCAL},请在允许本机网络访问的环境中重试"
184
+ fi
185
+ # 只有明确的连接拒绝且端口无人监听,才能判断报告尚未启动。
186
+ if (( curl_exit != 7 )) || ! grep -qi 'Connection refused' "$TMP/curl-error" || listening "$PORT"; then
187
+ cat "$TMP/curl-error" >&2
188
+ die "无法连接报告 ${LOCAL}(curl 退出码 ${curl_exit}),无法确认端口上的服务状态"
189
+ fi
190
+ fi
142
191
  case $status in
143
192
  401)
144
193
  root=$(login_repo_root) || die "端口 $PORT 上的 Evidra 不接受本项目的密码"
@@ -146,7 +195,6 @@ case $status in
146
195
  log "报告:复用"
147
196
  ;;
148
197
  000)
149
- ! listening "$PORT" || die "端口 $PORT 被其他程序占用(无 HTTP 响应)"
150
198
  start_report
151
199
  log "报告:已启动"
152
200
  ;;
@@ -44,7 +44,18 @@ login_repo_root() {
44
44
  }
45
45
 
46
46
  report_running=0
47
- status=$(curl --noproxy '*' -s -o "$TMP/project" -w '%{http_code}' --max-time 5 "$LOCAL/api/project" || true)
47
+ curl_exit=0
48
+ status=$(LC_ALL=C curl --noproxy '*' -sS -v -o "$TMP/project" -w '%{http_code}' --max-time 5 "$LOCAL/api/project" 2>"$TMP/curl-error") || curl_exit=$?
49
+ if (( curl_exit != 0 )); then
50
+ if grep -Eq 'Operation not permitted|Permission denied' "$TMP/curl-error"; then
51
+ cat "$TMP/curl-error" >&2
52
+ die "当前执行环境禁止访问 ${LOCAL},请在允许本机网络访问的环境中重试"
53
+ fi
54
+ if (( curl_exit != 7 )) || ! grep -qi 'Connection refused' "$TMP/curl-error"; then
55
+ cat "$TMP/curl-error" >&2
56
+ die "无法查询报告 ${LOCAL}(curl 退出码 ${curl_exit}),无法确认服务状态"
57
+ fi
58
+ fi
48
59
  case $status in
49
60
  401)
50
61
  if root=$(login_repo_root) && [[ $root == "$ROOT" ]]; then