-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelease.js
77 lines (63 loc) · 1.8 KB
/
release.js
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
var exec = require("child_process").exec
var chalk = require("chalk")
var npmInfo = require("./package")
var readline = require("readline")
var fs = require("fs")
// main
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
console.log(`Current version: ${npmInfo.version}`)
let version = ""
ask("New version: ")
.then(responseVersion => {
version = responseVersion.trim()
return ask(`Confirm: ${version}`)
})
.then(() => execute(`npm test`))
.then(() => {
const newPackage = { ...npmInfo, version }
fs.writeFileSync("./package.json", JSON.stringify(newPackage, null, 2), { encoding: "utf8" })
return execute("git add package.json")
})
.then(assertGitClean)
.then(() => execute(`git commit -m v${version}`))
.then(() => execute(`git push origin master`))
.then(() => execute(`npm publish`))
.then(() => execute(`git tag -a v${version} -m "release v${version}"`))
.then(() => execute(`git push origin v${version}`))
.then(() => process.exit(0))
.catch(err => {
console.log(err)
process.exit(1)
})
// utils
function ask(question) {
return new Promise((resolve, reject) => {
rl.question(question, function(response) {
resolve(response)
})
})
}
function execute(command) {
console.log(chalk.magenta("execute: ") + `"${command}"`)
return new Promise((resolve, reject) => {
exec(command, function(error, stdout, stderr) {
if (error) {
console.log(chalk.red("Whoops!"))
reject(error)
return
}
console.log(chalk.green("ok! 👍"))
resolve(stdout)
}).stdout.pipe(process.stdout)
})
}
function assertGitClean() {
return execute("git diff --shortstat 2> /dev/null | tail -n1").then(out => {
if (out.trim() !== "") {
throw "Git isn't clean"
}
})
}