@@ -8,9 +8,18 @@ import { getUserPkgManager } from "./utils/get-package-manager";
88 *
99 * For compiled binaries: The binary runs from @better-t-stack/cli-{platform}-{arch},
1010 * but templates are shipped with the main create-better-t-stack package.
11- * We need to find that package in node_modules .
11+ * We need to find that package.
1212 *
1313 * For dev/npm distribution: Uses the current source directory.
14+ *
15+ * Package structure when installed via npm/bunx:
16+ * <cache>/node_modules/create-better-t-stack/
17+ * ├── templates/ <-- templates are here
18+ * ├── bin/ <-- stub is here
19+ * └── node_modules/
20+ * └── @better-t-stack/
21+ * └── cli-darwin-arm64/
22+ * └── bin/ <-- compiled binary runs from here
1423 */
1524function findPackageRoot ( ) : string {
1625 // Get the directory of the current module
@@ -25,32 +34,24 @@ function findPackageRoot(): string {
2534 return devRoot ;
2635 }
2736
28- // For compiled binaries, the binary is in @better-t-stack/cli-{platform}-{arch}/bin/
29- // We need to find create-better-t-stack package which contains templates
30- // Walk up from the binary location to find node_modules
37+ // For compiled binaries, walk up the directory tree looking for templates
38+ // The binary is typically at:
39+ // .../create-better-t-stack/node_modules/@better-t-stack/cli-{os}-{arch}/bin/
40+ // And we need to find:
41+ // .../create-better-t-stack/templates/
3142 let current = __dirname ;
3243 while ( current !== path . dirname ( current ) ) {
33- // Check if we're in node_modules and can find the main package
34- const nodeModulesPath = path . join ( current , "node_modules" ) ;
35- if ( fs . existsSync ( nodeModulesPath ) ) {
36- const mainPackagePath = path . join ( nodeModulesPath , "create-better-t-stack" ) ;
37- const mainTemplatesPath = path . join ( mainPackagePath , "templates" ) ;
38- if ( fs . existsSync ( mainTemplatesPath ) ) {
39- return mainPackagePath ;
40- }
44+ // Check if this directory has templates (we found create-better-t-stack root)
45+ const templatesPath = path . join ( current , "templates" ) ;
46+ if ( fs . existsSync ( templatesPath ) ) {
47+ return current ;
4148 }
4249
43- // Also check if current directory is inside node_modules
44- // e.g., /path/node_modules/@better-t-stack/cli-darwin-arm64/bin
45- const parts = current . split ( path . sep ) ;
46- const nodeModulesIndex = parts . lastIndexOf ( "node_modules" ) ;
47- if ( nodeModulesIndex !== - 1 ) {
48- const nodeModulesDir = parts . slice ( 0 , nodeModulesIndex + 1 ) . join ( path . sep ) ;
49- const mainPackagePath = path . join ( nodeModulesDir , "create-better-t-stack" ) ;
50- const mainTemplatesPath = path . join ( mainPackagePath , "templates" ) ;
51- if ( fs . existsSync ( mainTemplatesPath ) ) {
52- return mainPackagePath ;
53- }
50+ // Check for create-better-t-stack in node_modules at this level
51+ const nodeModulesPath = path . join ( current , "node_modules" , "create-better-t-stack" ) ;
52+ const nodeModulesTemplatesPath = path . join ( nodeModulesPath , "templates" ) ;
53+ if ( fs . existsSync ( nodeModulesTemplatesPath ) ) {
54+ return nodeModulesPath ;
5455 }
5556
5657 current = path . dirname ( current ) ;
0 commit comments