-
Notifications
You must be signed in to change notification settings - Fork 4
/
release.mjs
executable file
·103 lines (87 loc) · 2.99 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
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env zx
/*
* Script to release the seats.io python 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()
const latestVersion = removeLeadingV(latestReleaseTag)
const nextVersion = await determineNextVersionNumber(latestVersion)
await assertChangesSinceRelease(latestReleaseTag)
await bumpVersionInFiles()
await commitAndPush()
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 bumpVersionInFiles() {
await replaceInFile("setup.py", `version='v${latestVersion}'`, `version='v${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 gitAdd(filename) {
return await $`git add ${filename}`
}
async function commitAndPush() {
await $`git commit -m "version bump"`
await $`git push origin master`
}
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() {
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
})
}