-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrelease.ts
114 lines (94 loc) · 3.31 KB
/
release.ts
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
104
105
106
107
108
109
110
111
112
113
114
import * as fs from 'fs'
import * as path from 'path'
import * as os from 'os'
import { promisify } from 'util'
import { exec } from 'child_process'
import consola from 'consola'
import semver from 'semver'
const log = consola
const readFile = promisify(fs.readFile)
const execCommand = async (command: string): Promise<string> =>
new Promise((resolve, reject) => {
exec(command, (error, stdout, stderr) => {
if (error) {
log.warn(error.message)
log.warn(stderr)
log.warn(stdout)
return reject(new Error(`${error.message}\n${stderr}\n${stdout}`))
}
return resolve(`${stdout}`.trim())
})
})
const release = async (): Promise<void> => {
log.info('preparing release')
const pkgContents = await readFile(path.resolve('./package.json'))
const pkg = JSON.parse(pkgContents.toString())
const pkgVersion = semver.parse(pkg.version)
if (!pkgVersion) {
throw Error(`cannot determine package version`)
}
const tagMatch = `v${pkgVersion.major}.${pkgVersion.minor}.*`
const branch = await execCommand(`git rev-parse --abbrev-ref HEAD`)
log.info(`current branch ${branch}`)
log.info(`fetching remote`)
await execCommand(`git fetch --tags`)
log.info(`determine latest release version by tags`)
const tags = (await execCommand(`git tag --list ${tagMatch}`))
.split(os.EOL)
.filter((tag) => tag.length)
.map((tag) => tag.slice(1))
.sort(semver.compare)
const latestVersion = semver.parse(
tags.length
? tags[tags.length - 1]
: `${pkgVersion.major}.${pkgVersion.minor}.0`
)
if (!latestVersion) {
throw Error(`unable to determine latest version`)
}
const nextVersion = `${latestVersion.major}.${latestVersion.minor}.${
latestVersion.patch + 1
}`
log.info(
`determined latest version: ${latestVersion.version}, next version ${nextVersion}`
)
const releaseTag = `v${nextVersion}`
const referenceTag = `v${latestVersion.major}`
// eslint-disable-next-line no-console
console.log(`::set-output name=RELEASE_TAG::${releaseTag}`)
log.info(`stage build artifacts`)
await execCommand(`yarn add-dist`)
log.info(`committing build artifacts`)
await execCommand(`git config user.name 'Resolve Bot'`)
await execCommand(`git config user.email '[email protected]'`)
let skipRelease = true
try {
await execCommand(`git commit -nam "Build artifacts"`)
await execCommand(`git tag ${releaseTag}`)
await execCommand(`git tag -f ${referenceTag}`)
await execCommand(`git push --atomic origin ${branch} ${releaseTag}`)
await execCommand(`git push origin ${referenceTag} --force`)
skipRelease = false
} catch (error) {
if (!error.message.includes('nothing to commit, working tree clean')) {
throw error
}
log.info(`no changes detected`)
}
if (skipRelease) {
log.info(`skipping release`)
// eslint-disable-next-line no-console
console.log(`::set-output name=SKIP_RELEASE::true`)
} else {
const commitSHA = await execCommand(`git rev-parse HEAD`)
log.info(`current commit SHA: ${commitSHA}`)
// eslint-disable-next-line no-console
console.log(`::set-output name=SKIP_RELEASE::false`)
// eslint-disable-next-line no-console
console.log(`::set-output name=COMMIT_SHA::${commitSHA}`)
}
}
release().catch((error) => {
log.error(error)
process.exit(1)
})