|
| 1 | +import { execSync } from 'child_process'; |
| 2 | +import * as path from 'path'; |
| 3 | +import * as fs from 'fs'; |
| 4 | + |
| 5 | +export interface TypeScriptResolutionResult { |
| 6 | + ts: any | null; |
| 7 | + resolutionMethod: 'project' | 'global' | 'temporary' | null; |
| 8 | + errors: string[]; |
| 9 | +} |
| 10 | + |
| 11 | +/** |
| 12 | + * Resolves TypeScript with a 3-level fallback strategy: |
| 13 | + * 1. Project search - look in the user's project node_modules |
| 14 | + * 2. Global search - look in global node_modules |
| 15 | + * 3. Temporary install - use npx to temporarily install typescript |
| 16 | + */ |
| 17 | +export function resolveTypeScript(): TypeScriptResolutionResult { |
| 18 | + const errors: string[] = []; |
| 19 | + |
| 20 | + try { |
| 21 | + const projectTs = tryResolveFromProject(); |
| 22 | + if (projectTs) { |
| 23 | + return { |
| 24 | + ts: projectTs, |
| 25 | + resolutionMethod: 'project', |
| 26 | + errors: [] |
| 27 | + }; |
| 28 | + } |
| 29 | + } catch (err) { |
| 30 | + errors.push(`Project resolution failed: ${err?.message || err}`); |
| 31 | + } |
| 32 | + |
| 33 | + try { |
| 34 | + const globalTs = tryResolveFromGlobal(); |
| 35 | + if (globalTs) { |
| 36 | + return { |
| 37 | + ts: globalTs, |
| 38 | + resolutionMethod: 'global', |
| 39 | + errors: [] |
| 40 | + }; |
| 41 | + } |
| 42 | + } catch (err) { |
| 43 | + errors.push(`Global resolution failed: ${err?.message || err}`); |
| 44 | + } |
| 45 | + |
| 46 | + try { |
| 47 | + const tempTs = tryResolveViaTemporaryInstall(); |
| 48 | + if (tempTs) { |
| 49 | + return { |
| 50 | + ts: tempTs, |
| 51 | + resolutionMethod: 'temporary', |
| 52 | + errors: [] |
| 53 | + }; |
| 54 | + } |
| 55 | + } catch (err) { |
| 56 | + errors.push(`Temporary install failed: ${err?.message || err}`); |
| 57 | + } |
| 58 | + |
| 59 | + return { |
| 60 | + ts: null, |
| 61 | + resolutionMethod: null, |
| 62 | + errors |
| 63 | + }; |
| 64 | +} |
| 65 | + |
| 66 | +function tryResolveFromProject(): any { |
| 67 | + const searchPaths = [ |
| 68 | + process.cwd(), |
| 69 | + path.dirname(require.main?.filename || ''), |
| 70 | + ]; |
| 71 | + |
| 72 | + for (const searchPath of searchPaths) { |
| 73 | + try { |
| 74 | + const tsPath = require.resolve('typescript', { paths: [searchPath] }); |
| 75 | + // tslint:disable-next-line:no-var-requires |
| 76 | + return require(tsPath); |
| 77 | + } catch { |
| 78 | + // Continue to next path |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + return null; |
| 83 | +} |
| 84 | + |
| 85 | +function tryResolveFromGlobal(): any { |
| 86 | + try { |
| 87 | + const globalPath = execSync('npm root -g', { encoding: 'utf8' }).trim(); |
| 88 | + const typescriptPath = path.join(globalPath, 'typescript'); |
| 89 | + |
| 90 | + if (fs.existsSync(typescriptPath)) { |
| 91 | + // tslint:disable-next-line:no-var-requires |
| 92 | + return require(typescriptPath); |
| 93 | + } |
| 94 | + } catch { |
| 95 | + // Fall through |
| 96 | + } |
| 97 | + |
| 98 | + return null; |
| 99 | +} |
| 100 | + |
| 101 | +function tryResolveViaTemporaryInstall(): any { |
| 102 | + try { |
| 103 | + const tmpDir = path.join(require('os').tmpdir(), 'devextreme-schematics-ts-' + Date.now()); |
| 104 | + fs.mkdirSync(tmpDir, { recursive: true }); |
| 105 | + |
| 106 | + const packageJson = { |
| 107 | + name: 'temp-typescript-resolver', |
| 108 | + version: '1.0.0', |
| 109 | + dependencies: { |
| 110 | + typescript: 'latest' |
| 111 | + } |
| 112 | + }; |
| 113 | + fs.writeFileSync( |
| 114 | + path.join(tmpDir, 'package.json'), |
| 115 | + JSON.stringify(packageJson, null, 2) |
| 116 | + ); |
| 117 | + |
| 118 | + execSync('npm install --silent --no-progress', { |
| 119 | + cwd: tmpDir, |
| 120 | + stdio: 'ignore', |
| 121 | + timeout: 30000 |
| 122 | + }); |
| 123 | + |
| 124 | + const typescriptPath = path.join(tmpDir, 'node_modules', 'typescript'); |
| 125 | + if (fs.existsSync(typescriptPath)) { |
| 126 | + // tslint:disable-next-line:no-var-requires |
| 127 | + const ts = require(typescriptPath); |
| 128 | + |
| 129 | + setTimeout(() => { |
| 130 | + try { |
| 131 | + fs.rmSync(tmpDir, { recursive: true, force: true }); |
| 132 | + } catch { |
| 133 | + // Ignore cleanup errors |
| 134 | + } |
| 135 | + }, 5000); |
| 136 | + |
| 137 | + return ts; |
| 138 | + } |
| 139 | + } catch { |
| 140 | + // Fall through |
| 141 | + } |
| 142 | + |
| 143 | + return null; |
| 144 | +} |
0 commit comments