-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathpublish.js
92 lines (80 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
87
88
89
90
91
92
/* eslint-disable @typescript-eslint/no-require-imports */
const inquirer = require('inquirer')
const semver = require('semver')
const package = require('../package.json')
const { exec } = require('child_process')
async function retry() {
const confirm = await inquirer.prompt([
{
type: 'confirm',
name: 'try',
message: '是否重新尝试发布?',
default: false
}
])
if (confirm.try) {
publish()
}
}
async function publish() {
const config = await inquirer.prompt([
{
type: 'list',
name: 'version',
message: '请选择本次发布的版本类型:',
choices: ['Prerelease', 'Patch', 'Minor', 'Major'],
filter: function(val) {
return val.toLowerCase()
}
},
{
type: 'confirm',
name: 'isLatest',
message: '是正式版发布吗?',
default: false
}
])
if (config.isLatest && config.version === 'prerelease') {
console.log('PreRelease版本不能发布正式版!')
setImmediate(retry)
return
}
if (!config.isLatest && config.version !== 'prerelease') {
config.version = 'pre' + config.version
}
let newVersion = semver.inc(package.version, config.version)
const publishCommand =
'npm publish' + (config.isLatest ? '' : ' --tag beta')
const confirmMessage = `请确认核对信息:
==========发布信息==========
当前版本:${package.version}
发布版本:${newVersion}
执行命令:npm version ${newVersion} && ${publishCommand}
==========发布信息==========
以上信息确认无误?`
const confirm = await inquirer.prompt([
{
type: 'confirm',
name: 'result',
message: confirmMessage,
default: false
}
])
if (confirm.result) {
console.log('发布中...')
exec(
`npm version ${newVersion} && ${publishCommand}`,
(err, stdout, stderr) => {
if (err) {
console.error(err)
}
stdout && console.log(stdout)
stderr && console.error(stderr)
if (stdout && !stderr) {
console.log('发布成功!')
}
}
)
}
}
publish()