UNPKG

cjlfastvuecli

Version:
75 lines (72 loc) 2.96 kB
const path = require('path') //转化成promise的库,不用安装,node自带 const { promisify } = require('util') //转成promise const download = promisify(require('download-git-repo')) //引入open库,用于打开浏览器 const open = require('open') //拿到仓库地址 const { vueRepo } = require('../config/repo-config') //拿到终端信息 const { commandSpawn } = require('../utils/terminal') //编译ejs const { compile, writeToFile, createDirSync } = require('../utils/utils') //创建项目的action //callback -> promisify函数 ->promise -> async awaut const createProjectAction = async (project) => { console.log('正在帮你创建项目~'); //1clone项目 //vueRepo仓库名 project 克隆后保存到的地址 await download(vueRepo, project, { clone: true }); //执行 npm install const command = process.platform === 'win32' ? 'npm.cmd' : 'npm' await commandSpawn(command, ['install'], { cwd: `./${project}` }) //运行npm run serve commandSpawn(command, ['run', 'serve'], { cwd: `./${project}` }) //打开浏览器 安装open库 npm install open open('http://localhost:8080/') } //添加组件的action const addCpnAction = async (name, dest) => { //1有对应的ejs模板 const result = await compile('vue-component.ejs', { name, lowerName: name.toLowerCase() }) //写入文件的操作 const targetPath = path.resolve(dest, `${name}.vue`) writeToFile(targetPath, result) //2搬移ejs模板result //3将result写入到.vue文件中 //4放到相应的文件夹中 } //添加页面以及当前页面的路由配置 const addPageAndRoute = async (name, dest) => { //编译ejs模板 const data = { name, lowerName: name.toLowerCase() }; const pageResult = await compile('vue-component.ejs', data) const routeResult = await compile('vue-router.ejs', data) //写入文件 const targetDest = path.resolve(dest,name.toLowerCase()) if (createDirSync(targetDest)) { const targetPagePath = path.resolve(targetDest, `${name}.vue`) const targetRoutePath = path.resolve(targetDest, 'router.js') writeToFile(targetPagePath, pageResult) writeToFile(targetRoutePath, routeResult) } } //添加store const addStoreAction = async(name,dest) =>{ const storeResult = await compile('vue-store.ejs',{}) const typesResult = await compile('vue-types.ejs',{}) //写入文件 const targetDest1 = path.resolve(dest,name.toLowerCase()) if (createDirSync(targetDest1)) { const targetStorePath = path.resolve(targetDest1, `${name}.js`) const targetTypesPath = path.resolve(targetDest1, 'types.js') writeToFile(targetStorePath, storeResult) writeToFile(targetTypesPath, typesResult) } } module.exports = { createProjectAction, addCpnAction, addPageAndRoute, addStoreAction }