#!/usr/bin/env node
const path = require('path')
const exists = require('fs').existsSync
const program = require('commander')
const chalk = require('chalk')
const inquirer = require('inquirer')
const download = require('git-clone')
const clone = require('git-clone/promise')
const shell = require('shelljs')
const spawn = require('child_process').spawn
program.on('--help', () => {
{
console.log(' Examples:')
console.log()
console.log(chalk.gray(' # create a new project with oms-cli'))
console.log(chalk.greenBright(' $ oms create my-project'))
}
})
program
.command('create <app-name>')
.description('create a new project powered by oms-cli')
.option('-c, --clone', 'Use git clone when fetching remote repository')
.action((name, options) => {
if (!name) {
program.help()
} else {
const pkg = require('./package.json')
console.log(chalk.blueBright(`Oms CLI v${pkg.version}`))
handelPro(name)
}
})
function help() {
program.parse(process.argv)
if (program.args.length < 1) return program.help()
}
help()
function handelPro(name) {
const rawName = name
const to = path.resolve(rawName)
if (exists(to)) {
inquirer
.prompt([
{
type: 'confirm',
message: `项目名${rawName}已经存在,是否确认继续 ?`,
name: 'ok'
}
])
.then(answers => {
if (answers.ok) {
run(name)
}
})
} else {
run(name)
}
}
function run(name) {
let giturl = 'https://github.com/vuejs/vue-next-webpack-preview.git'
giturl = 'https://gitee.com/product_centerfront_end/burgeon-internationalization.git'
giturl = 'https://gitee.com/product_centerfront_end/front-standard-product.git'
var p1 = new Promise(function (resolve, reject) {
console.log()
console.log(chalk.yellow(`============ 项目${name}正在创建中...... =============`))
console.log()
clone(giturl, `./${name}`, { checkout: 'sit' })
.then(res => {
resolve()
shell.cd(name)
shell.rm('-rf', `${name}/.git`)
console.log(chalk.green('Project created successfully !'))
console.log()
installDependencies(name)
})
.catch(err => {
reject()
console.log(chalk.yellow(err))
return
})
})
}
async function installDependencies(params) {
const { ok } = await inquirer.prompt([
{
type: 'confirm',
message: '是否安装依赖并启动项目?',
name: 'ok'
}
])
if (ok) {
const runPro = spawn('npm', ['install'])
runPro.on('close', status => {
if (status == 0) {
shell.exec('npm start')
} else {
onError(new Error("'npm start' failed with status " + status))
}
})
} else {
console.log(
chalk.green(`
创建项目${name}成功
cd ${name} 进入项目
npm install
npm start
`)
)
}
}