UNPKG

2.14 kBPlain TextView Raw
1#!/usr/bin/env node
2const path = require('path')
3const fs = require('fs-extra')
4const program = require('commander')
5const chalk = require('chalk')
6const { shouldUseCnpm } = require('../dist/util')
7const ora = require('ora')
8const inquirer = require('inquirer')
9const exec = require('child_process').exec
10const { PROJECT_CONFIG } = require('../dist/util/constants')
11const { createCore } = require('../templates/default/index')
12const projectConfPath = path.join(process.cwd(), PROJECT_CONFIG)
13
14// 这里没有使用 command 的形式:syberh-update-self
15program.parse(process.argv)
16
17const args = program.args
18
19if (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
36function 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
42function 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
64function 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}