Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

fix-version-bump.ts

156 lines4.3 KB

1import { readFileSync, writeFileSync } from 'node:fs'
2import { resolve } from 'node:path'
3import { glob } from 'tinyglobby'
4​
5const WRONG_VERSION = '1.0.0'
6​
7interface PackageToFix {
8 name: string
9 packageJsonPath: string
10 changelogPath: string
11 detectedVersion: string | null
13​
14function parseArgs(): { version: string | null } {
15 const args = process.argv.slice(2)
16 const versionIndex = args.findIndex(
17 (arg) => arg === '--version' || arg === '-v',
18 )
19​
20 if (versionIndex !== -1 && args[versionIndex + 1]) {
21 return { version: args[versionIndex + 1] }
22 }
23​
24 return { version: null }
26​
27function detectVersionFromChangelog(changelogPath: string): string | null {
28 try {
29 const content = readFileSync(changelogPath, 'utf-8')
30​
31 // Look for "Updated dependencies" section and extract version numbers
32 // Pattern: - @tanstack/package-name@X.Y.Z
33 const dependencyPattern = /-\s+@tanstack\/[\w-]+@(\d+\.\d+\.\d+)/g
34 const matches = [...content.matchAll(dependencyPattern)]
35​
36 if (matches.length > 0) {
37 // Get the first dependency version (they should all be the same in a changeset bump)
38 const version = matches[0][1]
39 // Make sure it's not also 1.0.0
40 if (version !== WRONG_VERSION) {
41 return version
42 }
43 }
44​
45 return null
46 } catch {
47 return null
48 }
50​
51function fixPackageJson(path: string, newVersion: string): void {
52 const content = readFileSync(path, 'utf-8')
53 const updated = content.replace(
54 /"version":\s*"1\.0\.0"/,
55 `"version": "${newVersion}"`,
56 )
57 writeFileSync(path, updated)
59​
60function fixChangelog(path: string, newVersion: string): void {
61 const content = readFileSync(path, 'utf-8')
62 // Replace the first occurrence of "## 1.0.0" with the correct version
63 const updated = content.replace(/^## 1\.0\.0$/m, `## ${newVersion}`)
64 writeFileSync(path, updated)
66​
67async function main() {
68 const { version: cliVersion } = parseArgs()
69​
70 console.log('🔍 Scanning for packages with version 1.0.0...\n')
71​
72 // Find all package.json files in packages/typescript
73 const packageJsonFiles = await glob('packages/typescript/*/package.json', {
74 ignore: ['**/node_modules/**'],
75 })
76​
77 const packagesToFix: PackageToFix[] = []
78​
79 for (const packageJsonPath of packageJsonFiles) {
80 try {
81 const content = readFileSync(packageJsonPath, 'utf-8')
82 const pkg = JSON.parse(content)
83​
84 if (pkg.version === WRONG_VERSION) {
85 const changelogPath = packageJsonPath.replace(
86 'package.json',
87 'CHANGELOG.md',
88 )
89 const detectedVersion = detectVersionFromChangelog(changelogPath)
90​
91 packagesToFix.push({
92 name: pkg.name,
93 packageJsonPath,
94 changelogPath,
95 detectedVersion,
96 })
97 }
98 } catch {
99 // Skip files that can't be parsed
100 }
101 }
102​
103 if (packagesToFix.length === 0) {
104 console.log('✅ No packages found with version 1.0.0. Nothing to fix!')
105 process.exit(0)
106 }
107​
108 console.log(`Found ${packagesToFix.length} package(s) with version 1.0.0:\n`)
109​
110 const errors: string[] = []
111​
112 for (const pkg of packagesToFix) {
113 const targetVersion = cliVersion || pkg.detectedVersion
114​
115 if (!targetVersion) {
116 errors.push(pkg.name)
117 console.log(` ❌ ${pkg.name} - could not auto-detect version`)
118 } else {
119 console.log(` 📦 ${pkg.name} → ${targetVersion}`)
120 }
121 }
122​
123 if (errors.length > 0) {
124 console.log('\n❌ Could not auto-detect version for some packages.')
125 console.log(' Please run with an explicit version:\n')
126 console.log(' node scripts/fix-version-bump.ts --version X.Y.Z\n')
127 process.exit(1)
128 }
129​
130 console.log('\n🔧 Fixing versions...\n')
131​
132 for (const pkg of packagesToFix) {
133 const targetVersion = cliVersion || pkg.detectedVersion!
134​
135 // Fix package.json
136 fixPackageJson(pkg.packageJsonPath, targetVersion)
137 console.log(` ✓ ${pkg.packageJsonPath}`)
138​
139 // Fix CHANGELOG.md
140 try {
141 fixChangelog(pkg.changelogPath, targetVersion)
142 console.log(` ✓ ${pkg.changelogPath}`)
143 } catch {
144 console.log(` ⚠ ${pkg.changelogPath} (not found or could not update)`)
145 }
146 }
147​
148 console.log('\n✅ Done! Version bump fixed.')
149 console.log('\nNext steps:')
150 console.log(' 1. Review the changes: git diff')
151 console.log(
152 ' 2. Commit: git add -A && git commit -m "fix: correct version bump to X.Y.Z"',
153 )
155​
156main().catch(console.error)