-
Notifications
You must be signed in to change notification settings - Fork 31.9k
Expand file tree
/
Copy pathchange-npm-name.js
More file actions
102 lines (83 loc) · 3.11 KB
/
Copy pathchange-npm-name.js
File metadata and controls
102 lines (83 loc) · 3.11 KB
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
const fs = require('fs')
const path = require('path')
const newPackageName = process.argv[2]
if (!newPackageName) {
console.error('Usage: node change-npm-name.js <new-package-name>')
console.error('Example: node change-npm-name.js @next/rspack-core')
process.exit(1)
}
const bindingPackageName = newPackageName.replace(/core/g, 'binding')
console.log(`Core package name: ${newPackageName}`)
console.log(`Binding package name: ${bindingPackageName}`)
console.log(`GitHub Repository: ${process.env.GITHUB_REPOSITORY || 'not set'}`)
function updatePackageJson(filePath, packageName) {
try {
if (!fs.existsSync(filePath)) {
console.warn(`Package.json not found: ${filePath}`)
return
}
const packageJson = JSON.parse(fs.readFileSync(filePath, 'utf8'))
packageJson.name = packageName
// Update repository URL to match current GitHub repo for provenance validation
if (packageJson.repository && packageJson.repository.url) {
const githubRepo = process.env.GITHUB_REPOSITORY
if (githubRepo) {
packageJson.repository.url = `git+https://github.com/${githubRepo}.git`
console.log(
`📝 Updated repository URL to: ${packageJson.repository.url}`
)
} else {
console.log(
`⚠️ GITHUB_REPOSITORY not found, keeping original repository URL: ${packageJson.repository.url}`
)
}
}
fs.writeFileSync(filePath, JSON.stringify(packageJson, null, 2) + '\n')
console.log(`✅ Updated ${filePath} with name: ${packageName}`)
} catch (error) {
console.error(`❌ Error updating ${filePath}:`, error.message)
}
}
function updateBindingIndex(filePath, packageName) {
try {
if (!fs.existsSync(filePath)) {
console.warn(`Binding index.js not found: ${filePath}`)
return
}
let content = fs.readFileSync(filePath, 'utf8')
// Replace all require('@next/rspack-binding-*') with the new package name pattern
const requireRegex =
/require\(['"`]@next\/rspack-binding(-[^'"`]*)?['"`]\)/g
let updateCount = 0
content = content.replace(requireRegex, (match, suffix) => {
updateCount++
return `require('${packageName}${suffix || ''}')`
})
if (updateCount > 0) {
fs.writeFileSync(filePath, content)
console.log(
`✅ Updated ${filePath} with ${updateCount} require statements using package name: ${packageName}`
)
} else {
console.warn(
`⚠️ No require statements with @next/rspack-binding found in ${filePath}`
)
}
} catch (error) {
console.error(`❌ Error updating ${filePath}:`, error.message)
}
}
function main() {
const rootDir = __dirname
const rootPackageJsonPath = path.join(rootDir, 'package.json')
updatePackageJson(rootPackageJsonPath, newPackageName)
const bindingPackageJsonPath = path.join(
rootDir,
'crates/binding/package.json'
)
updatePackageJson(bindingPackageJsonPath, bindingPackageName)
const bindingIndexPath = path.join(rootDir, 'crates/binding/index.js')
updateBindingIndex(bindingIndexPath, bindingPackageName)
console.log('\n🎉 Package name update completed!')
}
main()