prd-workflow-cli 1.1.25 → 1.1.27

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": "prd-workflow-cli",
3
- "version": "1.1.25",
3
+ "version": "1.1.27",
4
4
  "description": "产品需求管理规范 CLI 工具 - 基于 A→R→B→C 流程,集成 PM 确认机制和对话归档的需求管理命令行工具",
5
5
  "main": "index.js",
6
6
  "bin": {
@@ -2,14 +2,15 @@
2
2
 
3
3
  /**
4
4
  * postinstall 脚本
5
- * 在 npm install 完成后自动执行,更新项目中的 workflows 和规则文件
5
+ * 在 npm install 完成后自动执行,复制 workflows 和规则文件到项目中
6
6
  */
7
7
 
8
8
  const fs = require('fs');
9
9
  const path = require('path');
10
10
 
11
- // 静默模式运行(减少安装时的输出噪音)
11
+ // 静默模式运行
12
12
  const SILENT = process.env.PRD_POSTINSTALL_SILENT === 'true';
13
+ const DEBUG = process.env.PRD_DEBUG === 'true';
13
14
 
14
15
  function log(msg) {
15
16
  if (!SILENT) {
@@ -17,27 +18,14 @@ function log(msg) {
17
18
  }
18
19
  }
19
20
 
20
- function logError(msg) {
21
- // 错误始终输出
22
- console.error(msg);
23
- }
24
-
25
- /**
26
- * 获取项目根目录(向上查找包含 .prd-config.json 的目录)
27
- */
28
- function findProjectRoot(startDir) {
29
- let currentDir = startDir;
30
- const root = path.parse(currentDir).root;
31
-
32
- while (currentDir !== root) {
33
- const configPath = path.join(currentDir, '.prd-config.json');
34
- if (fs.existsSync(configPath)) {
35
- return currentDir;
36
- }
37
- currentDir = path.dirname(currentDir);
21
+ function debug(msg) {
22
+ if (DEBUG) {
23
+ console.log('[DEBUG]', msg);
38
24
  }
25
+ }
39
26
 
40
- return null;
27
+ function logError(msg) {
28
+ console.error(msg);
41
29
  }
42
30
 
43
31
  /**
@@ -57,28 +45,6 @@ function filesAreDifferent(sourcePath, targetPath) {
57
45
  }
58
46
  }
59
47
 
60
- /**
61
- * 递归复制目录
62
- */
63
- function copyDirSync(src, dest) {
64
- if (!fs.existsSync(dest)) {
65
- fs.mkdirSync(dest, { recursive: true });
66
- }
67
-
68
- const entries = fs.readdirSync(src, { withFileTypes: true });
69
-
70
- for (const entry of entries) {
71
- const srcPath = path.join(src, entry.name);
72
- const destPath = path.join(dest, entry.name);
73
-
74
- if (entry.isDirectory()) {
75
- copyDirSync(srcPath, destPath);
76
- } else {
77
- fs.copyFileSync(srcPath, destPath);
78
- }
79
- }
80
- }
81
-
82
48
  /**
83
49
  * 获取目录下所有文件(递归)
84
50
  */
@@ -107,31 +73,81 @@ function getAllFiles(dirPath, basePath = dirPath) {
107
73
  return files;
108
74
  }
109
75
 
76
+ /**
77
+ * 检查是否是全局安装
78
+ */
79
+ function isGlobalInstall(packagePath) {
80
+ // 全局安装的路径通常包含 lib/node_modules 或类似结构
81
+ // 本地安装的路径是 项目/node_modules/包名
82
+ const normalizedPath = packagePath.toLowerCase();
83
+
84
+ // 检查是否在项目的 node_modules 中
85
+ const pathParts = packagePath.split(path.sep);
86
+ const nodeModulesIndex = pathParts.lastIndexOf('node_modules');
87
+
88
+ if (nodeModulesIndex === -1) {
89
+ return true; // 不在 node_modules 中,可能是开发环境
90
+ }
91
+
92
+ // 检查 node_modules 前面是否有 lib 目录(全局安装的特征)
93
+ if (nodeModulesIndex > 0 && pathParts[nodeModulesIndex - 1] === 'lib') {
94
+ return true;
95
+ }
96
+
97
+ // 检查是否在 /usr/local 或 /opt 等系统目录下
98
+ if (normalizedPath.includes('/usr/') || normalizedPath.includes('/opt/')) {
99
+ return true;
100
+ }
101
+
102
+ return false;
103
+ }
104
+
110
105
  /**
111
106
  * 主函数
112
107
  */
113
108
  function main() {
114
109
  try {
115
- // 获取 npm 包的路径(当前脚本所在目录的上级)
110
+ // 获取 npm 包的路径
111
+ // 脚本路径: node_modules/prd-workflow-cli/scripts/postinstall.js
112
+ // __dirname = node_modules/prd-workflow-cli/scripts
113
+ // packagePath = node_modules/prd-workflow-cli
116
114
  const packagePath = path.join(__dirname, '..');
117
115
 
118
- // 尝试找到项目根目录
119
- // 通常 node_modules 在项目根目录下,所以向上查找
120
- const nodeModulesPath = path.dirname(path.dirname(packagePath));
121
- const potentialProjectRoot = path.dirname(nodeModulesPath);
116
+ debug(`packagePath = ${packagePath}`);
122
117
 
123
- // 检查是否是 PRD 项目
124
- const projectRoot = findProjectRoot(potentialProjectRoot);
118
+ // 检查是否是全局安装
119
+ if (isGlobalInstall(packagePath)) {
120
+ debug('检测到全局安装,跳过文件复制');
121
+ return;
122
+ }
123
+
124
+ // 计算项目根目录
125
+ // packagePath = /project/node_modules/prd-workflow-cli
126
+ // node_modules = /project/node_modules
127
+ // projectRoot = /project
128
+ const nodeModulesPath = path.dirname(packagePath);
129
+ const projectRoot = path.dirname(nodeModulesPath);
130
+
131
+ debug(`nodeModulesPath = ${nodeModulesPath}`);
132
+ debug(`projectRoot = ${projectRoot}`);
133
+
134
+ // 验证项目根目录存在
135
+ if (!fs.existsSync(projectRoot)) {
136
+ debug('项目根目录不存在,跳过');
137
+ return;
138
+ }
125
139
 
126
- if (!projectRoot) {
127
- // 不是 PRD 项目,静默退出(这是正常情况,比如全局安装)
140
+ // 检查是否有 package.json(确认是一个 npm 项目)
141
+ const packageJsonPath = path.join(projectRoot, 'package.json');
142
+ if (!fs.existsSync(packageJsonPath)) {
143
+ debug('项目根目录没有 package.json,跳过');
128
144
  return;
129
145
  }
130
146
 
131
- log('\n📦 prd-workflow-cli: 检测到 PRD 项目,正在更新配置文件...');
147
+ log('\n📦 prd-workflow-cli: 正在配置 AI 工作流文件...');
132
148
 
133
- // 定义需要更新的文件/目录
134
- const updateItems = [
149
+ // 定义需要复制的文件/目录
150
+ const copyItems = [
135
151
  {
136
152
  name: 'Workflows',
137
153
  source: '.agent/workflows',
@@ -161,11 +177,12 @@ function main() {
161
177
  let updatedCount = 0;
162
178
  let newCount = 0;
163
179
 
164
- for (const item of updateItems) {
180
+ for (const item of copyItems) {
165
181
  const sourcePath = path.join(packagePath, item.source);
166
182
  const targetPath = path.join(projectRoot, item.target);
167
183
 
168
184
  if (!fs.existsSync(sourcePath)) {
185
+ debug(`源文件不存在: ${sourcePath}`);
169
186
  continue;
170
187
  }
171
188
 
@@ -225,16 +242,20 @@ function main() {
225
242
  if (updatedCount > 0) {
226
243
  log(` 🔄 更新 ${updatedCount} 个文件`);
227
244
  }
228
- log(' ✅ 配置文件已同步到最新版本\n');
245
+ log(' ✅ AI 工作流配置完成!\n');
246
+ log(' 📁 已添加文件:');
247
+ log(' .agent/workflows/ - PRD 工作流指引');
248
+ log(' .cursorrules - Cursor AI 规则');
249
+ log(' .antigravity/ - Antigravity AI 规则');
250
+ log(' AI-GUIDE.md - AI 使用指南\n');
229
251
  } else {
230
252
  log(' ✅ 所有配置文件已是最新版本\n');
231
253
  }
232
254
 
233
255
  } catch (error) {
234
256
  // postinstall 失败不应该阻止安装
235
- // 只输出警告,不抛出错误
236
- logError(`\n⚠️ prd-workflow-cli: 自动更新配置文件失败: ${error.message}`);
237
- logError(' 您可以稍后手动运行 "prd upgrade" 来更新配置文件\n');
257
+ logError(`\n⚠️ prd-workflow-cli: 配置文件复制失败: ${error.message}`);
258
+ logError(' 您可以手动运行 "npx prd upgrade" 来完成配置\n');
238
259
  }
239
260
  }
240
261