-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathsync-version.js
More file actions
144 lines (120 loc) · 4.52 KB
/
Copy pathsync-version.js
File metadata and controls
144 lines (120 loc) · 4.52 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env node
/**
* Sync version from root package.json to all packages
*
* Usage:
* node scripts/sync-version.js # Sync current version from root
* node scripts/sync-version.js 0.1.0 # Set root version, then sync all packages
*/
import chalk from 'chalk'
import { existsSync, readFileSync, readdirSync, writeFileSync } from 'fs'
import { dirname, join } from 'path'
import { exit } from 'process'
import { fileURLToPath } from 'url'
const __dirname = dirname(fileURLToPath(import.meta.url))
const rootDir = join(__dirname, '..')
const versionArg = process.argv[2]
// Read root package.json
const rootPkgPath = join(rootDir, 'package.json')
const rootPkg = JSON.parse(readFileSync(rootPkgPath, 'utf-8'))
const oldVersion = rootPkg.version
const newVersion = versionArg ?? rootPkg.version
if (!newVersion) {
console.log(chalk.yellow('⚠️ No version found in root package.json.\n'))
exit(1)
}
console.log(chalk.cyan.bold('\n📦 Syncing version\n'))
// Update root package.json if new version specified
if (versionArg) {
rootPkg.version = newVersion
writeFileSync(rootPkgPath, JSON.stringify(rootPkg, null, ' ') + '\n')
console.log(
chalk.green('✓') +
` ${chalk.bold('root')}: ${chalk.dim(oldVersion)} → ${chalk.yellow(newVersion)}`
)
} else {
console.log(chalk.dim(' root:') + ` ${chalk.yellow(newVersion)} ${chalk.dim('(source)')}`)
}
// Sync to all packages
const packagesDir = join(rootDir, 'packages')
const packages = readdirSync(packagesDir, { withFileTypes: true })
.filter((d) => d.isDirectory())
.map((d) => d.name)
let hasChanges = !!versionArg
/**
* Check if a dependency name is a page-agent internal package
*/
function isInternalPackage(name) {
return name === 'page-agent' || name.startsWith('@page-agent/')
}
/**
* Update internal package versions in dependencies object
* @returns {boolean} Whether any changes were made
*/
function updateInternalDeps(deps, newVersion) {
if (!deps) return false
let changed = false
for (const [name, version] of Object.entries(deps)) {
if (isInternalPackage(name) && version !== newVersion) {
deps[name] = newVersion
changed = true
}
}
return changed
}
for (const pkg of packages) {
const pkgPath = join(packagesDir, pkg, 'package.json')
if (!existsSync(pkgPath)) continue
const pkgJson = JSON.parse(readFileSync(pkgPath, 'utf-8'))
let pkgChanged = false
// Update package version
if (pkgJson.version !== newVersion) {
pkgJson.version = newVersion
pkgChanged = true
}
// Update internal dependencies (dependencies only, devDeps keep "*")
if (updateInternalDeps(pkgJson.dependencies, newVersion)) {
pkgChanged = true
}
if (!pkgChanged) {
console.log(chalk.dim(` ${pkgJson.name}: ${newVersion} (unchanged)`))
continue
}
writeFileSync(pkgPath, JSON.stringify(pkgJson, null, ' ') + '\n')
console.log(
chalk.green('✓') +
` ${chalk.bold(pkgJson.name)}: ${chalk.dim(oldVersion)} → ${chalk.yellow(newVersion)}`
)
hasChanges = true
}
// Update CDN URLs in documentation and source files
const CDN_DEMO_URL_OLD = `https://cdn.jsdelivr.net/npm/page-agent@${oldVersion}/dist/iife/page-agent.demo.js`
const CDN_DEMO_URL_NEW = `https://cdn.jsdelivr.net/npm/page-agent@${newVersion}/dist/iife/page-agent.demo.js`
const CDN_DEMO_CN_URL_OLD = `https://registry.npmmirror.com/page-agent/${oldVersion}/files/dist/iife/page-agent.demo.js`
const CDN_DEMO_CN_URL_NEW = `https://registry.npmmirror.com/page-agent/${newVersion}/files/dist/iife/page-agent.demo.js`
const filesToUpdateCdn = ['README.md', 'docs/README-zh.md', 'packages/website/src/constants.ts']
for (const relPath of filesToUpdateCdn) {
const filePath = join(rootDir, relPath)
if (!existsSync(filePath)) continue
let content = readFileSync(filePath, 'utf-8')
const original = content
content = content.replaceAll(CDN_DEMO_URL_OLD, CDN_DEMO_URL_NEW)
content = content.replaceAll(CDN_DEMO_CN_URL_OLD, CDN_DEMO_CN_URL_NEW)
if (content !== original) {
writeFileSync(filePath, content)
console.log(chalk.green('✓') + ` ${chalk.bold(relPath)}: CDN URLs updated`)
hasChanges = true
}
}
console.log(chalk.green.bold(`\n✓ Version synced: ${newVersion}\n`))
// Show git commands hint
if (hasChanges) {
const tagName = `v${newVersion}`
console.log(chalk.cyan.bold('📋 Next steps:\n'))
console.log(chalk.blueBright(`npm i`))
console.log(
chalk.blueBright(`git add . && git commit -m "chore(version): bump version to ${newVersion}"`)
)
console.log(chalk.blueBright(`git tag -a ${tagName} -m "${tagName}"`))
console.log(chalk.blueBright(`git push && git push origin ${tagName}\n`))
}