| 1 |
#!/usr/bin/env node |
| 2 |
'use strict'; |
| 3 |
|
| 4 |
/* globals Set */ |
| 5 |
/*! |
| 6 |
* Script to update version number references in the project. |
| 7 |
* Copyright 2015-2019 Twitter, Inc. |
| 8 |
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE) |
| 9 |
*/ |
| 10 |
var fs = require('fs'); |
| 11 |
var path = require('path'); |
| 12 |
var sh = require('shelljs'); |
| 13 |
sh.config.fatal = true; |
| 14 |
var sed = sh.sed; |
| 15 |
|
| 16 |
// Blame TC39... https://github.com/benjamingr/RegExp.escape/issues/37 |
| 17 |
RegExp.quote = function (string) { |
| 18 |
return string.replace(/[-\\^$*+?.()|[\]{}]/g, '\\$&'); |
| 19 |
}; |
| 20 |
RegExp.quoteReplacement = function (string) { |
| 21 |
return string.replace(/[$]/g, '$$'); |
| 22 |
}; |
| 23 |
|
| 24 |
var DRY_RUN = false; |
| 25 |
|
| 26 |
function walkAsync(directory, excludedDirectories, fileCallback, errback) { |
| 27 |
if (excludedDirectories.has(path.parse(directory).base)) { |
| 28 |
return; |
| 29 |
} |
| 30 |
fs.readdir(directory, function (err, names) { |
| 31 |
if (err) { |
| 32 |
errback(err); |
| 33 |
return; |
| 34 |
} |
| 35 |
names.forEach(function (name) { |
| 36 |
var filepath = path.join(directory, name); |
| 37 |
fs.lstat(filepath, function (err, stats) { |
| 38 |
if (err) { |
| 39 |
process.nextTick(errback, err); |
| 40 |
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 |
original = new RegExp(RegExp.quote(original), 'g'); |
| 58 |
replacement = RegExp.quoteReplacement(replacement); |
| 59 |
var updateFile = !DRY_RUN ? function (filepath) { |
| 60 |
if (allowedExtensions.has(path.parse(filepath).ext)) { |
| 61 |
sed('-i', original, replacement, filepath); |
| 62 |
} |
| 63 |
} : function (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, function (err) { |
| 72 |
console.error('ERROR while traversing directory!:'); |
| 73 |
console.error(err); |
| 74 |
process.exit(1); |
| 75 |
}); |
| 76 |
} |
| 77 |
|
| 78 |
function main(args) { |
| 79 |
if (args.length !== 2) { |
| 80 |
console.error('USAGE: change-version old_version new_version'); |
| 81 |
console.error('Got arguments:', args); |
| 82 |
process.exit(1); |
| 83 |
} |
| 84 |
var oldVersion = args[0]; |
| 85 |
var newVersion = args[1]; |
| 86 |
var EXCLUDED_DIRS = new Set([ |
| 87 |
'.git', |
| 88 |
'node_modules', |
| 89 |
'vendor' |
| 90 |
]); |
| 91 |
var INCLUDED_EXTENSIONS = new Set([ |
| 92 |
// This extension whitelist is how we avoid modifying binary files |
| 93 |
'', |
| 94 |
'.css', |
| 95 |
'.html', |
| 96 |
'.js', |
| 97 |
'.json', |
| 98 |
'.less', |
| 99 |
'.md', |
| 100 |
'.nuspec', |
| 101 |
'.ps1', |
| 102 |
'.scss', |
| 103 |
'.txt', |
| 104 |
'.yml' |
| 105 |
]); |
| 106 |
replaceRecursively('.', EXCLUDED_DIRS, INCLUDED_EXTENSIONS, oldVersion, newVersion); |
| 107 |
} |
| 108 |
|
| 109 |
main(process.argv.slice(2)); |