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

'use strict'
4
5
6

/*!
 * Script to update version number references in the project.
7
8
 * Copyright 2017 The Bootstrap Authors
 * Copyright 2017 Twitter, Inc.
9
10
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
 */
11
12
13

/* global Set */

14
15
16
const fs = require('fs')
const path = require('path')
const sh = require('shelljs')
17

18
sh.config.fatal = true
19
20

// Blame TC39... https://github.com/benjamingr/RegExp.escape/issues/37
21
22
RegExp.quote = (string) => string.replace(/[-\\^$*+?.()|[\]{}]/g, '\\$&')
RegExp.quoteReplacement = (string) => string.replace(/[$]/g, '$$')
23

24
const DRY_RUN = false
25
26
27

function walkAsync(directory, excludedDirectories, fileCallback, errback) {
  if (excludedDirectories.has(path.parse(directory).base)) {
28
    return
29
  }
30
  fs.readdir(directory, (err, names) => {
31
    if (err) {
32
33
      errback(err)
      return
34
    }
35
36
37
    names.forEach((name) => {
      const filepath = path.join(directory, name)
      fs.lstat(filepath, (err, stats) => {
38
        if (err) {
39
40
          process.nextTick(errback, err)
          return
41
42
        }
        if (stats.isSymbolicLink()) {
43
          return
44
45
        }
        else if (stats.isDirectory()) {
46
          process.nextTick(walkAsync, filepath, excludedDirectories, fileCallback, errback)
47
48
        }
        else if (stats.isFile()) {
49
          process.nextTick(fileCallback, filepath)
50
        }
51
52
53
      })
    })
  })
54
55
56
}

function replaceRecursively(directory, excludedDirectories, allowedExtensions, original, replacement) {
57
58
  original = new RegExp(RegExp.quote(original), 'g')
  replacement = RegExp.quoteReplacement(replacement)
59
  const updateFile = !DRY_RUN ? (filepath) => {
60
    if (allowedExtensions.has(path.parse(filepath).ext)) {
61
      sh.sed('-i', original, replacement, filepath)
62
    }
63
  } : (filepath) => {
64
    if (allowedExtensions.has(path.parse(filepath).ext)) {
65
      console.log(`FILE: ${filepath}`)
66
67
    }
    else {
68
      console.log(`EXCLUDED:${filepath}`)
69
    }
70
  }
71
  walkAsync(directory, excludedDirectories, updateFile, (err) => {
72
73
74
75
    console.error('ERROR while traversing directory!:')
    console.error(err)
    process.exit(1)
  })
76
77
78
79
}

function main(args) {
  if (args.length !== 2) {
80
81
82
    console.error('USAGE: change-version old_version new_version')
    console.error('Got arguments:', args)
    process.exit(1)
83
  }
84
85
86
  const oldVersion = args[0]
  const newVersion = args[1]
  const EXCLUDED_DIRS = new Set([
87
88
89
    '.git',
    'node_modules',
    'vendor'
90
  ])
91
  const INCLUDED_EXTENSIONS = new Set([
92
93
94
95
96
97
98
99
100
101
    // This extension whitelist is how we avoid modifying binary files
    '',
    '.css',
    '.html',
    '.js',
    '.json',
    '.md',
    '.scss',
    '.txt',
    '.yml'
102
103
  ])
  replaceRecursively('.', EXCLUDED_DIRS, INCLUDED_EXTENSIONS, oldVersion, newVersion)
104
}
105

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