Skip to content

Commit 6d2b681

Browse files
Brian MadisonBrian Madison
authored andcommitted
fix: preserve user's cwd when running via npx
1 parent 06dab16 commit 6d2b681

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

tools/bmad-npx-wrapper.js

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,34 @@
55
* This file ensures proper execution when run via npx from GitHub or npm registry
66
*/
77

8-
// Simply delegate to the CLI tool
9-
require('./cli/bmad-cli.js');
8+
const { execSync } = require('node:child_process');
9+
const path = require('node:path');
10+
const fs = require('node:fs');
11+
12+
// Check if we're running in an npx temporary directory
13+
const isNpxExecution = __dirname.includes('_npx') || __dirname.includes('.npm');
14+
15+
if (isNpxExecution) {
16+
// Running via npx - spawn child process to preserve user's working directory
17+
const args = process.argv.slice(2);
18+
const bmadCliPath = path.join(__dirname, 'cli', 'bmad-cli.js');
19+
20+
if (!fs.existsSync(bmadCliPath)) {
21+
console.error('Error: Could not find bmad-cli.js at', bmadCliPath);
22+
console.error('Current directory:', __dirname);
23+
process.exit(1);
24+
}
25+
26+
try {
27+
// Execute CLI from user's working directory (process.cwd()), not npm cache
28+
execSync(`node "${bmadCliPath}" ${args.join(' ')}`, {
29+
stdio: 'inherit',
30+
cwd: process.cwd(), // This preserves the user's working directory
31+
});
32+
} catch (error) {
33+
process.exit(error.status || 1);
34+
}
35+
} else {
36+
// Local execution - use require
37+
require('./cli/bmad-cli.js');
38+
}

0 commit comments

Comments
 (0)