12
const fs = require('fs').promises
13
const path = require('path')
14
const globby = require('globby')
16
const VERBOSE = process.argv.includes('--verbose')
17
const DRY_RUN = process.argv.includes('--dry') || process.argv.includes('--dry-run')
21
'**/*.{css,html,js,json,md,scss,txt,yml}'
23
const GLOBBY_OPTIONS = {
24
cwd: path.join(__dirname, '..'),
29
function regExpQuote(string) {
30
return string.replace(/[$()*+.?[\\\]^{|}-]/g, '\\$&')
33
function regExpQuoteReplacement(string) {
34
return string.replace(/\$/g, '$$')
37
async function replaceRecursively(file, oldVersion, newVersion) {
38
const originalString = await fs.readFile(file, 'utf8')
39
const newString = originalString.replace(
40
new RegExp(regExpQuote(oldVersion), 'g'), regExpQuoteReplacement(newVersion)
44
if (originalString === newString) {
49
console.log(`FILE: ${file}`)
56
await fs.writeFile(file, newString, 'utf8')
59
async function main(args) {
60
const [oldVersion, newVersion] = args
62
if (!oldVersion || !newVersion) {
63
console.error('USAGE: change-version old_version new_version [--verbose] [--dry[-run]]')
64
console.error('Got arguments:', args)
69
[oldVersion, newVersion].map(arg => arg.startsWith('v') ? arg.slice(1) : arg)
72
const files = await globby(GLOB, GLOBBY_OPTIONS)
74
await Promise.all(files.map(file => replaceRecursively(file, oldVersion, newVersion)))
81
main(process.argv.slice(2))