123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- #!/usr/bin/env node
- 'use strict'
- import { execFile } from 'node:child_process'
- import fs from 'node:fs/promises'
- import process from 'node:process'
- const VERBOSE = process.argv.includes('--verbose')
- const DRY_RUN = process.argv.includes('--dry') || process.argv.includes('--dry-run')
- const FILES = [
- 'README.md',
- 'test.md',
- 'preview/_config.yml',
- 'core/scss/_banner.scss',
- 'core/js/base.js'
- ]
- 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)
- )
- // Also replace the version used by the rubygem,
- // which is using periods (`.`) instead of hyphens (`-`)
- .replace(
- new RegExp(regExpQuote(oldVersion.replace(/-/g, '.')), 'g'),
- regExpQuoteReplacement(newVersion.replace(/-/g, '.'))
- )
- // No need to move any further if the strings are identical
- if (originalString === newString) {
- return
- }
- if (VERBOSE) {
- console.log(`Found ${oldVersion} in ${file}`)
- }
- if (DRY_RUN) {
- return
- }
- await fs.writeFile(file, newString, 'utf8')
- }
- function showUsage(args) {
- console.error('USAGE: change-version old_version new_version [--verbose] [--dry[-run]]')
- console.error('Got arguments:', args)
- process.exit(1)
- }
- function bumpPnpmVersion(newVersion) {
- if (DRY_RUN) {
- return
- }
- if (process.env.npm_package_version !== newVersion) {
- execFile('pnpm', ['version', '-r', newVersion, '--no-git-tag'], { shell: true }, error => {
- if (error) {
- console.error(error)
- process.exit(1)
- }
- })
- }
- }
- async function main(args) {
- let [oldVersion, newVersion] = args
- if (!oldVersion || !newVersion) {
- showUsage(args)
- }
- [oldVersion, newVersion] = [oldVersion, newVersion].map(arg => {
- return arg.startsWith('v') ? arg.slice(1) : arg
- })
- if (oldVersion === newVersion) {
- showUsage(args)
- }
- bumpPnpmVersion(newVersion)
- try {
- await Promise.all(
- FILES.map(file => replaceRecursively(file, oldVersion, newVersion))
- )
- } catch (error) {
- console.error(error)
- process.exit(1)
- }
- }
- main(process.argv.slice(2))
|