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