forked from redwoodjs/redwood
-
Notifications
You must be signed in to change notification settings - Fork 0
/
downgradeToReact18.mts
107 lines (83 loc) · 3.23 KB
/
downgradeToReact18.mts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Run this file with `yarn tsx downgradeToReact18`
import fs from 'node:fs/promises'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import type { PackageJson } from 'type-fest'
// tsx unfortunately doesn't support import.meta.dirname yet
// See https://github.com/evanw/esbuild/issues/1492#issuecomment-2061825201
const __dirname = path.dirname(fileURLToPath(import.meta.url))
async function findPackageJsonFiles(directory: string) {
const files: string[] = []
const entries = await fs.readdir(directory, { withFileTypes: true })
for (const entry of entries) {
const fullPath = path.join(directory, entry.name)
if (entry.isDirectory()) {
const nestedFiles = await findPackageJsonFiles(fullPath)
files.push(...nestedFiles)
} else if (entry.isFile() && entry.name === 'package.json') {
files.push(fullPath)
}
}
return files
}
async function parsePackageJsonFiles(packageJsonFilePaths: string[]) {
const packageJsonMap: Record<string, PackageJson> = {}
for (const packageJsonFile of packageJsonFilePaths) {
const packageJson = JSON.parse(
await fs.readFile(packageJsonFile, { encoding: 'utf-8' }),
)
packageJsonMap[packageJsonFile] = packageJson
}
return packageJsonMap
}
async function downgradeReactVersion(packageJsonArray: PackageJson[]) {
const targetReactVersion = "18.3.1"
for (const packageJson of packageJsonArray) {
if (packageJson.dependencies?.react?.startsWith('19.')) {
packageJson.dependencies.react = targetReactVersion
}
if (packageJson.devDependencies?.react?.startsWith('19.')) {
packageJson.devDependencies.react = targetReactVersion
}
if (packageJson.peerDependencies?.react?.startsWith('19.')) {
packageJson.peerDependencies.react = targetReactVersion
}
if (packageJson.dependencies?.['react-dom']?.startsWith('19.')) {
packageJson.dependencies['react-dom'] = targetReactVersion
}
if (packageJson.devDependencies?.['react-dom']?.startsWith('19.')) {
packageJson.devDependencies['react-dom'] = targetReactVersion
}
if (packageJson.peerDependencies?.['react-dom']?.startsWith('19.')) {
packageJson.peerDependencies['react-dom'] = targetReactVersion
}
}
}
async function removeReactIsResolution(packageJsonArray: PackageJson[]) {
for (const packageJson of packageJsonArray) {
if (packageJson.resolutions?.['react-is']) {
delete packageJson.resolutions['react-is']
}
}
}
async function writePackageJsonFiles(
packageJsonMap: Record<string, PackageJson>,
) {
for (const [packageJsonFile, packageJson] of Object.entries(packageJsonMap)) {
await fs.writeFile(
packageJsonFile,
JSON.stringify(packageJson, null, 2) + '\n',
)
}
}
const fixturesPath = path.join(__dirname, '../__fixtures__/')
const packagesPath = path.join(__dirname, '../packages/')
const packageJsonFilePaths = [
...(await findPackageJsonFiles(fixturesPath)),
...(await findPackageJsonFiles(packagesPath)),
]
const packageJsonMap = await parsePackageJsonFiles(packageJsonFilePaths)
const packageJsonArray = Object.values(packageJsonMap)
await downgradeReactVersion(packageJsonArray)
await removeReactIsResolution(packageJsonArray)
await writePackageJsonFiles(packageJsonMap)