@routerhub/agent-rules 1.5.33 → 1.5.35

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 (2) hide show
  1. package/package.json +1 -1
  2. package/postinstall.js +58 -3
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@routerhub/agent-rules",
3
- "version": "1.5.33",
3
+ "version": "1.5.35",
4
4
  "description": "Shared Copilot agent rules and guidelines for RouterHub projects",
5
5
  "main": "AGENTS.base.md",
6
6
  "bin": {
package/postinstall.js CHANGED
@@ -4,18 +4,73 @@ const { spawn } = require("child_process");
4
4
  const path = require("path");
5
5
  const fs = require("fs");
6
6
 
7
- // 获取下游项目根目录(INIT_CWD 是 npm/pnpm 执行 install 时的原始目录)
8
- const projectRoot = process.env.INIT_CWD || process.cwd();
9
7
  const packageRoot = __dirname;
10
8
  const mergeScriptPath = path.join(packageRoot, "merge.js");
11
9
 
12
- // 安全检查:如果项目根目录在 node_modules 内,说明 INIT_CWD 未正确设置,跳过初始化
10
+ /**
11
+ * 查找下游项目的根目录,兼容 npm / pnpm / yarn 等包管理器
12
+ *
13
+ * 查找策略(按优先级):
14
+ * 1. INIT_CWD — npm 和 pnpm (v8+) 在执行 install 脚本时设置
15
+ * 2. 从 __dirname 向上遍历,找到 node_modules 的父目录即为项目根目录
16
+ * (适用于 pnpm 旧版本或 INIT_CWD 未正确设置的场景)
17
+ * 3. process.cwd() — 最终兜底
18
+ */
19
+ function findProjectRoot() {
20
+ // 1. 优先使用 INIT_CWD(npm / pnpm v8+ 均会设置)
21
+ const initCwd = process.env.INIT_CWD;
22
+ if (initCwd && !initCwd.includes("node_modules")) {
23
+ return initCwd;
24
+ }
25
+
26
+ // 2. 从包安装路径向上遍历,定位项目根目录
27
+ // 例如 __dirname = /Users/x/project/node_modules/@routerhub/agent-rules
28
+ // 向上找到 node_modules 目录,其父目录就是项目根
29
+ let dir = packageRoot;
30
+ while (true) {
31
+ const parentDir = path.dirname(dir);
32
+ // 到达文件系统根目录,无法继续向上
33
+ if (parentDir === dir) {
34
+ break;
35
+ }
36
+
37
+ // 当前目录名为 node_modules 且父目录包含 package.json → 找到项目根
38
+ if (path.basename(dir) === "node_modules") {
39
+ const candidate = parentDir;
40
+ if (fs.existsSync(path.join(candidate, "package.json"))) {
41
+ return candidate;
42
+ }
43
+ }
44
+
45
+ dir = parentDir;
46
+ }
47
+
48
+ // 3. 最终兜底:使用当前工作目录
49
+ return process.cwd();
50
+ }
51
+
52
+ // 获取下游项目根目录
53
+ const projectRoot = findProjectRoot();
54
+
55
+ // 安全检查:如果项目根目录仍在 node_modules 内,跳过初始化
13
56
  if (projectRoot.includes("node_modules")) {
14
57
  console.log("[agent-rules] 检测到在 node_modules 内运行,跳过自动初始化");
15
58
  console.log("[agent-rules] 请手动执行: npx agent-rules init");
16
59
  process.exit(0);
17
60
  }
18
61
 
62
+ // 确保下游项目 .npmrc 中关闭 frozen-lockfile,让 pnpm install 能直接在版本更新后执行
63
+ const npmrcPath = path.join(projectRoot, ".npmrc");
64
+ let npmrcContent = "";
65
+ if (fs.existsSync(npmrcPath)) {
66
+ npmrcContent = fs.readFileSync(npmrcPath, "utf-8");
67
+ }
68
+ if (!npmrcContent.includes("frozen-lockfile")) {
69
+ const newEntry = "\n# 允许 pnpm install 在依赖版本更新后直接执行(由 agent-rules 自动添加)\nfrozen-lockfile=false\n";
70
+ fs.appendFileSync(npmrcPath, newEntry);
71
+ console.log("[agent-rules] 已配置 .npmrc: frozen-lockfile=false");
72
+ }
73
+
19
74
  console.log("");
20
75
  console.log("[agent-rules] 安装完成,正在同步规则文件...");
21
76