|
1 | | -import { join, resolve } from 'path'; |
| 1 | +import { join, resolve, dirname } from 'path'; |
| 2 | +import { fileURLToPath } from 'url'; |
2 | 3 | import { access, mkdir, rm } from 'fs/promises'; |
3 | 4 | import { spawn } from 'child_process'; |
4 | 5 | import chalk from 'chalk'; |
@@ -141,17 +142,30 @@ export async function traceCommand( |
141 | 142 | } |
142 | 143 |
|
143 | 144 | /** |
144 | | - * Find the tsc executable, preferring local node_modules |
| 145 | + * Find the tsc executable |
| 146 | + * Priority: 1) Project's local tsc, 2) This package's bundled tsc |
145 | 147 | */ |
146 | 148 | async function findTsc(projectPath: string): Promise<string> { |
147 | | - // Try local node_modules first |
148 | | - const localTsc = join(projectPath, 'node_modules', '.bin', 'tsc'); |
| 149 | + // Try project's local node_modules first |
| 150 | + const projectTsc = join(projectPath, 'node_modules', '.bin', 'tsc'); |
149 | 151 | try { |
150 | | - await access(localTsc); |
151 | | - return localTsc; |
| 152 | + await access(projectTsc); |
| 153 | + return projectTsc; |
152 | 154 | } catch { |
153 | | - // Fall back to global tsc |
154 | | - return 'tsc'; |
| 155 | + // Fall back to this package's bundled tsc |
| 156 | + const __filename = fileURLToPath(import.meta.url); |
| 157 | + const __dirname = dirname(__filename); |
| 158 | + // Navigate from dist/cli/commands/ to package root, then to node_modules |
| 159 | + const packageRoot = join(__dirname, '..', '..', '..'); |
| 160 | + const bundledTsc = join(packageRoot, 'node_modules', '.bin', 'tsc'); |
| 161 | + |
| 162 | + try { |
| 163 | + await access(bundledTsc); |
| 164 | + return bundledTsc; |
| 165 | + } catch { |
| 166 | + // Last resort: try global tsc |
| 167 | + return 'tsc'; |
| 168 | + } |
155 | 169 | } |
156 | 170 | } |
157 | 171 |
|
@@ -182,6 +196,16 @@ function runTsc( |
182 | 196 | }); |
183 | 197 |
|
184 | 198 | proc.on('close', (code) => { |
| 199 | + // Exit code 127 means command not found - this is a fatal error |
| 200 | + if (code === 127) { |
| 201 | + reject( |
| 202 | + new Error( |
| 203 | + 'TypeScript compiler (tsc) not found. Please install TypeScript in your project:\n npm install typescript --save-dev' |
| 204 | + ) |
| 205 | + ); |
| 206 | + return; |
| 207 | + } |
| 208 | + |
185 | 209 | // TypeScript may return non-zero exit code for type errors, |
186 | 210 | // but trace should still be generated |
187 | 211 | if (code !== 0 && verbose) { |
|
0 commit comments