UNPKG

kingsun-cli

Version:

前端通用模板脚手架工具

145 lines (136 loc) 4.61 kB
const fs = require('fs-extra') const inquirer = require('inquirer') // 实现交互式命令行 const download = require('download-git-repo') const ora = require('ora') const chalk = require('chalk') const logSymbols = require('log-symbols') const customChoices = [ { name: 'H5通用模板', value: 1 }, { name: 'PC通用模板', value: 2 }, { name: '小程序通用模板', value: 3 } ] const askList = [ { type: 'input', name: 'desc', message: '请输入你的项目描述' }, { type: 'list', name: 'custom', message: '请选择你要创建的模板', choices: customChoices } ] /** * 修改模板文件的参数 * @param {String} projectName 项目名称 * @param {Object} params 输入的参数 */ async function changePkg(projectName, params) { const isExist = await fs.pathExists(`${projectName}/package.json`) let pkg = isExist ? await fs.readJSON(`${projectName}/package.json`, {}) : {} pkg = Object.assign({}, pkg, params) await fs.writeJSON(`${projectName}/package.json`, pkg) } function startDownload(downLoadUrl, projectName, projectDesc) { // 在下载前提示 const spinner = ora('正在创建项目...').start() // const downLoadUrl = `http://192.168.11.6:10080:KS0617_PAJX/Pajxgypt#master` download(downLoadUrl, projectName, { clone: true }, err => { if (err) { spinner.fail() console.log(logSymbols.error, chalk.red('项目创建失败,失败原因:' + err)) return } spinner.succeed() changePkg(projectName, { name: projectName, description: projectDesc }) console.log(logSymbols.success, chalk.green('项目创建成功,建议执行')) console.log(logSymbols.info, '进入目录:' + chalk.green('cd ' + projectName)) console.log(logSymbols.info, '安装依赖:' + chalk.green('npm install')) }) } /** * 处理配置结果 * @param {Object} answers 输入配置结果 */ function handleAnswers(answers) { // 处理结果 const curSelect = customChoices.find(item => answers.custom === item.value) if (parseInt(curSelect.value) === 1) { // 选择h5模板 inquirer.prompt([ { type: 'confirm', name: 'isVant', message: '是否自动集成vant移动端组件库?' } ]).then(async (vantAnswers) => { let downLoadUrl = `https://github.com:fluoos/ks-h5-template#master` if (vantAnswers.isVant) { downLoadUrl = `https://github.com:fluoos/ks-vant-template#master` } startDownload(downLoadUrl, answers.name, answers.desc) }) } else if (parseInt(curSelect.value) === 2) { inquirer.prompt([ { type: 'confirm', name: 'isEle', message: '是否自动集成elementUI组件库?' } ]).then(async (eleAnswers) => { let downLoadUrl = `https://github.com:fluoos/ks-pc-template#master` if (eleAnswers.isEle) { downLoadUrl = `https://github.com:fluoos/ks-element-template#master` } startDownload(downLoadUrl, answers.name, answers.desc) }) } else { console.log(logSymbols.error, chalk.red('模板开发中,构建失败')) } } /** * 询问配置的内容 * @param {String} projectName 项目名称 */ async function askConfig(projectName) { const isExist = await fs.pathExists(projectName) if (isExist) { console.log(logSymbols.error, chalk.red('创建的项目已存在!')) return } // 设置问题 inquirer.prompt(askList).then(answers => { answers.name = projectName handleAnswers(answers) }) } async function init(projectName) { if (!projectName) { inquirer.prompt([ { type: 'input', name: 'name', message: '请输入你的项目名称' } ]).then(async (answers) => { if (!answers.name) { init('') return } askConfig(answers.name) }) return } askConfig(projectName) } module.exports = (...args) => { return init(...args).catch(err => { error(err) process.exit(1) }) }