prd-workflow-cli 1.1.26 → 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.26",
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,44 +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 包的路径
116
111
  // 脚本路径: node_modules/prd-workflow-cli/scripts/postinstall.js
117
112
  // __dirname = node_modules/prd-workflow-cli/scripts
118
113
  // packagePath = node_modules/prd-workflow-cli
119
114
  const packagePath = path.join(__dirname, '..');
120
115
 
121
- // 项目根目录的推断:
116
+ debug(`packagePath = ${packagePath}`);
117
+
118
+ // 检查是否是全局安装
119
+ if (isGlobalInstall(packagePath)) {
120
+ debug('检测到全局安装,跳过文件复制');
121
+ return;
122
+ }
123
+
124
+ // 计算项目根目录
122
125
  // packagePath = /project/node_modules/prd-workflow-cli
123
126
  // node_modules = /project/node_modules
124
127
  // projectRoot = /project
125
128
  const nodeModulesPath = path.dirname(packagePath);
126
- const potentialProjectRoot = path.dirname(nodeModulesPath);
127
-
128
- // 调试信息(仅在非静默模式下显示)
129
- if (!SILENT && process.env.PRD_DEBUG === 'true') {
130
- console.log('DEBUG: __dirname =', __dirname);
131
- console.log('DEBUG: packagePath =', packagePath);
132
- console.log('DEBUG: nodeModulesPath =', nodeModulesPath);
133
- console.log('DEBUG: potentialProjectRoot =', potentialProjectRoot);
134
- }
129
+ const projectRoot = path.dirname(nodeModulesPath);
130
+
131
+ debug(`nodeModulesPath = ${nodeModulesPath}`);
132
+ debug(`projectRoot = ${projectRoot}`);
135
133
 
136
- // 检查是否是 PRD 项目
137
- const projectRoot = findProjectRoot(potentialProjectRoot);
134
+ // 验证项目根目录存在
135
+ if (!fs.existsSync(projectRoot)) {
136
+ debug('项目根目录不存在,跳过');
137
+ return;
138
+ }
138
139
 
139
- if (!projectRoot) {
140
- // 不是 PRD 项目,静默退出(这是正常情况,比如全局安装)
140
+ // 检查是否有 package.json(确认是一个 npm 项目)
141
+ const packageJsonPath = path.join(projectRoot, 'package.json');
142
+ if (!fs.existsSync(packageJsonPath)) {
143
+ debug('项目根目录没有 package.json,跳过');
141
144
  return;
142
145
  }
143
146
 
144
- log('\n📦 prd-workflow-cli: 检测到 PRD 项目,正在更新配置文件...');
147
+ log('\n📦 prd-workflow-cli: 正在配置 AI 工作流文件...');
145
148
 
146
- // 定义需要更新的文件/目录
147
- const updateItems = [
149
+ // 定义需要复制的文件/目录
150
+ const copyItems = [
148
151
  {
149
152
  name: 'Workflows',
150
153
  source: '.agent/workflows',
@@ -174,11 +177,12 @@ function main() {
174
177
  let updatedCount = 0;
175
178
  let newCount = 0;
176
179
 
177
- for (const item of updateItems) {
180
+ for (const item of copyItems) {
178
181
  const sourcePath = path.join(packagePath, item.source);
179
182
  const targetPath = path.join(projectRoot, item.target);
180
183
 
181
184
  if (!fs.existsSync(sourcePath)) {
185
+ debug(`源文件不存在: ${sourcePath}`);
182
186
  continue;
183
187
  }
184
188
 
@@ -238,16 +242,20 @@ function main() {
238
242
  if (updatedCount > 0) {
239
243
  log(` 🔄 更新 ${updatedCount} 个文件`);
240
244
  }
241
- 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');
242
251
  } else {
243
252
  log(' ✅ 所有配置文件已是最新版本\n');
244
253
  }
245
254
 
246
255
  } catch (error) {
247
256
  // postinstall 失败不应该阻止安装
248
- // 只输出警告,不抛出错误
249
- logError(`\n⚠️ prd-workflow-cli: 自动更新配置文件失败: ${error.message}`);
250
- logError(' 您可以稍后手动运行 "prd upgrade" 来更新配置文件\n');
257
+ logError(`\n⚠️ prd-workflow-cli: 配置文件复制失败: ${error.message}`);
258
+ logError(' 您可以手动运行 "npx prd upgrade" 来完成配置\n');
251
259
  }
252
260
  }
253
261