@routerhub/agent-rules 1.5.52 → 1.5.53

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 (3) hide show
  1. package/merge.js +22 -0
  2. package/package.json +1 -1
  3. package/postinstall.js +44 -5
package/merge.js CHANGED
@@ -48,6 +48,26 @@ function ensureParentDir(filePath) {
48
48
  fs.mkdirSync(parentDir, { recursive: true });
49
49
  }
50
50
 
51
+ /**
52
+ * 更新 .agent-rules-version 版本标记文件
53
+ * 供 postinstall 判断已安装版本是否与上次同步版本一致,避免重复同步
54
+ */
55
+ function updateVersionMarker() {
56
+ try {
57
+ const currentVersion = JSON.parse(
58
+ fs.readFileSync(path.join(packageRoot, "package.json"), "utf-8")
59
+ ).version;
60
+ fs.writeFileSync(
61
+ path.join(currentRoot, ".agent-rules-version"),
62
+ currentVersion,
63
+ "utf-8"
64
+ );
65
+ } catch (e) {
66
+ // 非关键操作,写入失败不阻塞主流程
67
+ console.warn("[agent-rules] 版本标记写入失败:", e.message);
68
+ }
69
+ }
70
+
51
71
  function resolveOutputPaths(config) {
52
72
  if (Array.isArray(config.outputs) && config.outputs.length > 0) {
53
73
  return config.outputs;
@@ -517,6 +537,7 @@ function initAgents() {
517
537
  const privateOutputPath = path.join(currentRoot, "AGENTS.private.md");
518
538
 
519
539
  mergeAgents(getDefaultConfig());
540
+ updateVersionMarker();
520
541
 
521
542
  if (!fs.existsSync(privateOutputPath) && fs.existsSync(privateTemplatePath)) {
522
543
  fs.copyFileSync(privateTemplatePath, privateOutputPath);
@@ -656,6 +677,7 @@ function main() {
656
677
 
657
678
  if (command === "sync") {
658
679
  mergeAgents(getDefaultConfig());
680
+ updateVersionMarker();
659
681
  return;
660
682
  }
661
683
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@routerhub/agent-rules",
3
- "version": "1.5.52",
3
+ "version": "1.5.53",
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
@@ -59,17 +59,54 @@ if (projectRoot.includes("node_modules")) {
59
59
  process.exit(0);
60
60
  }
61
61
 
62
- // 确保下游项目 .npmrc 中关闭 frozen-lockfile,让 pnpm install 能直接在版本更新后执行
62
+ // 确保下游项目 .npmrc 中关闭 frozen-lockfile + 启用依赖 pre/post 脚本
63
+ // frozen-lockfile=false:让 pnpm install 能在版本更新后直接执行
64
+ // enable-pre-post-scripts=true:让 pnpm 始终执行依赖包的 postinstall 生命周期脚本,
65
+ // 否则 pnpm 默认不会运行依赖的 postinstall,导致 agent-rules 更新后规则文件不刷新
63
66
  const npmrcPath = path.join(projectRoot, ".npmrc");
64
67
  let npmrcContent = "";
65
68
  if (fs.existsSync(npmrcPath)) {
66
69
  npmrcContent = fs.readFileSync(npmrcPath, "utf-8");
67
70
  }
71
+ const npmrcEntries = [];
68
72
  if (!npmrcContent.includes("frozen-lockfile")) {
69
- const newEntry =
70
- "\n# 允许 pnpm install 在依赖版本更新后直接执行(由 agent-rules 自动添加)\nfrozen-lockfile=false\n";
73
+ npmrcEntries.push("# 允许 pnpm install 在依赖版本更新后直接执行(由 agent-rules 自动添加)");
74
+ npmrcEntries.push("frozen-lockfile=false");
75
+ }
76
+ if (!npmrcContent.includes("enable-pre-post-scripts")) {
77
+ npmrcEntries.push("# 确保依赖包的 postinstall 生命周期脚本始终执行(由 agent-rules 自动添加)");
78
+ npmrcEntries.push("enable-pre-post-scripts=true");
79
+ }
80
+ if (npmrcEntries.length > 0) {
81
+ const newEntry = "\n" + npmrcEntries.join("\n") + "\n";
71
82
  fs.appendFileSync(npmrcPath, newEntry);
72
- console.log("[agent-rules] 已配置 .npmrc: frozen-lockfile=false");
83
+ console.log("[agent-rules] 已配置 .npmrc: " + npmrcEntries.filter((e) => !e.startsWith("#")).join(", "));
84
+ }
85
+
86
+ // 版本标记机制:比较已安装版本与上次同步的版本
87
+ // 相同 → 跳过同步(避免每次 pnpm install 都重复生成)
88
+ // 不同/不存在 → 执行同步并更新版本标记
89
+ const versionMarkerPath = path.join(projectRoot, ".agent-rules-version");
90
+ const currentVersion = JSON.parse(
91
+ fs.readFileSync(path.join(packageRoot, "package.json"), "utf-8")
92
+ ).version;
93
+ let needsSync = true;
94
+
95
+ if (fs.existsSync(versionMarkerPath)) {
96
+ const lastVersion = fs.readFileSync(versionMarkerPath, "utf-8").trim();
97
+ if (lastVersion === currentVersion) {
98
+ needsSync = false;
99
+ console.log("[agent-rules] 版本未变更 (" + currentVersion + "),跳过同步");
100
+ } else {
101
+ console.log("[agent-rules] 版本变更: " + lastVersion + " → " + currentVersion + ",执行同步...");
102
+ }
103
+ } else {
104
+ console.log("[agent-rules] 首次安装 (v" + currentVersion + "),执行同步...");
105
+ }
106
+
107
+ if (!needsSync) {
108
+ console.log("");
109
+ process.exit(0);
73
110
  }
74
111
 
75
112
  console.log("");
@@ -83,7 +120,9 @@ const syncProcess = spawn("node", [mergeScriptPath, "init"], {
83
120
 
84
121
  syncProcess.on("close", (code) => {
85
122
  if (code === 0) {
86
- console.log("[agent-rules] 规则文件初始化完成");
123
+ // 同步成功后写入版本标记,下次 postinstall 时比较版本号避免重复同步
124
+ fs.writeFileSync(versionMarkerPath, currentVersion, "utf-8");
125
+ console.log("[agent-rules] 规则文件初始化完成 (v" + currentVersion + ")");
87
126
  console.log("");
88
127
  } else {
89
128
  console.error("[agent-rules] 初始化失败,退出码:", code);