MCPcopy
hub / github.com/arktypeio/arktype / forTypeScriptVersions

Function forTypeScriptVersions

ark/attest/tsVersioning.ts:25–74  ·  view source on GitHub ↗
(
	versions: TsVersionData[],
	fn: (version: TsVersionData) => void
)

Source from the content-addressed store, hash-verified

23 * fn should spawn a new process so the new symlinked version can be loaded.
24 */
25export 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
76export type TsVersionData = {
77 alias: string

Callers 1

setupFunction · 0.90

Calls 5

assertPackageRootFunction · 0.90
joinFunction · 0.85
mapMethod · 0.65
fnFunction · 0.50
errorMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…