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