Skip to content

Commit

Permalink
Improve change-version script
Browse files Browse the repository at this point in the history
Only replace the version in specific files instead of blindly all repo files
  • Loading branch information
XhmikosR committed Aug 6, 2023
1 parent 56664e0 commit d0abc77
Showing 1 changed file with 27 additions and 16 deletions.
43 changes: 27 additions & 16 deletions build/change-version.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
*/

import { execFile, spawn } from 'node:child_process'
import fs from 'node:fs/promises'
import path from 'node:path'
import process from 'node:process'
import { fileURLToPath } from 'node:url'
import globby from 'globby'

const __dirname = path.dirname(fileURLToPath(import.meta.url))

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

// These are the filetypes we only care about replacing the version
const GLOB = [
'**/*.{css,html,js,json,md,scss,txt,yml}'
// 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'
]
const GLOBBY_OPTIONS = {
cwd: path.join(__dirname, '..'),
gitignore: true
}

// Blame TC39... https://github.com/benjamingr/RegExp.escape/issues/37
function regExpQuote(string) {
Expand Down Expand Up @@ -54,7 +54,7 @@ async function replaceRecursively(file, oldVersion, newVersion) {
}

if (VERBOSE) {
console.log(`FILE: ${file}`)
console.log(`Found ${oldVersion} in ${file}`)
}

if (DRY_RUN) {
Expand All @@ -64,6 +64,19 @@ async function replaceRecursively(file, oldVersion, newVersion) {
await fs.writeFile(file, newString, 'utf8')
}

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)
}
})
}

function showUsage(args) {
console.error('USAGE: change-version old_version new_version [--verbose] [--dry[-run]]')
console.error('Got arguments:', args)
Expand All @@ -87,12 +100,10 @@ async function main(args) {
showUsage(args)
}

try {
const files = await globby(GLOB, GLOBBY_OPTIONS)
bumpNpmVersion(newVersion)

await Promise.all(
files.map(file => replaceRecursively(file, oldVersion, newVersion))
)
try {
await Promise.all(FILES.map(file => replaceRecursively(file, oldVersion, newVersion)))
} catch (error) {
console.error(error)
process.exit(1)
Expand Down

0 comments on commit d0abc77

Please sign in to comment.