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