quick-create-app-cli
Version:
前端基础建设模版脚手架,快速搭建PC、HybridApp、微信小程序、微前端等项目
191 lines (180 loc) • 6.1 kB
JavaScript
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 create-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 hybrid 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: 'H5 App',
value: 'app'
},
{
name: 'Wechat App',
value: 'wechat'
}
]
}
])
.then(async (answer) => {
answer = Object.assign(answer, { projectName })
if (answer.platform !== 'wechat') {
let preset = await askCommand([
{
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 (preset.mode === 'custom') {
let tasks = [
//是否引入vuex,并自动关联本地缓存
{
name: 'vuex',
type: 'confirm',
message:
'Whether to introduce Vuex and automatically associate local caches?'
},
//是否引入axios,并使用自定义网络工具库
{
name: 'axios',
type: 'confirm',
message:
'Whether to introduce Axios and use a custom network tool library?'
}
]
//pc项目和微前端项目
if (answer.platform === 'pc') {
//是否引入qb-ui,并使用自定义组件和外层模版框架
tasks.push({
name: 'qbui',
type: 'confirm',
message:
'Whether to introduce QU-UI 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))
}
return Promise.resolve(answer)
})
.then(async (answer) => {
let installed = await askCommand([
{
name: 'installed',
type: 'confirm',
message: `Should we run '${
answer.platform === 'pc' ? 'qbnpm' : 'cnpm'
} install' for you after the project has been created? `
}
])
return Promise.resolve(Object.assign(answer, installed))
})
.then((answer) => {
//加载全部配置(vuex,axios)
if (answer.mode === 'all') {
answer.vuex = true
answer.axios = true
}
//主分支
if (answer.platform !== 'wechat' && answer.mode === 'vue2') {
answer.cloneOptions = ['-b', `${answer.platform}/index`]
}
//组件库
if (answer.platform === 'pc' && answer.mode === 'all') {
answer.qbui = true
} else if (answer.platform === 'app' && answer.mode === 'all') {
answer.vant = true
}
//依赖
let temp = []
if (answer.vuex) temp.push('vuex')
if (answer.axios) temp.push('axios')
if (answer.qbui) temp.push('qbui')
if (answer.vant) temp.push('vant')
if (temp.length) {
answer.cloneOptions = ['-b', `${answer.platform}/${temp.join('-')}`]
}
//仓库地址
if (answer.platform === 'pc' || answer.platform === 'app') {
answer.fileUrl =
'https://gitlab.szzhijing.com/quanbu-bu-frontend/quanbu-app-template/quanbu-vue-template.git'
} else if (answer.platform === 'wechat') {
}
console.log(answer)
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)