UNPKG

@chonglou_/quick-app-cli

Version:

前端基础建设模版脚手架,快速搭建PC、Micro(qiankun)、App、微信小程序等项目

221 lines (213 loc) 7.04 kB
#!/usr/bin/env node const { commander, logSymbols, chalk } = require('../lib/utils/dependencies') const { askCommand } = require('../lib/utils/index') //从package.json中读取脚手架版本号 let version = require('../package.json').version commander.version(version, '-v, --version') //检查版本升级 const upgradeCli = require('../lib/upgrade') commander .command('update') .description('Check the qb-app-cli version.') .action(() => { upgradeCli() }) //初始化项目 const { isCheckProjectNameRepetition, cloneProjectTemplate } = require('../lib/init') commander .name('qb-app-cli') .usage('<commands> [options]') .command('create <project_name>') .description('Create a app project.') .action((projectName) => { isCheckProjectNameRepetition(projectName).then(() => { askCommand([ { name: 'platform', type: 'list', message: 'Please pick a platform project:', choices: [ { name: 'Web App', value: 'pc' }, { name: 'Mirco App', value: 'micro' }, { name: 'H5 App', value: 'app' }, { name: 'Wechat App', value: 'weapp' } ] } ]) .then(async (answer) => { answer = Object.assign(answer, { projectName }) if (answer.platform !== 'weapp') { let commandTask = [ { name: 'mode', type: 'list', message: 'Please pick a preset:', choices: [ { name: 'Default ([Vue 2] babel, eslint)', value: 'vue2' }, { name: 'Load all configurations', value: 'all' }, { name: 'Manually select features', value: 'custom' } ] } ] if (answer.platform === 'micro') { //微前端插入路由类型 commandTask.splice(0, 0, { name: 'routerMode', type: 'list', message: 'Please pick a router mode:', choices: [ { name: 'Qiankun and Router Hash Mode', value: 'qiankun/hash' }, { name: 'Qiankun and Router History Mode', value: 'qiankun/history' } ] }) } let preset = await askCommand(commandTask) if (preset.mode === 'custom') { let tasks = [] if (answer.platform === 'pc' || answer.platform === 'micro') { //是否引入qb-ui,并使用自定义组件和外层模版框架 tasks.push({ name: 'antd', type: 'confirm', message: 'Whether to introduce Ant-Design-Vue and use custom components and outer frame templates?' }) } //app项目 else if (answer.platform === 'app') { //是否引入vant,并使用自定义组件和外层模版框架 tasks.push({ name: 'vant', type: 'confirm', message: 'Whether to introduce Vant and use custom components and outer frame templates?' }) } let dependencies = await askCommand(tasks) preset = Object.assign(preset, dependencies) } return Promise.resolve(Object.assign(answer, preset)) } else { let dependencies = await askCommand([ //是否引入vant/weapp,并使用自定义组件和外层模版框架 { name: 'vant', type: 'confirm', message: 'Whether to introduce Vant/weapp and use custom components and outer frame templates?' } ]) return Promise.resolve(Object.assign(answer, dependencies)) } }) .then(async (answer) => { let installed = await askCommand([ { name: 'installed', type: 'confirm', message: `Should we run 'cnpm install' for you after the project has been created? ` } ]) return Promise.resolve(Object.assign(answer, installed)) }) .then((answer) => { //仓库地址 if ( answer.platform === 'pc' || answer.platform === 'micro' || answer.platform === 'app' ) { switch (answer.mode) { case 'vue2': answer.cloneOptions = [ '-b', `${ answer.platform + (answer.routerMode ? '/' + answer.routerMode : '') }/index` ] break case 'all': if (answer.platform === 'pc' || answer.platform === 'micro') answer.antd = true if (answer.platform === 'app') answer.vant = true break case 'custom': break } //依赖 let temp = [] if (answer.antd) temp.push('antd') if (answer.vant) temp.push('vant') if (temp.length) { answer.cloneOptions = [ '-b', `${ answer.platform + (answer.routerMode ? '/' + answer.routerMode : '') }/${temp.join('-')}` ] } answer.fileUrl = 'https://gitee.com/open-source-tools/vue-app-template.git' } else if (answer.platform === 'weapp') { //依赖 let temp = [] if (answer.vant) temp.push('vant') if (temp.length) { answer.cloneOptions = ['-b', `feature/${temp.join('-')}`] } answer.fileUrl = 'https://gitee.com/open-source-tools/weapp-template.git' } return cloneProjectTemplate(answer) }) .catch((error) => { if (error.isTtyError) { console.log( logSymbols.error, chalk.red( "Prompt couldn't be rendered in the current environment." ) ) } else { console.log(logSymbols.error, chalk.red(error)) } }) .finally(() => { console.log(logSymbols.success) }) }) }) //解析命令行参数 commander.parse(process.argv)