acv-cli
Version:
a auto create vue2 cli
74 lines (67 loc) • 2.71 kB
JavaScript
const { promisify } = require('util');
const download = promisify(require('download-git-repo'));
const path = require('path');
// 模板地址
const { vuetmp } = require('../config/temp-comfig')
// 执行cmd指令
const { commandSpawn } = require('../utils/terminal')
// 编译模板
const { compile, writeToFile, createDir } = require('../utils/utils')
// callback -> promisify(函数) -> Promise -> async await
// project 所创建的文件名
// 创建项目
const createAction = async (project) => {
console.log("正在构建项目中~")
// 远程克隆vue2的模板
await download(vuetmp, project, { clone: true })
// 初始化依赖下载 判断电脑配置环境
const command = process.platform === 'win32' ? 'npm.cmd' : 'npm';
await commandSpawn(command, ['install'], { cwd: `./${project}` })
//运行项目 执行npm run serve
commandSpawn(command, ['run', 'serve'], { cwd: `./${project}` });
}
// 创建组件
const addComponentAction = async (name, dest) => {
// 获取编译成功后的模板内容
const result = await compile("vue-component.ejs", { name, lowerName: name.toLowerCase() })
// 写入文件的操作
const targetPath = path.resolve(dest, `${name}.vue`);
if (createDir(dest)) {
writeToFile(targetPath, result);
}
}
// 创建页面
const addPageAndRouteAction = async (page, dest) => {
// 获取编译成功后的模板内容
const data = { name: page, lowerName: page.toLowerCase() }
const pageTmp = await compile("vue-component.ejs", data)
const routeTmp = await compile("vue-router.ejs", data)
// 写入文件的操作
const targetDest = path.resolve(dest, page.toLowerCase())
if (createDir(targetDest)) {
const pagePath = path.resolve(targetDest, `${page}.vue`);
const routePath = path.resolve(targetDest, `router.js`);
writeToFile(pagePath, pageTmp);
writeToFile(routePath, routeTmp);
}
}
// 创建store模块
const addStoreAction = async (module, dest) => {
// 获取编译成功后的模板内容
const storeTmp = await compile("vue-store.ejs", {})
const typeTmp = await compile("vue-types.ejs", {})
// 写入文件的操作
const targetDest = path.resolve(dest, module.toLowerCase())
if (createDir(targetDest)) {
const storePath = path.resolve(targetDest, `${module}.js`);
const typePath = path.resolve(targetDest, `types.js`);
writeToFile(storePath, storeTmp);
writeToFile(typePath, typeTmp);
}
}
module.exports = {
createAction,
addComponentAction,
addPageAndRouteAction,
addStoreAction
}