forked from mobxjs/mobx-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
publish.js
executable file
·86 lines (74 loc) · 2.4 KB
/
publish.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
78
79
80
81
82
83
84
85
86
#!/usr/bin/env node
/* Publish.js, publish a new version of the npm package as found in the current directory */
/* Run this file from the root of the repository */
const shell = require("shelljs")
const fs = require("fs")
const readline = require("readline")
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
})
function run(command, options) {
const continueOnErrors = options && options.continueOnErrors
const ret = shell.exec(command, options)
if (!continueOnErrors && ret.code !== 0) {
shell.exit(1)
}
return ret
}
function exit(code, msg) {
console.error(msg)
shell.exit(code)
}
async function prompt(question, defaultValue) {
return new Promise((resolve) => {
rl.question(`${question} [${defaultValue}]: `, (answer) => {
answer = answer && answer.trim()
resolve(answer ? answer : defaultValue)
})
})
}
async function main() {
const pkg = JSON.parse(fs.readFileSync("package.json", "utf8"))
// Bump version number
let nrs = pkg.version.split(".")
nrs[2] = 1 + parseInt(nrs[2], 10)
const version = (pkg.version = await prompt(
"Please specify the new package version of '" + pkg.name + "' (Ctrl^C to abort)",
nrs.join(".")
))
if (!version.match(/^\d+\.\d+\.\d+$/)) {
exit(1, "Invalid semantic version: " + version)
}
// Check registry data
const npmInfoRet = run(`npm info ${pkg.name} --json`, {
continueOnErrors: true,
silent: true,
})
if (npmInfoRet.code === 0) {
//package is registered in npm?
var publishedPackageInfo = JSON.parse(npmInfoRet.stdout)
if (
publishedPackageInfo.versions == version ||
publishedPackageInfo.versions.includes(version)
) {
exit(2, "Version " + pkg.version + " is already published to npm")
}
fs.writeFileSync("package.json", JSON.stringify(pkg, null, 2), "utf8")
// build
run("npm run prepublishOnly")
// Finally, commit and publish!
run("npm publish")
run(`git commit -am "Published version ${version}"`)
run(`git tag ${version}`)
run("git push")
run("git push --tags")
console.log("Published!")
exit(0)
} else {
exit(1, pkg.name + " is not an existing npm package")
}
}
main().catch((e) => {
throw e
})