-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbump-version.js
71 lines (59 loc) · 2.18 KB
/
bump-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
import fs from 'fs'
import { execSync } from 'child_process'
const incrementType = process.argv.slice(2)
if (incrementType.length === 0) {
console.log('Please specify one of these: --major | --minor | --patch | --undo')
process.exit(1)
}
const installFile = 'install.sh'
const incrementVersion = (version, type) => {
const versionParts = version.split('.').map(Number)
switch (type) {
case '--major':
versionParts[0] += 1
versionParts[1] = 0
versionParts[2] = 0
break
case '--minor':
versionParts[1] += 1
versionParts[2] = 0
break
case '--patch':
versionParts[2] += 1
break
}
return versionParts.join('.')
}
try {
const lastTag = execSync('git describe --tags --abbrev=0').toString().trim()
if (!lastTag) {
throw new Error('No git tags found')
}
if (incrementType.includes('--undo')) {
// check if last commit is "chore: bump version"
const lastCommit = execSync('git log -1 --pretty=%B').toString().trim()
if (lastCommit !== 'chore: bump version') {
console.log('Last commit is not "chore: bump version" so nothing to undo')
process.exit(1)
}
execSync(`git tag -d ${lastTag}`)
execSync(`git stash`)
execSync(`git reset --hard HEAD~1`)
execSync(`git stash pop`)
process.exit(0)
}
const lastVersion = lastTag.startsWith('v') ? lastTag.slice(1) : lastTag
const newVersion = incrementVersion(lastVersion, incrementType[0])
const data = fs.readFileSync(installFile, 'utf8')
const regex = /(wget https:\/\/github\.com\/flawiddsouza\/general-tester\/releases\/download\/)v[0-9]+(\.[0-9]+)*\/(general-tester-linux\.gz)/
const replacement = `$1v${newVersion}/$3`
const updatedData = data.replace(regex, replacement)
fs.writeFileSync(installFile, updatedData, 'utf8')
execSync(`git add ${installFile}`)
execSync(`git commit -m "chore: bump version"`)
execSync(`git tag v${newVersion}`)
console.log(`Version number updated to v${newVersion}`)
} catch (err) {
console.error(err)
process.exit(1)
}