Skip to content

Commit

Permalink
Improve change-version script
Browse files Browse the repository at this point in the history
* switch to ESM
* only replace the version in specific files instead of blindly all repo files
  • Loading branch information
XhmikosR committed Jul 31, 2023
1 parent 92aedd1 commit 088a6b4
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 21 deletions.
14 changes: 13 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
"overrides": [
{
"files": [
"build/**"
"build/**/*.js"
],
"env": {
"browser": false,
Expand All @@ -115,6 +115,18 @@
"unicorn/prefer-top-level-await": "off"
}
},
{
"files": [
"build/**/*.mjs"
],
"parserOptions": {
"sourceType": "module"
},
"rules": {
"no-console": "off",
"unicorn/prefer-top-level-await": "off"
}
},
{
"files": [
"js/**"
Expand Down
66 changes: 48 additions & 18 deletions build/change-version.js → build/change-version.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,43 @@
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
*/

'use strict'

const fs = require('node:fs').promises
const path = require('node:path')
const globby = require('globby')
import { execFile } from 'node:child_process'
import { promises as fs } from 'node:fs'
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
/**
* @param {string} string
*/
function regExpQuote(string) {
return string.replace(/[$()*+-.?[\\\]^{|}]/g, '\\$&')
}

/**
* @param {string} string
*/
function regExpQuoteReplacement(string) {
return string.replace(/\$/g, '$$')
}

/**
* @param {import("fs").PathLike | fs.FileHandle} file
* @param {string} oldVersion
* @param {string} newVersion
*/
async function replaceRecursively(file, oldVersion, newVersion) {
const originalString = await fs.readFile(file, 'utf8')
const newString = originalString
Expand All @@ -53,7 +63,7 @@ async function replaceRecursively(file, oldVersion, newVersion) {
}

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

if (DRY_RUN) {
Expand All @@ -63,12 +73,34 @@ async function replaceRecursively(file, oldVersion, newVersion) {
await fs.writeFile(file, newString, 'utf8')
}

/**
* @param {string} newVersion
*/
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)
}
})
}

/**
* @param {string[]} args
*/
function showUsage(args) {
console.error('USAGE: change-version old_version new_version [--verbose] [--dry[-run]]')
console.error('Got arguments:', args)
process.exit(1)
}

/**
* @param {string[]} args
*/
async function main(args) {
let [oldVersion, newVersion] = args

Expand All @@ -86,12 +118,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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"js-compile-standalone-esm": "rollup --environment ESM:true,BUNDLE:false --config build/rollup.config.js --sourcemap",
"js-compile-bundle": "rollup --environment BUNDLE:true --config build/rollup.config.js --sourcemap",
"js-compile-plugins": "node build/build-plugins.js",
"js-lint": "eslint --cache --cache-location .cache/.eslintcache --report-unused-disable-directives --ext .html,.js,.md .",
"js-lint": "eslint --cache --cache-location .cache/.eslintcache --report-unused-disable-directives --ext .html,.js,.mjs,.md .",
"js-minify": "npm-run-all --aggregate-output --parallel js-minify-*",
"js-minify-standalone": "terser --compress passes=2 --mangle --comments \"/^!/\" --source-map \"content=dist/js/bootstrap.js.map,includeSources,url=bootstrap.min.js.map\" --output dist/js/bootstrap.min.js dist/js/bootstrap.js",
"js-minify-standalone-esm": "terser --compress passes=2 --mangle --comments \"/^!/\" --source-map \"content=dist/js/bootstrap.esm.js.map,includeSources,url=bootstrap.esm.min.js.map\" --output dist/js/bootstrap.esm.min.js dist/js/bootstrap.esm.js",
Expand All @@ -85,7 +85,7 @@
"update-deps": "ncu -u -x globby,jasmine,karma-browserstack-launcher,karma-rollup-preprocessor && echo Manually update site/assets/js/vendor",
"release": "npm-run-all dist release-sri docs-build release-zip*",
"release-sri": "node build/generate-sri.js",
"release-version": "node build/change-version.js",
"release-version": "node build/change-version.mjs",
"release-zip": "cross-env-shell \"rm -rf bootstrap-$npm_package_version-dist bootstrap-$npm_package_version-dist.zip && cp -r dist/ bootstrap-$npm_package_version-dist && zip -qr9 bootstrap-$npm_package_version-dist.zip bootstrap-$npm_package_version-dist && rm -rf bootstrap-$npm_package_version-dist\"",
"release-zip-examples": "node build/zip-examples.js",
"dist": "npm-run-all --aggregate-output --parallel css js",
Expand Down

0 comments on commit 088a6b4

Please sign in to comment.