@@ -25,6 +25,16 @@ type IgnoreInstance = ReturnType<typeof ignore>;
2525
2626const MAX_FILE_SIZE = 10 * 1024 * 1024 ; // 10MB in bytes
2727
28+ function getActualWorkingDirectory ( ) {
29+ // INIT_CWD is set by npm/npx to the directory from which the command was invoked
30+ // This is more reliable than process.cwd() when running via npx
31+ if ( process . env . INIT_CWD ) {
32+ return process . env . INIT_CWD ;
33+ }
34+
35+ return process . cwd ( ) ;
36+ }
37+
2838// Simple debounce function to avoid multiple rebuilds when many files change at once
2939function debounce < F extends ( ...args : any [ ] ) => any > (
3040 func : F ,
@@ -193,7 +203,7 @@ async function watchFiles(
193203 // Ignore the output file
194204 const outputAbsPath = path.isAbsolute(outputFile)
195205 ? outputFile
196- : path.join(process.cwd (), outputFile);
206+ : path.join(getActualWorkingDirectory (), outputFile);
197207 if (filePath === outputAbsPath) {
198208 return true;
199209 }
@@ -453,7 +463,7 @@ async function aggregateFiles(
453463
454464 const outputAbsPath = path.isAbsolute(outputFile)
455465 ? outputFile
456- : path.join(process.cwd (), outputFile);
466+ : path.join(getActualWorkingDirectory (), outputFile);
457467
458468 if (
459469 fullPath === outputAbsPath ||
@@ -503,7 +513,7 @@ async function aggregateFiles(
503513 }
504514 }
505515
506- await fs . mkdir ( path . dirname ( outputFile ) , { recursive : true } ) ;
516+ await fs . mkdir ( path . dirname ( path . resolve ( getActualWorkingDirectory ( ) , outputFile ) ) , { recursive : true } ) ;
507517
508518 // Write to a temporary file first to prevent partial writes during SIGINT
509519 const tempFile = `${outputFile } . temp `;
@@ -600,7 +610,7 @@ program
600610 . option (
601611 "- i , -- input < directories ...> ",
602612 "Input directories ( multiple allowed ) ",
603- [ process . cwd ( ) ]
613+ [ getActualWorkingDirectory ( ) ] // Fix: Use getActualWorkingDirectory() instead of process.cwd()
604614 )
605615 . option ( "- o , -- output < file > ", "Output file name ", "codebase . md ")
606616 . option ( "-- no - default - ignores ", "Disable default ignore patterns ")
@@ -613,9 +623,10 @@ program
613623 . option ( "-- watch ", "Watch for file changes and rebuild automatically ")
614624 . action ( async ( options ) => {
615625 const inputDirs = options . input . map ( ( dir : string ) => path . resolve ( dir ) ) ;
626+ // Fix: Use getActualWorkingDirectory() instead of process.cwd()
616627 const outputFile = path . isAbsolute ( options . output )
617628 ? options . output
618- : path . join ( process . cwd ( ) , options . output ) ;
629+ : path . join ( getActualWorkingDirectory ( ) , options . output ) ;
619630
620631 if ( options . watch ) {
621632 // Run in watch mode
@@ -641,4 +652,4 @@ program
641652 }
642653 } ) ;
643654
644- program . parse ( process . argv ) ;
655+ program . parse ( process . argv ) ;
0 commit comments