#!/usr/bin/env node const path = require('path') const fs = require('fs-extra') const program = require('commander') const chalk = require('chalk') const { shouldUseCnpm } = require('../dist/util') const ora = require('ora') const inquirer = require('inquirer') const exec = require('child_process').exec const { PROJECT_CONFIG } = require('../dist/util/constants') const { createCore } = require('../templates/default/index') const projectConfPath = path.join(process.cwd(), PROJECT_CONFIG) // 这里没有使用 command 的形式:syberh-update-self program.parse(process.argv) const args = program.args if (args.length === 1) { switch (args[0]) { case 'self': { updateSelf() break } case 'project': { updateProject() break } default: info() } } else { info() } function info () { console.log(chalk.red('命令错误:')) console.log(`${chalk.green('syberh update self')} 更新 Syberh 开发工具 cli 到最新版本`) console.log(`${chalk.green('syberh update project')} 更新项目所有 Syberh 相关依赖到最新版本...`) } function updateSelf () { let command if (shouldUseCnpm()) { command = 'cnpm i -g @syberos/cli@latest' } else { command = 'npm i -g @syberos/cli@latest' } const child = exec(command) const spinner = ora('即将将 Syberh开发工具 cli 更新到最新版本...').start() child.stdout.on('data', function (data) { console.log(data) spinner.stop() }) child.stderr.on('data', function (data) { console.log(data) spinner.stop() }) } function updateProject () { if (!fs.existsSync(projectConfPath)) { console.log(chalk.red(`找不到项目配置文件${PROJECT_CONFIG},请确定当前目录是Syberh项目根目录!`)) process.exit(1) } // tslint:disable-next-line: no-floating-promises inquirer.prompt([{ type: 'confirm', message: '更新项目依赖会修改platforms目录下的文件,是否更新?', name: 'agree', prefix: chalk.yellow('警告!'), default: false }]).then(answers => { if (answers.agree) { createCore() } }) }