2015-12-02 05:25:09 +01:00
|
|
|
#!/usr/bin/env node
|
2016-12-31 06:25:26 +01:00
|
|
|
|
2015-12-02 05:25:09 +01:00
|
|
|
/*!
|
|
|
|
* Script to update version number references in the project.
|
2024-02-05 21:39:38 +01:00
|
|
|
* Copyright 2017-2024 The Bootstrap Authors
|
2020-06-16 20:41:47 +02:00
|
|
|
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
|
2015-12-02 05:25:09 +01:00
|
|
|
*/
|
2016-12-31 06:25:26 +01:00
|
|
|
|
2023-08-21 08:03:09 +02:00
|
|
|
import { execFile } from 'node:child_process'
|
2023-08-06 06:37:24 +02:00
|
|
|
import fs from 'node:fs/promises'
|
2023-08-21 08:03:09 +02:00
|
|
|
import process from 'node:process'
|
2021-03-11 10:37:52 +01:00
|
|
|
|
|
|
|
const VERBOSE = process.argv.includes('--verbose')
|
|
|
|
const DRY_RUN = process.argv.includes('--dry') || process.argv.includes('--dry-run')
|
|
|
|
|
2023-08-21 08:03:09 +02:00
|
|
|
// These are the files we only care about replacing the version
|
|
|
|
const FILES = [
|
|
|
|
'README.md',
|
|
|
|
'hugo.yml',
|
|
|
|
'js/src/base-component.js',
|
|
|
|
'package.js',
|
|
|
|
'scss/mixins/_banner.scss',
|
|
|
|
'site/data/docs-versions.yml'
|
2021-03-11 10:37:52 +01:00
|
|
|
]
|
2015-12-02 05:25:09 +01:00
|
|
|
|
|
|
|
// Blame TC39... https://github.com/benjamingr/RegExp.escape/issues/37
|
2017-12-16 13:00:38 +01:00
|
|
|
function regExpQuote(string) {
|
2019-12-24 17:23:17 +01:00
|
|
|
return string.replace(/[$()*+-.?[\\\]^{|}]/g, '\\$&')
|
2017-12-16 13:00:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function regExpQuoteReplacement(string) {
|
2019-12-24 17:23:17 +01:00
|
|
|
return string.replace(/\$/g, '$$')
|
2017-12-16 13:00:38 +01:00
|
|
|
}
|
2015-12-02 05:25:09 +01:00
|
|
|
|
2021-03-11 10:37:52 +01:00
|
|
|
async function replaceRecursively(file, oldVersion, newVersion) {
|
|
|
|
const originalString = await fs.readFile(file, 'utf8')
|
2023-04-12 21:56:22 +02:00
|
|
|
const newString = originalString
|
|
|
|
.replace(
|
|
|
|
new RegExp(regExpQuote(oldVersion), 'g'),
|
|
|
|
regExpQuoteReplacement(newVersion)
|
|
|
|
)
|
|
|
|
// Also replace the version used by the rubygem,
|
|
|
|
// which is using periods (`.`) instead of hyphens (`-`)
|
|
|
|
.replace(
|
|
|
|
new RegExp(regExpQuote(oldVersion.replace(/-/g, '.')), 'g'),
|
|
|
|
regExpQuoteReplacement(newVersion.replace(/-/g, '.'))
|
|
|
|
)
|
2015-12-02 05:25:09 +01:00
|
|
|
|
2021-03-11 10:37:52 +01:00
|
|
|
// No need to move any further if the strings are identical
|
|
|
|
if (originalString === newString) {
|
2016-12-31 06:25:26 +01:00
|
|
|
return
|
2015-12-02 05:25:09 +01:00
|
|
|
}
|
2019-02-26 12:20:34 +01:00
|
|
|
|
2021-03-11 10:37:52 +01:00
|
|
|
if (VERBOSE) {
|
2023-08-21 08:03:09 +02:00
|
|
|
console.log(`Found ${oldVersion} in ${file}`)
|
2021-03-11 10:37:52 +01:00
|
|
|
}
|
2015-12-02 05:25:09 +01:00
|
|
|
|
2021-03-11 10:37:52 +01:00
|
|
|
if (DRY_RUN) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
await fs.writeFile(file, newString, 'utf8')
|
2015-12-02 05:25:09 +01:00
|
|
|
}
|
|
|
|
|
2023-08-21 08:03:09 +02:00
|
|
|
function bumpNpmVersion(newVersion) {
|
|
|
|
if (DRY_RUN) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
execFile('npm', ['version', newVersion, '--no-git-tag'], { shell: true }, error => {
|
|
|
|
if (error) {
|
|
|
|
console.error(error)
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-04-12 21:56:22 +02:00
|
|
|
function showUsage(args) {
|
|
|
|
console.error('USAGE: change-version old_version new_version [--verbose] [--dry[-run]]')
|
|
|
|
console.error('Got arguments:', args)
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
|
2021-03-11 10:37:52 +01:00
|
|
|
async function main(args) {
|
2021-11-24 09:49:35 +01:00
|
|
|
let [oldVersion, newVersion] = args
|
2021-03-11 10:37:52 +01:00
|
|
|
|
|
|
|
if (!oldVersion || !newVersion) {
|
2023-04-12 21:56:22 +02:00
|
|
|
showUsage(args)
|
2015-12-02 05:25:09 +01:00
|
|
|
}
|
2019-02-26 12:20:34 +01:00
|
|
|
|
2023-04-12 21:56:22 +02:00
|
|
|
// Strip any leading `v` from arguments because
|
|
|
|
// otherwise we will end up with duplicate `v`s
|
|
|
|
[oldVersion, newVersion] = [oldVersion, newVersion].map(arg => {
|
|
|
|
return arg.startsWith('v') ? arg.slice(1) : arg
|
|
|
|
})
|
|
|
|
|
|
|
|
if (oldVersion === newVersion) {
|
|
|
|
showUsage(args)
|
|
|
|
}
|
2021-03-11 10:37:52 +01:00
|
|
|
|
2023-08-21 08:03:09 +02:00
|
|
|
bumpNpmVersion(newVersion)
|
2021-03-11 10:37:52 +01:00
|
|
|
|
2023-08-21 08:03:09 +02:00
|
|
|
try {
|
2023-04-12 21:56:22 +02:00
|
|
|
await Promise.all(
|
2023-08-21 08:03:09 +02:00
|
|
|
FILES.map(file => replaceRecursively(file, oldVersion, newVersion))
|
2023-04-12 21:56:22 +02:00
|
|
|
)
|
2021-03-11 10:37:52 +01:00
|
|
|
} catch (error) {
|
|
|
|
console.error(error)
|
|
|
|
process.exit(1)
|
|
|
|
}
|
2015-12-17 06:15:33 +01:00
|
|
|
}
|
2015-12-02 05:25:09 +01:00
|
|
|
|
2016-12-31 06:25:26 +01:00
|
|
|
main(process.argv.slice(2))
|