2015-12-01 23:25:09 -05:00
|
|
|
#!/usr/bin/env node
|
2016-12-31 00:25:26 -05:00
|
|
|
|
2015-12-01 23:25:09 -05:00
|
|
|
/*!
|
|
|
|
* Script to update version number references in the project.
|
2019-01-08 01:05:26 -05:00
|
|
|
* Copyright 2017-2019 The Bootstrap Authors
|
|
|
|
* Copyright 2017-2019 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
|
|
|
|
2018-11-28 06:03:28 -05:00
|
|
|
'use strict'
|
|
|
|
|
2017-09-26 08:14:52 -04:00
|
|
|
const fs = require('fs')
|
|
|
|
const path = require('path')
|
|
|
|
const sh = require('shelljs')
|
2017-11-28 03:43:09 -05:00
|
|
|
|
2016-12-31 00:25:26 -05:00
|
|
|
sh.config.fatal = true
|
2015-12-01 23:25:09 -05:00
|
|
|
|
|
|
|
// Blame TC39... https://github.com/benjamingr/RegExp.escape/issues/37
|
2017-12-16 07:00:38 -05:00
|
|
|
function regExpQuote(string) {
|
|
|
|
return string.replace(/[-\\^$*+?.()|[\]{}]/g, '\\$&')
|
|
|
|
}
|
|
|
|
|
|
|
|
function regExpQuoteReplacement(string) {
|
|
|
|
return string.replace(/[$]/g, '$$')
|
|
|
|
}
|
2015-12-01 23:25:09 -05:00
|
|
|
|
2017-09-26 08:14:52 -04:00
|
|
|
const 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
|
|
|
}
|
2019-02-26 06:20:34 -05:00
|
|
|
|
2017-09-26 08:14:52 -04:00
|
|
|
fs.readdir(directory, (err, names) => {
|
2015-12-01 23:25:09 -05:00
|
|
|
if (err) {
|
2016-12-31 00:25:26 -05:00
|
|
|
errback(err)
|
|
|
|
return
|
2015-12-01 23:25:09 -05:00
|
|
|
}
|
2019-02-26 06:20:34 -05:00
|
|
|
|
|
|
|
names.forEach(name => {
|
2017-09-26 08:14:52 -04:00
|
|
|
const filepath = path.join(directory, name)
|
|
|
|
fs.lstat(filepath, (err, stats) => {
|
2015-12-01 23:25:09 -05:00
|
|
|
if (err) {
|
2016-12-31 00:25:26 -05:00
|
|
|
process.nextTick(errback, err)
|
|
|
|
return
|
2015-12-01 23:25:09 -05:00
|
|
|
}
|
2019-02-26 06:20:34 -05:00
|
|
|
|
2017-12-16 07:00:38 -05:00
|
|
|
if (stats.isDirectory()) {
|
2016-12-31 00:25:26 -05:00
|
|
|
process.nextTick(walkAsync, filepath, excludedDirectories, fileCallback, errback)
|
2017-12-16 07:00:38 -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) {
|
2017-12-16 07:00:38 -05:00
|
|
|
original = new RegExp(regExpQuote(original), 'g')
|
|
|
|
replacement = regExpQuoteReplacement(replacement)
|
2019-02-26 06:20:34 -05:00
|
|
|
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)
|
|
|
|
}
|
2015-12-17 00:26:29 -05:00
|
|
|
}
|
2019-02-26 06:20:34 -05:00
|
|
|
|
|
|
|
walkAsync(directory, excludedDirectories, updateFile, 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
|
|
|
}
|
2019-02-26 06:20:34 -05:00
|
|
|
|
2017-09-26 08:14:52 -04:00
|
|
|
const oldVersion = args[0]
|
|
|
|
const newVersion = args[1]
|
|
|
|
const EXCLUDED_DIRS = new Set([
|
2015-12-01 23:25:09 -05:00
|
|
|
'.git',
|
2019-08-19 02:37:40 -04:00
|
|
|
'_gh_pages',
|
2015-12-01 23:25:09 -05:00
|
|
|
'node_modules',
|
|
|
|
'vendor'
|
2016-12-31 00:25:26 -05:00
|
|
|
])
|
2017-09-26 08:14:52 -04:00
|
|
|
const INCLUDED_EXTENSIONS = new Set([
|
2015-12-01 23:25:09 -05:00
|
|
|
// This extension whitelist is how we avoid modifying binary files
|
|
|
|
'',
|
|
|
|
'.css',
|
|
|
|
'.html',
|
|
|
|
'.js',
|
|
|
|
'.json',
|
|
|
|
'.md',
|
2019-11-26 02:00:21 -05:00
|
|
|
'.nuspec',
|
2015-12-01 23:25:09 -05:00
|
|
|
'.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))
|