wypcli
Version:
```npm install wypcli```
102 lines (99 loc) • 2.91 kB
JavaScript
// promise工具
const program = require("commander")
const path = require('path')
const open = require('open')
const chalk = require('chalk');
const fs = require('fs')
const {
promisify
} = require(('util'))
const download = promisify(require('download-git-repo'))
const {
vueRepo
} = require('../config/repo-config')
const {
commandSpawn
} = require('../untils/terminal')
const {
compile,
writeToFile
} = require('../untils/until')
/**
* 创建项目Action
*
* */
const createProjectAction = async (project) => {
console.log(chalk.yellow('wuyupei help you create Vue Project'));
// 1.clone项目模板
await download(vueRepo, project, {
clone: true
})
const command = process.platform === 'win32' ? 'npm.cmd' : 'npm'
// 2.npm install 安装依赖
await commandSpawn(command, ['install'], {
cwd: `./${project}`
})
// 3.运行项目 npm run serve
commandSpawn(command, ['run', 'serve'], {
cwd: `./${project}`
})
// 4.打开浏览器
open('http://localhost:8080')
}
/**
*添加组件Action
*/
const addComponentAction = async (name) => {
// 编译.ejs文件
let result = await compile('vue-template.ejs', {
name,
lowername: name.toLowerCase()
})
// 写文件到指定的文件里 program.opts().dest 获取参数
const targetPath = path.resolve(program.opts().dest || 'src/components', `${name}.vue`)
writeToFile(targetPath, result)
}
/**
* 添加pageAction
*/
const addPageAction = async (name) => {
const pageResult = await compile('vue-template.ejs', {
name,
lowername: name.toLowerCase()
})
const routeResult = await compile('vue-router.ejs', {
name,
lowername: name.toLowerCase()
})
const targetPagePath = path.resolve(program.opts().dest, `${name}.vue`)
const targetRoutePath = path.resolve(program.opts().dest, `route.js`)
fs.mkdir(program.opts().dest, err => {
if (err) {
console.log(chalk.red(err));
}
})
writeToFile(targetPagePath, pageResult)
writeToFile(targetRoutePath, routeResult)
}
/**
* 添加storeAction
*/
const addStoreAction = async (store) => {
const storeResult = await compile('vue-store.ejs', {})
const typesResult = await compile('vue-types.ejs', {})
const targetStorePath = path.resolve(program.opts().dest, `index.js`)
const targetResultPath = path.resolve(program.opts().dest, `types.js`)
fs.mkdir(program.opts().dest, err => {
if (err) {
console.log(chalk.red(err));
}
})
writeToFile(targetStorePath, storeResult)
writeToFile(targetResultPath, typesResult)
}
module.exports = {
createProjectAction,
addComponentAction,
addPageAction,
addStoreAction
}