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