From 6d2b6810c2d9361ff75446f664f79a47f1099882 Mon Sep 17 00:00:00 2001 From: Brian Madison Date: Wed, 29 Oct 2025 09:31:38 -0500 Subject: [PATCH] fix: preserve user's cwd when running via npx --- tools/bmad-npx-wrapper.js | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/tools/bmad-npx-wrapper.js b/tools/bmad-npx-wrapper.js index 8a5ad09d..bc63a412 100755 --- a/tools/bmad-npx-wrapper.js +++ b/tools/bmad-npx-wrapper.js @@ -5,5 +5,34 @@ * This file ensures proper execution when run via npx from GitHub or npm registry */ -// Simply delegate to the CLI tool -require('./cli/bmad-cli.js'); +const { execSync } = require('node:child_process'); +const path = require('node:path'); +const fs = require('node:fs'); + +// Check if we're running in an npx temporary directory +const isNpxExecution = __dirname.includes('_npx') || __dirname.includes('.npm'); + +if (isNpxExecution) { + // Running via npx - spawn child process to preserve user's working directory + const args = process.argv.slice(2); + const bmadCliPath = path.join(__dirname, 'cli', 'bmad-cli.js'); + + if (!fs.existsSync(bmadCliPath)) { + console.error('Error: Could not find bmad-cli.js at', bmadCliPath); + console.error('Current directory:', __dirname); + process.exit(1); + } + + try { + // Execute CLI from user's working directory (process.cwd()), not npm cache + execSync(`node "${bmadCliPath}" ${args.join(' ')}`, { + stdio: 'inherit', + cwd: process.cwd(), // This preserves the user's working directory + }); + } catch (error) { + process.exit(error.status || 1); + } +} else { + // Local execution - use require + require('./cli/bmad-cli.js'); +}