'use strict'
const exec = require('child_process').exec
const config = require('../templates')
const co = require('co')
const prompt = require('co-prompt')
const chalk = require('chalk')
const suffixWithDoc = [
'.browserslistrc',
'.editorconfig',
'.',
'.gitignore',
]
= () => {
const { tpl } = config;
const keys = (tpl);
(key => {
const { url, branch } = tpl[key];
('------------------------------------------------------------------------------');
(`| Name Branch url |`);
(`| ${key} ${branch} ${(('/') + 1)} |`);
('------------------------------------------------------------------------------');
})
co(function* () {
// 处理用户输入
let tplName = yield prompt('Template name: ');
if (![tplName]) {
(('\n × Template does not exit!'))
()
}
// 判断用户的输入
const yesArr = ['YES', 'Y'];
const noArr = ['NO', 'N'];
let isRootPath, isRoot;
while (true) {
// 是否下载在执行根目录
isRootPath = yield prompt('Downloading template in the excute root dictionay?(Y/N):');
isRootPath = ();
if ((isRootPath) || isRootPath === '') {
isRoot = true;
break;
} else if ((isRootPath)) {
isRoot = false;
break;
}
}
let projectName = `${tplName}${()}`;
if (!isRoot) {
projectName = yield prompt('Project name: ')
}
const { url: gitUrl, branch } = [tplName]
// git命令,远程拉取项目并自定义项目名
let cmdStr = `git clone ${gitUrl} ${projectName}`
(('\n Start generating...'))
exec(cmdStr, error => {
if (error) {
(error)
()
}
// 切换到最新的分支并且删除.git
const checkoutCmd = `cd ${projectName} && git checkout ${branch} && rm -rf .git && cd ..`
const exitCheckOutCMD = (callback) => {
exec(checkoutCmd, err => {
if (err) {
(err);
();
}
callback();
})
}
const success = () => {
(('\n √ Generation completed!'))
(`\n you can run your project by npm start now! \n`)
();
}
exitCheckOutCMD(() => {
if (isRoot) {
// 移动项目文件夹下所有文件至执行根目录
const delCMD = `mv -f ${()}/${projectName}/* ${()} && rm -rf ${projectName} && npm install`
// 执行挪动文件夹操作
exec(delCMD, (err) => {
if (err) {
(err)
}
success();
})
} else {
success();
}
})
})
})
}