change-version.js 2.72 KB
Newer Older
1
#!/usr/bin/env node
2

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

10
11
'use strict'

12
13
14
const fs = require('fs')
const path = require('path')
const sh = require('shelljs')
15

16
sh.config.fatal = true
17
18

// Blame TC39... https://github.com/benjamingr/RegExp.escape/issues/37
XhmikosR's avatar
XhmikosR committed
19
20
21
22
23
24
25
function regExpQuote(string) {
  return string.replace(/[-\\^$*+?.()|[\]{}]/g, '\\$&')
}

function regExpQuoteReplacement(string) {
  return string.replace(/[$]/g, '$$')
}
26

27
const DRY_RUN = false
28
29
30

function walkAsync(directory, excludedDirectories, fileCallback, errback) {
  if (excludedDirectories.has(path.parse(directory).base)) {
31
    return
32
  }
XhmikosR's avatar
XhmikosR committed
33

34
  fs.readdir(directory, (err, names) => {
35
    if (err) {
36
37
      errback(err)
      return
38
    }
XhmikosR's avatar
XhmikosR committed
39
40

    names.forEach(name => {
41
42
      const filepath = path.join(directory, name)
      fs.lstat(filepath, (err, stats) => {
43
        if (err) {
44
45
          process.nextTick(errback, err)
          return
46
        }
XhmikosR's avatar
XhmikosR committed
47

XhmikosR's avatar
XhmikosR committed
48
        if (stats.isDirectory()) {
49
          process.nextTick(walkAsync, filepath, excludedDirectories, fileCallback, errback)
XhmikosR's avatar
XhmikosR committed
50
        } else if (stats.isFile()) {
51
          process.nextTick(fileCallback, filepath)
52
        }
53
54
55
      })
    })
  })
56
57
58
}

function replaceRecursively(directory, excludedDirectories, allowedExtensions, original, replacement) {
XhmikosR's avatar
XhmikosR committed
59
60
  original = new RegExp(regExpQuote(original), 'g')
  replacement = regExpQuoteReplacement(replacement)
XhmikosR's avatar
XhmikosR committed
61
62
63
64
65
66
67
68
69
70
71
72
  const updateFile = DRY_RUN ?
    filepath => {
      if (allowedExtensions.has(path.parse(filepath).ext)) {
        console.log(`FILE: ${filepath}`)
      } else {
        console.log(`EXCLUDED:${filepath}`)
      }
    } :
    filepath => {
      if (allowedExtensions.has(path.parse(filepath).ext)) {
        sh.sed('-i', original, replacement, filepath)
      }
73
    }
XhmikosR's avatar
XhmikosR committed
74
75

  walkAsync(directory, excludedDirectories, updateFile, err => {
76
77
78
79
    console.error('ERROR while traversing directory!:')
    console.error(err)
    process.exit(1)
  })
80
81
82
83
}

function main(args) {
  if (args.length !== 2) {
84
85
86
    console.error('USAGE: change-version old_version new_version')
    console.error('Got arguments:', args)
    process.exit(1)
87
  }
XhmikosR's avatar
XhmikosR committed
88

89
90
91
  const oldVersion = args[0]
  const newVersion = args[1]
  const EXCLUDED_DIRS = new Set([
92
    '.git',
93
    '_gh_pages',
94
95
    'node_modules',
    'vendor'
96
  ])
97
  const INCLUDED_EXTENSIONS = new Set([
98
99
100
101
102
103
104
105
106
107
    // This extension whitelist is how we avoid modifying binary files
    '',
    '.css',
    '.html',
    '.js',
    '.json',
    '.md',
    '.scss',
    '.txt',
    '.yml'
108
109
  ])
  replaceRecursively('.', EXCLUDED_DIRS, INCLUDED_EXTENSIONS, oldVersion, newVersion)
110
}
111

112
main(process.argv.slice(2))