-
Notifications
You must be signed in to change notification settings - Fork 11
/
release.mjs
executable file
·74 lines (62 loc) · 2.09 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
#!/usr/bin/env zx
/*
* Script to release the seats.io php 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 latestReleaseTag = await fetchLatestReleasedVersionNumber()
await 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`
return JSON.parse(result).tagName
}
async function determineNextVersionNumber(previous) {
return semver.inc(previous, versionToBump)
}
async function getCurrentCommitHash() {
return (await $`git rev-parse HEAD`).stdout.trim()
}
async function getCommitHashOfTag(tag) {
return (await $`git rev-list -n 1 ${tag}`).stdout.trim()
}
async function assertChangesSinceRelease(releaseTag) {
let masterCommitHash = await getCurrentCommitHash()
let releaseCommitHash = await getCommitHashOfTag(releaseTag)
if(masterCommitHash === releaseCommitHash) {
throw new Error("No changes on master since release tagged " + releaseTag)
}
}
async function release() {
await assertChangesSinceRelease(latestReleaseTag)
const nextVersion = await determineNextVersionNumber(removeLeadingV(latestReleaseTag))
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
})
}