Skip to content

Commit ba44960

Browse files
authored
Improve change-version script (#38983)
Only replace the version in specific files instead of blindly all repo files
1 parent 803c5de commit ba44960

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

build/change-version.mjs

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,22 @@
66
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
77
*/
88

9+
import { execFile } from 'node:child_process'
910
import fs from 'node:fs/promises'
10-
import path from 'node:path'
11-
import { fileURLToPath } from 'node:url'
12-
import globby from 'globby'
13-
14-
const __dirname = path.dirname(fileURLToPath(import.meta.url))
11+
import process from 'node:process'
1512

1613
const VERBOSE = process.argv.includes('--verbose')
1714
const DRY_RUN = process.argv.includes('--dry') || process.argv.includes('--dry-run')
1815

19-
// These are the filetypes we only care about replacing the version
20-
const GLOB = [
21-
'**/*.{css,html,js,json,md,scss,txt,yml}'
16+
// These are the files we only care about replacing the version
17+
const FILES = [
18+
'README.md',
19+
'hugo.yml',
20+
'js/src/base-component.js',
21+
'package.js',
22+
'scss/mixins/_banner.scss',
23+
'site/data/docs-versions.yml'
2224
]
23-
const GLOBBY_OPTIONS = {
24-
cwd: path.join(__dirname, '..'),
25-
gitignore: true
26-
}
2725

2826
// Blame TC39... https://github.com/benjamingr/RegExp.escape/issues/37
2927
function regExpQuote(string) {
@@ -54,7 +52,7 @@ async function replaceRecursively(file, oldVersion, newVersion) {
5452
}
5553

5654
if (VERBOSE) {
57-
console.log(`FILE: ${file}`)
55+
console.log(`Found ${oldVersion} in ${file}`)
5856
}
5957

6058
if (DRY_RUN) {
@@ -64,6 +62,19 @@ async function replaceRecursively(file, oldVersion, newVersion) {
6462
await fs.writeFile(file, newString, 'utf8')
6563
}
6664

65+
function bumpNpmVersion(newVersion) {
66+
if (DRY_RUN) {
67+
return
68+
}
69+
70+
execFile('npm', ['version', newVersion, '--no-git-tag'], { shell: true }, error => {
71+
if (error) {
72+
console.error(error)
73+
process.exit(1)
74+
}
75+
})
76+
}
77+
6778
function showUsage(args) {
6879
console.error('USAGE: change-version old_version new_version [--verbose] [--dry[-run]]')
6980
console.error('Got arguments:', args)
@@ -87,11 +98,11 @@ async function main(args) {
8798
showUsage(args)
8899
}
89100

90-
try {
91-
const files = await globby(GLOB, GLOBBY_OPTIONS)
101+
bumpNpmVersion(newVersion)
92102

103+
try {
93104
await Promise.all(
94-
files.map(file => replaceRecursively(file, oldVersion, newVersion))
105+
FILES.map(file => replaceRecursively(file, oldVersion, newVersion))
95106
)
96107
} catch (error) {
97108
console.error(error)

0 commit comments

Comments
 (0)