@routerhub/agent-rules 1.5.34 → 1.5.36
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/AGENTS.base.md +1 -0
- package/package.json +1 -1
- package/postinstall.js +46 -3
package/AGENTS.base.md
CHANGED
|
@@ -201,6 +201,7 @@ Closes #456
|
|
|
201
201
|
|
|
202
202
|
- 依赖安装统一 `pnpm`,禁止 `npm install`/`yarn install`。
|
|
203
203
|
- 写完代码先 `npm run format`,再 `npm run build`,构建通过才可发版。
|
|
204
|
+
- AI 改完代码后禁止自动执行任何格式化命令(包括但不限于 `npm run format`、`pnpm run format`、`prettier`、`eslint --fix` 等),避免产生仅含格式差异的文件变更,防止 PR 中混入大量与业务逻辑无关的格式化 diff;仅在用户明确要求格式化时才执行格式化操作。
|
|
204
205
|
|
|
205
206
|
## 测试
|
|
206
207
|
|
package/package.json
CHANGED
package/postinstall.js
CHANGED
|
@@ -4,12 +4,55 @@ 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
|
-
|
|
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");
|