-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelease.ts
48 lines (42 loc) · 1.5 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
import {exec, execSync} from 'child_process'
import * as fs from 'fs'
const config = {
devBranch: 'develop',
mainBranch: 'master',
}
const run = (cl: string) => {
return new Promise((resolve, reject) => {
const _ = exec(cl)
_.stdout?.on('data', console.log)
_.stderr?.on('data', console.error)
_.on('exit', code => {
if (code === 0) {
resolve(code)
} else {
console.log(`${cl} exited with ${code}`)
reject(code)
}
})
})
}
const gitWorkspaceIsEmpty = () => execSync(`git status --porcelain`).toString() === ''
const newversion = process.argv[2] ?? 'patch' as 'patch' | 'minor' | 'major'
const getPackageVersion = () => JSON.parse(fs.readFileSync('package.json', 'utf8')).version
const isOnMainBranch = () => new RegExp(`${config.devBranch}\s*\n*`).test(execSync('git branch --show-current').toString())
;(async () => {
if (!isOnMainBranch()) {
console.error(`You must be on branch ${config.devBranch} to publish.`)
} else if (!gitWorkspaceIsEmpty()) {
console.error(`Your git status must be clean before to publish.`)
} else {
await run(`npm version ${newversion}`)
await run(`npm publish`)
// await run(`git commit -m "Release ${getPackageVersion()}"`)
await run(`git push`)
await run(`git checkout ${config.mainBranch}`)
await run(`git merge ${config.devBranch}`)
await run(`git push`)
await run(`git checkout ${config.devBranch}`)
console.log(`Successfully published verison ${getPackageVersion()} !`)
}
})()