Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve change-version script #38983

Merged
merged 2 commits into from
Aug 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,22 @@
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
*/

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

const __dirname = path.dirname(fileURLToPath(import.meta.url))
import process from 'node:process'

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 +52,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 +62,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)
Comment on lines +72 to +73
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
console.error(error)
process.exit(1)
console.error(error)
process.exit(1)

Suppose we could use throw error here, too?

}
})
}

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

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

try {
await Promise.all(
files.map(file => replaceRecursively(file, oldVersion, newVersion))
FILES.map(file => replaceRecursively(file, oldVersion, newVersion))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need the recursive replacement?

)
} catch (error) {
console.error(error)
Expand Down