-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathupdate.view.js
70 lines (69 loc) · 2.08 KB
/
update.view.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
import inquirer from 'inquirer'
import { checkUpdate } from '#common/utils/update/update.js'
import { currentEnv } from '#common/utils/etc/checkEnv.js'
import { updateByEnv } from '#common/utils/update/updateByEnv.js'
import { logger } from '#common/utils/logger/logger.js'
export async function easyUpdateView() {
// 1. 询问当前的环境是啥 (自动检测一次)
// 检测到的当前环境
const env = currentEnv()
const envQuestion = {
type: 'list',
name: 'choseEnv',
message: `自动检测到的环境为[ ${env} ],如果不是,请进行选择,如是,请按下回车确认.`,
choices: ['cli', 'project'],
default: env
}
const { choseEnv } = await inquirer.prompt(envQuestion, null)
// 2. 检测版本更新
const {
localVersion,
npmVersion,
githubVersion,
isCliUpdate,
isGithubUpdate
} = await checkUpdate()
logger.info(
`当前版本:[ ${localVersion} ] npm包最新版本:[ ${npmVersion} ] github版本:[ ${githubVersion} ]`
)
let isUpdate = false
let version = '未知'
switch (choseEnv) {
case 'project':
isUpdate = isGithubUpdate
version = githubVersion
break
case 'cli':
isUpdate = isCliUpdate
version = npmVersion
break
default:
logger.warn('未知环境:', choseEnv)
process.exit(0)
break
}
// 3. 询问是否更新
if (isUpdate) {
const checkQuestion = {
type: 'confirm',
name: 'willUpdate',
message: `检测到[ ${env} ]可更新版本[ ${version} ],是否进行更新?`
}
const { willUpdate } = await inquirer.prompt(checkQuestion, null)
if (willUpdate) {
// 4.1 选择更新
logger.info('开始更新...')
await updateByEnv(env)
logger.info('更新完成~祝你使用愉快~')
} else {
// 4.2 取消更新
logger.info(
'你选择跳过此次更新,如果想要进行更新,随时可以使用参数 -u 进行更新检测!祝你使用愉快~'
)
}
process.exit(0)
} else {
logger.info('当前已是最新版本!祝你使用愉快~')
process.exit(0)
}
}