@fuse-oo/cli
Version:
fuse内部脚手架工具
83 lines (74 loc) • 2.52 kB
JavaScript
const fs = require('fs-extra')
const path = require('path')
const chalk = require('chalk')
const inquirer = require('inquirer')
const PageCreator = require('./PageCreator')
const validFileName = require('valid-filename')
const {error, stopSpinner, exit, clearConsole} = require('../lib/utils/common')
const pages = ['html', 'vue']
/**
* 创建项目
* @param {*} pageName
* @param {*} options
*/
async function create (pageName, options) {
// 检测文件名是否合规
const result = validFileName(pageName)
// 如果所输入的不是合法npm包名,则退出
if (!result) {
error(chalk.red(`不合法的文件名: "${pageName}"`))
exit(1)
}
// 确定新建模版的类型
const type = options.name || 'html'
const cwd = options.cwd || process.cwd()
const projectPath = type === 'vue' ? './src/son-sys' : ''
// const filePath = pageName.charAt(0).toUpperCase() + pageName.slice(1).toLowerCase() + (type === 'vue' ? '-sys' : '')
const filePath = pageName + (type === 'vue' ? '-sys' : '')
const pagePath = path.resolve(cwd, projectPath, filePath)
const pkgJsonFile = path.resolve(cwd, 'package.json')
if (type && !pages.includes(type)) {
error(chalk.red(`请输入正确的模版名称`), '💣')
return
}
// 如果创建的是vue模版,若不存在package.json,说明不再根目录,不能创建
if (type === 'vue' && !fs.existsSync(pkgJsonFile)) {
error(chalk.red(`请确认您是否在项目根目录下运行此命令`), '💣')
return
}
// 如果page已经存在,询问覆盖还是取消
if (fs.existsSync(pagePath)) {
// 是否直接覆盖
if (options.force) {
await fs.remove(pagePath)
} else { // 用户自己选择
await clearConsole()
const { action } = await inquirer.prompt([
{
name: 'action',
type: 'list',
message: `已存在 ${chalk.cyan(pageName)} 页面,请选择:`,
choices: [
{name: '覆盖', value: true},
{name: '取消', value: false},
]
}
])
if (!action) {
return
} else {
console.log(`\nRemoving ${chalk.cyan(pagePath)}...`)
await fs.remove(pagePath)
}
}
}
// 前面完成准备工作,正式开始创建页面
const pageCreator = new PageCreator(pageName, pagePath)
await pageCreator.create(options)
}
module.exports = (...args) => {
return create(...args).catch(err => {
stopSpinner(false)
error(err)
})
}