| 23 | * fn should spawn a new process so the new symlinked version can be loaded. |
| 24 | */ |
| 25 | export const forTypeScriptVersions = ( |
| 26 | versions: TsVersionData[], |
| 27 | fn: (version: TsVersionData) => void |
| 28 | ): void => { |
| 29 | const passedVersions: TsVersionData[] = [] |
| 30 | const failedVersions: TsVersionData[] = [] |
| 31 | const nodeModules = join(assertPackageRoot(process.cwd()), "node_modules") |
| 32 | const tsPrimaryPath = join(nodeModules, "typescript") |
| 33 | const tsTemporaryPath = join(nodeModules, "typescript-temp") |
| 34 | |
| 35 | if (existsSync(tsTemporaryPath)) unlinkSync(tsTemporaryPath) |
| 36 | if (existsSync(tsPrimaryPath)) renameSync(tsPrimaryPath, tsTemporaryPath) |
| 37 | |
| 38 | try { |
| 39 | for (const version of versions) { |
| 40 | const targetPath = |
| 41 | version.path === tsPrimaryPath ? tsTemporaryPath : version.path |
| 42 | console.log( |
| 43 | `⛵ Switching to TypeScript version ${version.alias} (${version.version})...` |
| 44 | ) |
| 45 | try { |
| 46 | if (existsSync(tsPrimaryPath)) unlinkSync(tsPrimaryPath) |
| 47 | symlinkSync(targetPath, tsPrimaryPath, "junction") |
| 48 | fn(version) |
| 49 | passedVersions.push(version) |
| 50 | } catch (e) { |
| 51 | console.error(e) |
| 52 | failedVersions.push(version) |
| 53 | } |
| 54 | } |
| 55 | if (failedVersions.length !== 0) { |
| 56 | throw new Error( |
| 57 | `❌ The following TypeScript versions threw: ${failedVersions |
| 58 | .map(v => `${v.alias} (${v.version})`) |
| 59 | .join(", ")}` |
| 60 | ) |
| 61 | } |
| 62 | console.log( |
| 63 | `✅ Successfully ran TypeScript versions ${passedVersions |
| 64 | .map(v => `${v.alias} (${v.version})`) |
| 65 | .join(", ")}` |
| 66 | ) |
| 67 | } finally { |
| 68 | if (existsSync(tsTemporaryPath)) { |
| 69 | console.log(`⏮️ Restoring your original TypeScript version...`) |
| 70 | unlinkSync(tsPrimaryPath) |
| 71 | renameSync(tsTemporaryPath, tsPrimaryPath) |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | export type TsVersionData = { |
| 77 | alias: string |