-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathactions.js
More file actions
64 lines (51 loc) · 2.01 KB
/
actions.js
File metadata and controls
64 lines (51 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const { promisify } = require('util');
const path = require('path');
const fs = require('fs');
const program = require('commander');
const downloadRepo = promisify(require('download-git-repo'));
const open = require('open');
const log = require('../utils/log');
const terminal = require('../utils/terminal');
const { ejsCompile, writeFile, mkdirSync } = require('../utils/file');
const repoConfig = require('../config/repo_config');
const createProject = async (project, otherArg) => {
// 1.提示信息
log.hint('coderwhy helps you create your project, please wait a moment~');
// 2.clone项目从仓库
await downloadRepo(repoConfig.vueGitRepo, project, { clone: true });
// 3.执行终端命令npm install
// terminal.exec('npm install', {cwd: `./${project}`});
const npm = process.platform === 'win32' ? 'npm.cmd' : 'npm';
await terminal.spawn(npm, ['install'], { cwd: `./${project}` });
// 4.打开浏览器
open('http://localhost:8080/');
// 5.运行项目
await terminal.spawn(npm, ['run', 'serve'], { cwd: `./${project}` });
}
const handleEjsToFile = async (name, dest, template, filename) => {
// 1.获取模块引擎的路径
const templatePath = path.resolve(__dirname, template);
const result = await ejsCompile(templatePath, {name, lowerName: name.toLowerCase()});
// 2.写入文件中
// 判断文件夹是否存在,不存在就创建对应的文件夹
mkdirSync(dest);
const targetPath = path.resolve(dest, filename);
writeFile(targetPath, result);
}
const addComponent = async (name, dest) => {
handleEjsToFile(name, dest, '../template/component.vue.ejs', `${name}.vue`);
}
const addPage = async (name, dest) => {
addComponent(name, dest);
handleEjsToFile(name, dest, '../template/vue-router.js.ejs', 'router.js')
}
const addStore = async (name, dest) => {
handleEjsToFile(name, dest, '../template/vue-store.js.ejs', 'index.js')
handleEjsToFile(name, dest, '../template/vue-types.js.ejs', 'types.js')
}
module.exports = {
createProject,
addComponent,
addPage,
addStore
}