Open History to see the last commit
1import {
2 existsSync,
3 readFileSync,
4 readdirSync,
5 statSync,
6 writeFileSync,
7} from 'node:fs'
8import { join } from 'node:path'
9import { fileURLToPath } from 'node:url'
10β
11const __dirname = fileURLToPath(new URL('.', import.meta.url))
12β
13const ROOT_PACKAGE_JSON = join(__dirname, '..', 'package.json')
14const PACKAGES_DIR = join(__dirname, '..', 'packages')
15const SUPPORTED_TS_VERSIONS = ['5.6', '5.7', '5.8', '5.9', '6.0', '7.0']
16const API_COMPAT_TS_VERSION = '6.0'
17const API_COMPAT_TS_RANGE = '^6.0.2'
18const LATEST_TS_RANGE = '^7.0.2'
19const LATEST_TS_VERSION =
20 SUPPORTED_TS_VERSIONS[SUPPORTED_TS_VERSIONS.length - 1]
21β
22/**
23 * @param {string} packagePath
24 */
25function updatePackageJson(packagePath) {
26 const PREVIOUS_LATEST_VERSION =
27 SUPPORTED_TS_VERSIONS[SUPPORTED_TS_VERSIONS.length - 2]
28β
29 if (PREVIOUS_LATEST_VERSION === undefined) {
30 throw new Error('Previous latest version not found')
31 }
32β
33 const packageJsonPath = join(packagePath, 'package.json')
34 if (!existsSync(packageJsonPath)) return
35β
36 const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf8'))
37 packageJson.scripts = packageJson.scripts || {}
38β
39 const scriptKeys = Object.keys(packageJson.scripts)
40 const updatedScripts = { ...packageJson.scripts }
41 /** @type Record<string, string> */
42 const cliArgs = {}
43β
44 // Remove old TS test scripts and store CLI arguments
45 scriptKeys.forEach((key) => {
46 if (key.startsWith('test:types:')) {
47 const script = packageJson.scripts[key]
48 const match = script.match(/(?:node \S+\/tsc\.js|tsc6|tsc)(.*)/)
49 cliArgs[key] = match ? match[1].trim() : ''
50 delete updatedScripts[key]
51 }
52 })
53β
54 // Get CLI arguments from the previous latest version
55 const previousLatestKey = `test:types:ts${PREVIOUS_LATEST_VERSION.replace('.', '')}`
56 const previousLatestArgs = cliArgs[previousLatestKey] || ''
57β
58 // Insert new TS test scripts while maintaining order
59 let insertIndex = scriptKeys.findIndex((key) => key.startsWith('test:types:'))
60 if (insertIndex === -1) insertIndex = scriptKeys.length
61β
62 /** @type Record<string, string> */
63 const newScripts = {}
64 scriptKeys.forEach((key, index) => {
65 if (index === insertIndex) {
66 SUPPORTED_TS_VERSIONS.forEach((version, i) => {
67 const scriptKey = `test:types:ts${version.replace('.', '')}`
68 if (i === SUPPORTED_TS_VERSIONS.length - 1) {
69 // Use "tsc" directly for the latest version
70 newScripts[scriptKey] = `tsc ${previousLatestArgs}`.trim()
71 } else if (version === API_COMPAT_TS_VERSION) {
72 newScripts[scriptKey] = `tsc6 ${cliArgs[scriptKey] || ''}`.trim()
73 } else {
74 const args = cliArgs[scriptKey] || ''
75 newScripts[scriptKey] =
76 `node ../../node_modules/typescript${version.replace('.', '')}/lib/tsc.js ${args}`.trim()
77 }
78 })
79 }
80 if (!key.startsWith('test:types:')) {
81 newScripts[key] = packageJson.scripts[key]
82 }
83 })
84β
85 packageJson.scripts = newScripts
86 writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2) + '\n')
87 console.log(`Updated ${packageJsonPath}`)
88}
89β
90function updateRootPackageJson() {
91 if (!existsSync(ROOT_PACKAGE_JSON)) {
92 return
93 }
94β
95 const rootPackageJson = JSON.parse(readFileSync(ROOT_PACKAGE_JSON, 'utf8'))
96 rootPackageJson.devDependencies = rootPackageJson.devDependencies || {}
97β
98 // TypeScript 7 provides tsc, while TypeScript 6 provides the legacy API.
99 rootPackageJson.devDependencies['@typescript/native'] =
100 `npm:typescript@${LATEST_TS_RANGE}`
101 rootPackageJson.devDependencies['typescript'] =
102 `npm:@typescript/typescript6@${API_COMPAT_TS_RANGE}`
103β
104 // Remove old TypeScript aliases
105 Object.keys(rootPackageJson.devDependencies).forEach((dep) => {
106 if (dep.startsWith('typescript') && dep !== 'typescript') {
107 delete rootPackageJson.devDependencies[dep]
108 }
109 })
110β
111 // Add supported TypeScript aliases
112 SUPPORTED_TS_VERSIONS.slice(0, -1)
113 .filter((version) => version !== API_COMPAT_TS_VERSION)
114 .forEach((version) => {
115 rootPackageJson.devDependencies[`typescript${version.replace('.', '')}`] =
116 `npm:typescript@${version}`
117 })
118β
119 writeFileSync(
120 ROOT_PACKAGE_JSON,
121 JSON.stringify(rootPackageJson, null, 2) + '\n',
122 )
123 console.log(`Updated ${ROOT_PACKAGE_JSON}`)
124}
125β
126function updateAllPackages() {
127 if (!existsSync(PACKAGES_DIR)) {
128 throw new Error(`Packages directory not found: ${PACKAGES_DIR}`)
129 }
130β
131 const packageDirs = readdirSync(PACKAGES_DIR)
132 .map((name) => join(PACKAGES_DIR, name))
133 .filter((dir) => statSync(dir).isDirectory())
134 packageDirs.forEach(updatePackageJson)
135 updateRootPackageJson()
136}
137β
138updateAllPackages()