-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
70 lines (59 loc) · 1.79 KB
/
Copy pathindex.js
File metadata and controls
70 lines (59 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const minimist = require('minimist')
, semver = require('semver')
, log = require('fancy-log')
, fs = require('fs')
;
/**
* @param {function} cb
* @param {[{
* file: String,
* search:RegExp,
* replace:String
* }]} files
* @param {RegExp} initFileRegex
*/
module.exports = (cb, files, initFileRegex = /^\s*Version: (?<version>(?<major>\d).(?<minor>\d).(?<patch>.*))$/m) => {
const mainFile = files[0];
const mainFileContent = fs.readFileSync(mainFile.file).toString();
const {
version: old_version,
major: old_major,
minor: old_minor,
patch: old_patch
} = initFileRegex
.exec(mainFileContent)
.groups;
if (!semver.valid(old_version)) {
log('Current version is not valid.');
return cb();
}
log(`Current Version: ${old_version}`);
const args = process.argv.slice(3);
if (args.length === 0)
return cb(); // Only display
let argv = minimist(args);
const val = {
major: old_major,
minor: old_minor,
patch: old_patch,
version: old_version
};
['major', 'minor', 'patch'].forEach(k => {
if (argv[k] === true) {
val.version = semver.inc(val.version, k);
val[k] = semver[k](val.version);
} else if (k in argv) {
val[k] = argv[k];
val.version = [val.major, val.minor, val.patch].join('.')
}
});
// Pre release
if ('prerelease' in argv) {
val.version = semver.inc(val.version, 'prerelease', argv.prerelease);
}
log(`New Version: ${val.version}`);
files.forEach(({file, search, replace}) =>
fs.writeFileSync(file, fs.readFileSync(file).toString()
.replace(search, replace.replace('#NEW_VERSION#', val.version))));
cb();
};