|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/*! |
| 4 | + * Script to update version number references in the project. |
| 5 | + * Copyright 2023 The Bootstrap Authors |
| 6 | + * Licensed under MIT (https://github.com/twbs/icons/blob/main/LICENSE) |
| 7 | + */ |
| 8 | + |
| 9 | +'use strict' |
| 10 | + |
| 11 | +const { execFile } = require('node:child_process') |
| 12 | +const fs = require('node:fs').promises |
| 13 | + |
| 14 | +const VERBOSE = process.argv.includes('--verbose') |
| 15 | +const DRY_RUN = process.argv.includes('--dry') || process.argv.includes('--dry-run') |
| 16 | + |
| 17 | +// These are the files we only care about replacing the version |
| 18 | +const FILES = [ |
| 19 | + 'build/font/css.hbs', |
| 20 | + 'build/font/scss.hbs', |
| 21 | + 'config.yml' |
| 22 | +] |
| 23 | + |
| 24 | +// Blame TC39... https://github.com/benjamingr/RegExp.escape/issues/37 |
| 25 | +function regExpQuote(string) { |
| 26 | + return string.replace(/[$()*+-.?[\\\]^{|}]/g, '\\$&') |
| 27 | +} |
| 28 | + |
| 29 | +function regExpQuoteReplacement(string) { |
| 30 | + return string.replace(/\$/g, '$$') |
| 31 | +} |
| 32 | + |
| 33 | +async function replaceRecursively(file, oldVersion, newVersion) { |
| 34 | + const originalString = await fs.readFile(file, 'utf8') |
| 35 | + const newString = originalString.replace( |
| 36 | + new RegExp(regExpQuote(oldVersion), 'g'), |
| 37 | + regExpQuoteReplacement(newVersion) |
| 38 | + ) |
| 39 | + |
| 40 | + // No need to move any further if the strings are identical |
| 41 | + if (originalString === newString) { |
| 42 | + return |
| 43 | + } |
| 44 | + |
| 45 | + if (VERBOSE) { |
| 46 | + console.log(`Found ${oldVersion} in ${file}`) |
| 47 | + } |
| 48 | + |
| 49 | + if (DRY_RUN) { |
| 50 | + return |
| 51 | + } |
| 52 | + |
| 53 | + await fs.writeFile(file, newString, 'utf8') |
| 54 | +} |
| 55 | + |
| 56 | +function bumpNpmVersion(newVersion) { |
| 57 | + if (DRY_RUN) { |
| 58 | + return |
| 59 | + } |
| 60 | + |
| 61 | + execFile('npm', ['version', newVersion, '--no-git-tag'], { shell: true }, (error) => { |
| 62 | + if (error) { |
| 63 | + console.error(error) |
| 64 | + process.exit(1) |
| 65 | + } |
| 66 | + }) |
| 67 | +} |
| 68 | + |
| 69 | +function showUsage(args) { |
| 70 | + console.error('USAGE: change-version old_version new_version [--verbose] [--dry[-run]]') |
| 71 | + console.error('Got arguments:', args) |
| 72 | + process.exit(1) |
| 73 | +} |
| 74 | + |
| 75 | +async function main(args) { |
| 76 | + let [oldVersion, newVersion] = args |
| 77 | + |
| 78 | + if (!oldVersion || !newVersion) { |
| 79 | + showUsage(args) |
| 80 | + } |
| 81 | + |
| 82 | + // Strip any leading `v` from arguments because |
| 83 | + // otherwise we will end up with duplicate `v`s |
| 84 | + [oldVersion, newVersion] = [oldVersion, newVersion].map(arg => { |
| 85 | + return arg.startsWith('v') ? arg.slice(1) : arg |
| 86 | + }) |
| 87 | + |
| 88 | + if (oldVersion === newVersion) { |
| 89 | + showUsage(args) |
| 90 | + } |
| 91 | + |
| 92 | + bumpNpmVersion(newVersion) |
| 93 | + |
| 94 | + try { |
| 95 | + await Promise.all(FILES.map(file => replaceRecursively(file, oldVersion, newVersion))) |
| 96 | + } catch (error) { |
| 97 | + console.error(error) |
| 98 | + process.exit(1) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +main(process.argv.slice(2)) |
0 commit comments