-
Notifications
You must be signed in to change notification settings - Fork 1
/
release.mjs
executable file
·92 lines (78 loc) · 2.59 KB
/
release.mjs
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
87
88
89
90
91
92
#!/usr/bin/env zx
/*
* Script to release the seats.io java lib.
* - changes the version number in README.md
* - changes the version number in build.gradle
* - creates the release in Gihub (using gh cli)
*
*
* Prerequisites:
* - zx installed (https://github.com/google/zx)
* - gh cli installed (https://cli.github.com/)
*
* Usage:
* yarn zx ./release.mjs -v major/minor -n "release notes"
* */
// don't output the commands themselves
$.verbose = false
const semver = require('semver')
const versionToBump = getVersionToBump()
const latestVersion = await fetchLatestReleasedVersionNumber()
const nextVersion = await determineNextVersionNumber(latestVersion)
await pullLastVersion()
.then(bumpVersionInFiles)
.then(commitAndPush)
.then(release)
function getVersionToBump() {
if (!argv.v || !(argv.v === 'minor' || argv.v === 'major')) {
throw new Error ("Please specify -v major/minor")
}
return argv.v
}
function removeLeadingV(tagName) {
if (tagName.startsWith('v')) {
return tagName.substring(1)
}
return tagName
}
async function fetchLatestReleasedVersionNumber() {
let result = await $`gh release view --json tagName`
let tagName = JSON.parse(result).tagName
return removeLeadingV(tagName)
}
async function determineNextVersionNumber(previous) {
return semver.inc(previous, versionToBump)
}
async function bumpVersionInFiles() {
await replaceInFile("./projects/seatsio-angular/package.json", `"version": "${latestVersion}",`, `"version": "${nextVersion}",`)
}
async function replaceInFile(filename, latestVersion, nextVersion) {
return await fs.readFile(filename, 'utf8')
.then(text => {
if (text.indexOf(latestVersion) < 0) {
throw new Error('Not the correct version. Could not find ' + latestVersion + ' in ' + filename)
}
return text
})
.then(text => text.replace(latestVersion, nextVersion))
.then(text => fs.writeFileSync(filename, text))
.then(() => gitAdd(filename))
}
async function pullLastVersion() {
await $`git checkout master`
await $`git pull origin master`
}
async function gitAdd(filename) {
return await $`git add ${filename}`
}
async function commitAndPush() {
await $`git commit -m "version bump"`
await $`git push origin master`
}
async function release() {
const newTag = 'v' + nextVersion
return await $`gh release create ${newTag} --generate-notes`.catch(error => {
console.error('something went wrong while creating the release. Please revert the version change!')
throw error
})
}