1 | #!/usr/bin/env node
|
2 | const path = require('path')
|
3 | const fs = require('fs-extra')
|
4 | const program = require('commander')
|
5 | const chalk = require('chalk')
|
6 | const { shouldUseCnpm } = require('../dist/util')
|
7 | const ora = require('ora')
|
8 | const inquirer = require('inquirer')
|
9 | const exec = require('child_process').exec
|
10 | const { PROJECT_CONFIG } = require('../dist/util/constants')
|
11 | const { createCore } = require('../templates/default/index')
|
12 | const projectConfPath = path.join(process.cwd(), PROJECT_CONFIG)
|
13 |
|
14 | // 这里没有使用 command 的形式:syberh-update-self
|
15 | program.parse(process.argv)
|
16 |
|
17 | const args = program.args
|
18 |
|
19 | if (args.length === 1) {
|
20 | switch (args[0]) {
|
21 | case 'self': {
|
22 | updateSelf()
|
23 | break
|
24 | }
|
25 | case 'project': {
|
26 | updateProject()
|
27 | break
|
28 | }
|
29 | default:
|
30 | info()
|
31 | }
|
32 | } else {
|
33 | info()
|
34 | }
|
35 |
|
36 | function info () {
|
37 | console.log(chalk.red('命令错误:'))
|
38 | console.log(`${chalk.green('syberh update self')} 更新 Syberh 开发工具 cli 到最新版本`)
|
39 | console.log(`${chalk.green('syberh update project')} 更新项目所有 Syberh 相关依赖到最新版本...`)
|
40 | }
|
41 |
|
42 | function updateSelf () {
|
43 | let command
|
44 | if (shouldUseCnpm()) {
|
45 | command = 'cnpm i -g @syberos/cli@latest'
|
46 | } else {
|
47 | command = 'npm i -g @syberos/cli@latest'
|
48 | }
|
49 |
|
50 | const child = exec(command)
|
51 |
|
52 | const spinner = ora('即将将 Syberh开发工具 cli 更新到最新版本...').start()
|
53 |
|
54 | child.stdout.on('data', function (data) {
|
55 | console.log(data)
|
56 | spinner.stop()
|
57 | })
|
58 | child.stderr.on('data', function (data) {
|
59 | console.log(data)
|
60 | spinner.stop()
|
61 | })
|
62 | }
|
63 |
|
64 | function updateProject () {
|
65 | if (!fs.existsSync(projectConfPath)) {
|
66 | console.log(chalk.red(`找不到项目配置文件${PROJECT_CONFIG},请确定当前目录是Syberh项目根目录!`))
|
67 | process.exit(1)
|
68 | }
|
69 |
|
70 | // tslint:disable-next-line: no-floating-promises
|
71 | inquirer.prompt([{
|
72 | type: 'confirm',
|
73 | message: '更新项目依赖会修改platforms目录下的文件,是否更新?',
|
74 | name: 'agree',
|
75 | prefix: chalk.yellow('警告!'),
|
76 | default: false
|
77 | }]).then(answers => {
|
78 | if (answers.agree) {
|
79 | createCore()
|
80 | }
|
81 | })
|
82 | }
|