2015-12-02 04:25:09 +00:00
|
|
|
#!/usr/bin/env node
|
2016-12-31 05:25:26 +00:00
|
|
|
|
2015-12-02 04:25:09 +00:00
|
|
|
/*!
|
|
|
|
* Script to update version number references in the project.
|
2018-01-15 04:08:58 +00:00
|
|
|
* Copyright 2017-2018 The Bootstrap Authors
|
|
|
|
* Copyright 2017-2018 Twitter, Inc.
|
2015-12-02 04:25:09 +00:00
|
|
|
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
|
|
|
|
*/
|
2016-12-31 05:25:26 +00:00
|
|
|
|
2017-09-26 12:14:52 +00:00
|
|
|
const fs = require('fs')
|
|
|
|
const path = require('path')
|
|
|
|
const sh = require('shelljs')
|
2017-11-28 08:43:09 +00:00
|
|
|
|
2016-12-31 05:25:26 +00:00
|
|
|
sh.config.fatal = true
|
2015-12-02 04:25:09 +00:00
|
|
|
|
|
|
|
// Blame TC39... https://github.com/benjamingr/RegExp.escape/issues/37
|
2017-12-16 12:00:38 +00:00
|
|
|
function regExpQuote(string) {
|
|
|
|
return string.replace(/[-\\^$*+?.()|[\]{}]/g, '\\$&')
|
|
|
|
}
|
|
|
|
|
|
|
|
function regExpQuoteReplacement(string) {
|
|
|
|
return string.replace(/[$]/g, '$$')
|
|
|
|
}
|
2015-12-02 04:25:09 +00:00
|
|
|
|
2017-09-26 12:14:52 +00:00
|
|
|
const DRY_RUN = false
|
2015-12-02 04:25:09 +00:00
|
|
|
|
|
|
|
function walkAsync(directory, excludedDirectories, fileCallback, errback) {
|
|
|
|
if (excludedDirectories.has(path.parse(directory).base)) {
|
2016-12-31 05:25:26 +00:00
|
|
|
return
|
2015-12-02 04:25:09 +00:00
|
|
|
}
|
2017-09-26 12:14:52 +00:00
|
|
|
fs.readdir(directory, (err, names) => {
|
2015-12-02 04:25:09 +00:00
|
|
|
if (err) {
|
2016-12-31 05:25:26 +00:00
|
|
|
errback(err)
|
|
|
|
return
|
2015-12-02 04:25:09 +00:00
|
|
|
}
|
2017-09-26 12:14:52 +00:00
|
|
|
names.forEach((name) => {
|
|
|
|
const filepath = path.join(directory, name)
|
|
|
|
fs.lstat(filepath, (err, stats) => {
|
2015-12-02 04:25:09 +00:00
|
|
|
if (err) {
|
2016-12-31 05:25:26 +00:00
|
|
|
process.nextTick(errback, err)
|
|
|
|
return
|
2015-12-02 04:25:09 +00:00
|
|
|
}
|
2017-12-16 12:00:38 +00:00
|
|
|
if (stats.isDirectory()) {
|
2016-12-31 05:25:26 +00:00
|
|
|
process.nextTick(walkAsync, filepath, excludedDirectories, fileCallback, errback)
|
2017-12-16 12:00:38 +00:00
|
|
|
} else if (stats.isFile()) {
|
2016-12-31 05:25:26 +00:00
|
|
|
process.nextTick(fileCallback, filepath)
|
2015-12-02 04:25:09 +00:00
|
|
|
}
|
2016-12-31 05:25:26 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2015-12-02 04:25:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function replaceRecursively(directory, excludedDirectories, allowedExtensions, original, replacement) {
|
2017-12-16 12:00:38 +00:00
|
|
|
original = new RegExp(regExpQuote(original), 'g')
|
|
|
|
replacement = regExpQuoteReplacement(replacement)
|
|
|
|
const updateFile = DRY_RUN ? (filepath) => {
|
2015-12-17 05:26:29 +00:00
|
|
|
if (allowedExtensions.has(path.parse(filepath).ext)) {
|
2017-12-16 12:00:38 +00:00
|
|
|
console.log(`FILE: ${filepath}`)
|
|
|
|
} else {
|
|
|
|
console.log(`EXCLUDED:${filepath}`)
|
2015-12-17 05:26:29 +00:00
|
|
|
}
|
2017-09-26 12:14:52 +00:00
|
|
|
} : (filepath) => {
|
2015-12-17 05:26:29 +00:00
|
|
|
if (allowedExtensions.has(path.parse(filepath).ext)) {
|
2017-12-16 12:00:38 +00:00
|
|
|
sh.sed('-i', original, replacement, filepath)
|
2015-12-17 05:26:29 +00:00
|
|
|
}
|
2016-12-31 05:25:26 +00:00
|
|
|
}
|
2017-09-26 12:14:52 +00:00
|
|
|
walkAsync(directory, excludedDirectories, updateFile, (err) => {
|
2016-12-31 05:25:26 +00:00
|
|
|
console.error('ERROR while traversing directory!:')
|
|
|
|
console.error(err)
|
|
|
|
process.exit(1)
|
|
|
|
})
|
2015-12-02 04:25:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function main(args) {
|
|
|
|
if (args.length !== 2) {
|
2016-12-31 05:25:26 +00:00
|
|
|
console.error('USAGE: change-version old_version new_version')
|
|
|
|
console.error('Got arguments:', args)
|
|
|
|
process.exit(1)
|
2015-12-02 04:25:09 +00:00
|
|
|
}
|
2017-09-26 12:14:52 +00:00
|
|
|
const oldVersion = args[0]
|
|
|
|
const newVersion = args[1]
|
|
|
|
const EXCLUDED_DIRS = new Set([
|
2015-12-02 04:25:09 +00:00
|
|
|
'.git',
|
|
|
|
'node_modules',
|
|
|
|
'vendor'
|
2016-12-31 05:25:26 +00:00
|
|
|
])
|
2017-09-26 12:14:52 +00:00
|
|
|
const INCLUDED_EXTENSIONS = new Set([
|
2015-12-02 04:25:09 +00:00
|
|
|
// This extension whitelist is how we avoid modifying binary files
|
|
|
|
'',
|
|
|
|
'.css',
|
|
|
|
'.html',
|
|
|
|
'.js',
|
|
|
|
'.json',
|
|
|
|
'.md',
|
|
|
|
'.scss',
|
|
|
|
'.txt',
|
|
|
|
'.yml'
|
2016-12-31 05:25:26 +00:00
|
|
|
])
|
|
|
|
replaceRecursively('.', EXCLUDED_DIRS, INCLUDED_EXTENSIONS, oldVersion, newVersion)
|
2015-12-17 05:15:33 +00:00
|
|
|
}
|
2015-12-02 04:25:09 +00:00
|
|
|
|
2016-12-31 05:25:26 +00:00
|
|
|
main(process.argv.slice(2))
|