169 lines
4.5 KiB
JavaScript
169 lines
4.5 KiB
JavaScript
#! /usr/bin/env node
|
||
|
||
const program = require('commander');
|
||
const inquirer = require('inquirer');
|
||
const chalk = require('chalk');
|
||
const util = require('./task/util');
|
||
const task = require('./task/task');
|
||
const package = require("./package.json");
|
||
|
||
program
|
||
.version(package.version)
|
||
.command('init')
|
||
.alias('i')
|
||
.option('-t, --type [template]', '模版类型(react)')
|
||
.option('-n, --name [pathName]', '构建路径名称')
|
||
.description('初始化一个项目')
|
||
.action(option => {
|
||
let config = Object.assign({
|
||
tpl: 0,
|
||
name: null
|
||
}, option)
|
||
|
||
//console.log(config)
|
||
|
||
let promps = [];
|
||
|
||
if(!config.tpl) {
|
||
promps.push({
|
||
type: 'list',
|
||
name: 'tpl',
|
||
message: '请选择需要创建的模板:',
|
||
choices: [
|
||
{
|
||
name: 'react',
|
||
value: 'react'
|
||
},
|
||
{
|
||
name: 'tpl',
|
||
value: 'tpl'
|
||
}
|
||
]
|
||
})
|
||
}
|
||
|
||
if(!config.name) {
|
||
promps.push({
|
||
type: 'input',
|
||
name: 'name',
|
||
message: '请输入需要创建的路径名:',
|
||
default: util.randomActName()
|
||
})
|
||
}
|
||
|
||
inquirer.prompt(promps).then(function (answers) {
|
||
//console.log('\n',answers)
|
||
option = Object.assign(answers,option);
|
||
//console.log(option);
|
||
|
||
let paths = util.paths(option.name, __dirname);
|
||
task.init(option.tpl, paths, option._name);
|
||
}).catch((err) => {
|
||
console.log(err);
|
||
})
|
||
})
|
||
|
||
program
|
||
.command('dev')
|
||
.alias('d')
|
||
.option('-n, --name [pathName]', '开发路径名称')
|
||
.description('开发一个已存在的项目')
|
||
.action(option => {
|
||
let config = Object.assign({
|
||
name: null
|
||
}, option)
|
||
|
||
//console.log(config)
|
||
|
||
let promps = [];
|
||
|
||
if(!config.name) {
|
||
promps.push({
|
||
type: 'input',
|
||
name: 'name',
|
||
message: '请输入需要开发的路径名:',
|
||
validate: function (input){
|
||
if(!input) {
|
||
return '不能为空'
|
||
}
|
||
|
||
return true;
|
||
}
|
||
})
|
||
}
|
||
|
||
inquirer.prompt(promps).then(function (answers) {
|
||
//console.log('\n',answers)
|
||
option = Object.assign(answers,option);
|
||
//console.log(option);
|
||
let paths = util.paths(option.name, __dirname);
|
||
|
||
let exi = util.pathExists(option.name, __dirname);
|
||
exi.then(result => {
|
||
//console.log(result);
|
||
if(!result) {
|
||
console.log(chalk.red('没有该目录:') + chalk.bold.red(paths.targetPath) + chalk.red(',请重新输入'));
|
||
return;
|
||
}
|
||
task.dev(paths, option._name);
|
||
}).catch(err => {
|
||
console.log(err);
|
||
});
|
||
|
||
}).catch((err) => {
|
||
console.log(err);
|
||
})
|
||
});
|
||
|
||
|
||
program
|
||
.command('pack')
|
||
.alias('p')
|
||
.description('打包构建一个已存在的项目')
|
||
.option('-n, --name [pathName]', '打包构建路径名称')
|
||
.action(option => {
|
||
let config = Object.assign({
|
||
name: null
|
||
}, option)
|
||
|
||
//console.log(config)
|
||
|
||
let promps = [];
|
||
|
||
if(!config.name) {
|
||
promps.push({
|
||
type: 'input',
|
||
name: 'name',
|
||
message: '请输入需要打包构建的路径名:',
|
||
validate: function (input){
|
||
if(!input) {
|
||
return '不能为空'
|
||
}
|
||
return true
|
||
}
|
||
})
|
||
}
|
||
|
||
inquirer.prompt(promps).then(function (answers) {
|
||
//console.log('\n',answers)
|
||
option = Object.assign(answers,option);
|
||
//console.log(option.name);
|
||
let paths = util.paths(option.name, __dirname);
|
||
|
||
let exi = util.pathExists(option.name, __dirname);
|
||
exi.then(result => {
|
||
//console.log(result);
|
||
if(!result) {
|
||
console.log(chalk.red('没有该目录:') + chalk.bold.red(paths.targetPath) + chalk.red(',请重新输入'));
|
||
return;
|
||
}
|
||
task.pack(paths, option._name);
|
||
}).catch(err => {
|
||
console.log(err);
|
||
});
|
||
}).catch((err) => {
|
||
console.log(err);
|
||
})
|
||
});
|
||
|
||
program.parse(process.argv); |