talos

Форк
0
81 строка · 2.1 Кб
1
#!/usr/bin/env node
2

3
/*!
4
 * Script to update version number references in the project.
5
 * Copyright 2017-2021 The Bootstrap Authors
6
 * Copyright 2017-2021 Twitter, Inc.
7
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
8
 */
9

10
'use strict'
11

12
const fs = require('fs').promises
13
const path = require('path')
14
const globby = require('globby')
15

16
const VERBOSE = process.argv.includes('--verbose')
17
const DRY_RUN = process.argv.includes('--dry') || process.argv.includes('--dry-run')
18

19
// These are the filetypes we only care about replacing the version
20
const GLOB = [
21
  '**/*.{css,html,js,json,md,scss,txt,yml}'
22
]
23
const GLOBBY_OPTIONS = {
24
  cwd: path.join(__dirname, '..'),
25
  gitignore: true
26
}
27

28
// Blame TC39... https://github.com/benjamingr/RegExp.escape/issues/37
29
function regExpQuote(string) {
30
  return string.replace(/[$()*+.?[\\\]^{|}-]/g, '\\$&')
31
}
32

33
function regExpQuoteReplacement(string) {
34
  return string.replace(/\$/g, '$$')
35
}
36

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)
41
  )
42

43
  // No need to move any further if the strings are identical
44
  if (originalString === newString) {
45
    return
46
  }
47

48
  if (VERBOSE) {
49
    console.log(`FILE: ${file}`)
50
  }
51

52
  if (DRY_RUN) {
53
    return
54
  }
55

56
  await fs.writeFile(file, newString, 'utf8')
57
}
58

59
async function main(args) {
60
  const [oldVersion, newVersion] = args
61

62
  if (!oldVersion || !newVersion) {
63
    console.error('USAGE: change-version old_version new_version [--verbose] [--dry[-run]]')
64
    console.error('Got arguments:', args)
65
    process.exit(1)
66
  }
67

68
  // Strip any leading `v` from arguments because otherwise we will end up with duplicate `v`s
69
  [oldVersion, newVersion].map(arg => arg.startsWith('v') ? arg.slice(1) : arg)
70

71
  try {
72
    const files = await globby(GLOB, GLOBBY_OPTIONS)
73

74
    await Promise.all(files.map(file => replaceRecursively(file, oldVersion, newVersion)))
75
  } catch (error) {
76
    console.error(error)
77
    process.exit(1)
78
  }
79
}
80

81
main(process.argv.slice(2))
82

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.