02_learn-cli-cqtext
Version:
88 lines (75 loc) • 2.9 kB
JavaScript
const { promisify } = require("util");
const download = promisify(require("download-git-repo"));
const open = require("open");
const path = require("path");
const { vueRepo } = require("../config/repo-config");
const { commandSpawn } = require("../utils/terminal");
const { compile, writeToFile, createDirSync } = require("../utils/utils");
const createProjectAction = async (project) => {
console.log("why helps you create your project ~");
// 1.clone项目
await download(vueRepo, project, { clone: true });
// 2.执行 npm install
// 再windows电脑上执行的是npm.cmd,虽然可以不写出来,但是自己写的很需要
const command = process.platform === "win32" ? "npm.cmd" : "npm";
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, dest) => {
//1.有对应的ejs模板
// 2.编译ejs模板 result
const result = await compile("vue-component.ejs", {
name,
lowerName: name.toLowerCase(),
});
// 3。将result写入.vue文件中
const targetPath = path.resolve(dest, `${name}.vue`);
console.log(targetPath);
writeToFile(targetPath, result);
// 4.放入对应的文件夹中
};
// page
const addPageAndRouteAction = async (name, dest) => {
//1.有对应的ejs模板
// 2.编译ejs模板 result
const pageResult = await compile("vue-component.ejs", {
name,
lowerName: name.toLowerCase(),
});
const routeResult = await compile("vue-component.ejs", {
name,
lowerName: name.toLowerCase(),
});
// 3。将result写入.vue文件中
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);
}
// 4.放入对应的文件夹中
};
const addStoreAction = async (name, dest) => {
// 1.遍历的过程
const storeResult = await compile("vue-store.ejs", {});
const typesResult = await compile("vue-types.ejs", {});
// 2.创建文件
const targetDest = path.resolve(dest, name.toLowerCase());
if (createDirSync(targetDest)) {
const targetPagePath = path.resolve(targetDest, `${name}.js`);
const targetRoutePath = path.resolve(targetDest, "types.js");
writeToFile(targetPagePath, storeResult);
writeToFile(targetRoutePath, typesResult);
}
};
module.exports = {
createProjectAction,
addComponentAction,
addPageAndRouteAction,
addStoreAction,
};