forked from davidfig/json-bump
/
version.js
78 lines (73 loc) · 2.56 KB
/
version.js
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
71
72
73
74
75
76
77
78
const jsonfile = require('jsonfile')
/**
* bumps the "version" entry for a .json file
* defaults to incrementing PATCH by 1 if no options are provided
* @param {string} filename
* @param {object} [options]
* @param {string="version"} [entry] name of entry to change
* @param {number} [major] increment major by number (resetting MINOR and PATCH to 0)
* @param {number} [minor] increment minor by number (resetting PATCH to 0)
* @param {number} [patch] increment patch by number
* @param {string} [replace] replace entry with this string
* @param {number=4} [spaces] number of spaces to format jsonfile (set to 0 to remove all spaces)
* @return {object.<string, string>} returns { original, updated }
*/
module.exports = function version(filename, options) {
options = options || {}
if (!options.replace && !options.major && !options.minor && !options.patch) {
options.patch = 1
}
options.spaces = typeof options.spaces === 'undefined' ? 4 : options.spaces
let json
try {
if (filename.indexOf('/') === -1 && filename.indexOf('\\') === -1) {
filename = process.cwd() + '/' + filename
}
json = jsonfile.readFileSync(filename)
} catch (e) {
console.error('ERROR opening file ' + filename + ' (' + e.error + ')')
process.exit(1)
}
options.entry = options.entry || 'version'
const current = json[options.entry]
let updated, split
if (options.replace) {
json[options.entry] = options.entry
updated = options.entry
} else if (options.pre) {
json[options.entry] = options.pre
updated = current + '-' + options.pre
options.replace = updated
} else {
split = current.split('.')
if (split.length !== 3) {
console.warn(
'WARNING version in ' +
filename +
' was not in MAJOR.MINOR.PATCH format'
)
for (let i = split.length; i < 3; i++) {
split[i] = '0'
}
}
if (options.major) {
split[0] = parseInt(split[0]) + options.major
split[1] = '0'
split[2] = '0'
} else if (options.minor) {
split[1] = parseInt(split[1]) + options.minor
split[2] = '0'
} else if (options.patch) {
split[2] = parseInt(split[2]) + options.patch
}
}
if (split) {
json[options.entry] =
parseInt(split[0]) + '.' + parseInt(split[1]) + '.' + parseInt(split[2])
} else {
json[options.entry] = options.replace
}
updated = json[options.entry]
jsonfile.writeFileSync(filename, json, { spaces: options.spaces })
return { original: current, updated }
}