UNPKG

uino-kiss-cli

Version:

uino-kiss-cli 用来初始化项目的方便工具

113 lines (101 loc) 2.56 kB
const log = console.log; const chalk = require('chalk'); const clear = require('clear'); const figlet = require('figlet'); const slog = require('single-line-log').stdout; const download = require('download-git-repo'); const { spawn } = require('child_process'); const inquirer = require('./inquirer'); const isWin32 = process.platform === 'win32'; const installPacks = require('./install-packs'); const initUI = require('./init-ui') const project = { options: {}, create(options) { this.options = options; if (options.ui === '不需要') options.ui = ''; global.dirPath = options.name; this.download(); }, download() { const options = this.options; const gitRepo = 'https://wangxinkai:a_mWzwZxqxxfZJhLhNQH@git.uinnova.com/kiss/kiss-cli-template.git#release'; // 定时输出 Downloading Installing 防止别人以为死机 let i = 0; let s = '文件下载中'; const timer = setInterval(() => { slog.clear(); slog(s + ['.', '..', '...'][i % 3]); i += 1; }, 500); download(`direct:${gitRepo}`, options.name, { clone: true }, (err) => { if (err) { log(err); return log(chalk.red('下载文件失败!')); } clearInterval(timer); slog('文件下载完成'); log(''); // test if (options.git) this.initGit(options.git); this.install() }); }, async install() { const options = this.options; installPacks.all(function(){ if(options.ui) { installPacks.ui(options.ui,function(){ initUI.register(options.ui, options.name) }) } }) }, initGit(url) { const coms = [['init'], ['remote', 'add', 'origin', url]]; this.runGit(coms); }, runGit(coms) { if (coms.length === 0) return; let cmd = coms.shift(); const dirPath = process.cwd() + `/${this.options.name}`; const software = isWin32 ? 'git.cmd' : 'git'; const npmInit = spawn(software, cmd, { cwd: dirPath, stdio: 'inherit', }); npmInit.on('close', (code) => { this.runGit(coms); }); }, }; const init = { run(dirName){ if (global.directoryExists(dirName)) { log(chalk.red('目录已存在')); process.exit(); } clear(); // 文字logo log( chalk.hex('#FF7A1E')( figlet.textSync('KISS CLI', { horizontalLayout: 'full', }) ) ); // 文字logo log('更多帮助,请访问: http://kiss.udolphin.com/'); log('\n\r'); const run = async () => { let options = await inquirer.askOptions(); options.name = dirName; project.create(options); }; run(); }, create(options){ project.create(options); } } module.exports = init