123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- #!/usr/bin/env node
- 'use strict'
- const fs = require('fs').promises
- const path = require('path')
- const globby = require('globby')
- const VERBOSE = process.argv.includes('--verbose')
- const DRY_RUN = process.argv.includes('--dry') || process.argv.includes('--dry-run')
- const GLOB = [
- '**/*.{css,html,js,json,md,scss,txt,yml}'
- ]
- const GLOBBY_OPTIONS = {
- cwd: path.join(__dirname, '..'),
- gitignore: true
- }
- function regExpQuote(string) {
- return string.replace(/[$()*+-.?[\\\]^{|}]/g, '\\$&')
- }
- function regExpQuoteReplacement(string) {
- return string.replace(/\$/g, '$$')
- }
- async function replaceRecursively(file, oldVersion, newVersion) {
- const originalString = await fs.readFile(file, 'utf8')
- const newString = originalString.replace(
- new RegExp(regExpQuote(oldVersion), 'g'), regExpQuoteReplacement(newVersion)
- )
-
- if (originalString === newString) {
- return
- }
- if (VERBOSE) {
- console.log(`FILE: ${file}`)
- }
- if (DRY_RUN) {
- return
- }
- await fs.writeFile(file, newString, 'utf8')
- }
- async function main(args) {
- const [oldVersion, newVersion] = args
- if (!oldVersion || !newVersion) {
- console.error('USAGE: change-version old_version new_version [--verbose] [--dry[-run]]')
- console.error('Got arguments:', args)
- process.exit(1)
- }
-
- [oldVersion, newVersion].map(arg => arg.startsWith('v') ? arg.slice(1) : arg)
- try {
- const files = await globby(GLOB, GLOBBY_OPTIONS)
- await Promise.all(files.map(file => replaceRecursively(file, oldVersion, newVersion)))
- } catch (error) {
- console.error(error)
- process.exit(1)
- }
- }
- main(process.argv.slice(2))
|