-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbumpVersion.js
86 lines (70 loc) · 2.21 KB
/
bumpVersion.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
79
80
81
82
83
84
85
86
/* global __dirname, process, require */
const fs = require('fs')
const path = require('path')
const FILE_ENCODING = 'utf8'
const FILENAME = 'package.json'
const VERSION_MAJOR = 'major'
const VERSION_MINOR = 'minor'
const VERSION_PATCH = 'patch'
const REGEX_MAJOR_VERSION = /(\d+)\.\d+\.\d+/
const REGEX_MINOR_VERSION = /\d+\.(\d+)\.\d+/
const REGEX_PATCH_VERSION = /\d+\.\d+\.(\d+)/
const REGEX_VERSION_WITH_SPACES = /\s+"version":\s+"(.+)",/
const REGEX_VERSION = /"version":\s+"(.+)",/
const filePath = path.join(__dirname, FILENAME)
if (!fs.existsSync(filePath)) {
console.error(`"${FILENAME}" not found, bailing...`)
return 1
}
console.log(`"${FILENAME}" found, reading contents...`)
// Open file for reading and writing
// fs.readFileSync(filePath, { encoding: 'UTF8', flag: 'r+' })
// Open file for reading
const packContents = fs.readFileSync(filePath, {
encoding: FILE_ENCODING,
flag: 'r'
})
const results = REGEX_VERSION_WITH_SPACES.exec(packContents)
if (!results) {
console.error(`"version" not found in "${FILENAME}", bailing...`)
return 2
}
const currentVersion = results[1]
const major = parseInt(REGEX_MAJOR_VERSION.exec(currentVersion)[1], 10)
const minor = parseInt(REGEX_MINOR_VERSION.exec(currentVersion)[1], 10)
const patch = parseInt(REGEX_PATCH_VERSION.exec(currentVersion)[1], 10)
console.log(`Major Version: "${major}"`)
console.log(`Minor Version: "${minor}"`)
console.log(`Patch Version: "${patch}"`)
let newVersion = ''
const selectedBump =
process.argv.length > 2
? process.argv[2] // 'major' | 'minor' | 'patch'
: VERSION_PATCH
switch (selectedBump) {
case VERSION_MAJOR:
newVersion = `${major + 1}.0.0`
break
case VERSION_MINOR:
newVersion = `${major}.${minor + 1}.0`
break
case VERSION_PATCH:
newVersion = `${major}.${minor}.${patch + 1}`
break
default:
console.log(
`Invalid bump selected (script was provided "${selectedBump}")...`,
`Maintaining version "${currentVersion}"...`
)
newVersion = currentVersion
break
}
console.log(
`Bumping "${selectedBump}" from "${currentVersion}" to version "${newVersion}"...`
)
const newContents = packContents.replace(
REGEX_VERSION,
`"version": "${newVersion}",`
)
fs.writeFileSync(filePath, newContents, { encoding: FILE_ENCODING })
return 0