forked from mobxjs/mobx-react-lite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
publish.js
116 lines (96 loc) · 3.33 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
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
115
116
#!/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 path = require("path")
const shell = require("shelljs")
const fs = require("fs")
const prompts = require("prompts")
const semver = require("semver")
function run(command, options) {
const continueOnErrors = options && options.continueOnErrors
const ret = shell.exec(command, options)
if (!continueOnErrors && ret.code !== 0) {
exit(1)
}
return ret
}
function exit(code, msg) {
console.error(msg)
shell.exit(code)
}
function writeJSON(path, obj) {
return new Promise((resolve, reject) => {
fs.writeFile(path, JSON.stringify(obj, null, 2), err => {
if (err) {
reject(err)
} else {
resolve()
}
})
})
}
async function main() {
const status = run("git status --porcelain -uno", { silent: true })
if (status.stdout.length > 0) {
exit(0, "You have uncommited local changes. Aborting...")
}
if (!process.env.npm_config_registry || process.env.npm_config_registry.includes("yarn")) {
exit(0, "Make sure to do npm run release, Yarn is broken.")
}
const rootPath = path.resolve(__dirname)
const rootPkgFile = path.join(rootPath, "package.json")
const rootPkg = require(rootPkgFile)
const nextPatch = semver.inc(rootPkg.version, "patch")
const nextMinor = semver.inc(rootPkg.version, "minor")
const nextMajor = semver.inc(rootPkg.version, "major")
const resp = await prompts(
[
{
type: "select",
name: "action",
message: "What do you want to publish?",
choices: [
{ title: "Nothing, abort this", value: null },
{ title: `Patch version (${nextPatch})`, value: nextPatch },
{ title: `Minor version (${nextMinor})`, value: nextMinor },
{ title: `Major version (${nextMajor})`, value: nextMajor }
],
initial: null
}
],
{ onCancel: () => exit(0) }
)
if (resp.action === null) {
return
}
const nextVersion = resp.action
console.log("Starting build...")
run("npm run build")
console.log("Build is done")
await writeJSON(rootPkgFile, { ...rootPkg, version: nextVersion })
await executePublish()
run(`git add ${rootPkgFile}`)
run(`git commit --no-verify -m "Published version ${nextVersion}"`)
run("git push")
run(`git tag ${nextVersion}`)
run("git push --tags")
console.log("Pushed updated version & tags to git")
exit(0)
async function executePublish() {
// Check registry data
const npmInfoRet = run(`npm info ${rootPkg.name} --json`, {
continueOnErrors: true,
silent: true
})
const publishedPackageInfo = JSON.parse(npmInfoRet.stdout)
if (publishedPackageInfo.versions.includes(nextVersion)) {
throw new Error(`Version ${nextVersion} is already published in NPM`)
}
console.log(`Starting publish of ${nextVersion}...`)
run(`npm publish`)
console.log(`Published ${nextVersion} to NPM`)
}
}
main().catch(e => {
console.error(e)
})