123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- const chalk = require("chalk");
- const path = require("path");
- const fs = require("fs");
- const resolve = (...file) => path.resolve(__dirname, ...file);
- const log = message => console.log(chalk.yellow(`${message}`));
- const successLog = message => console.log(chalk.green(`${message}`));
- const errorLog = error => console.log(chalk.red(`${error}`));
- const {
- indexPage,
- addEditPage,
- servicePage,
-
- } = require("./template");
- const generateFile = (path, data) => {
- if (fs.existsSync(path)) {
- errorLog(`${path}文件已存在`);
- return;
- }
- return new Promise((resolve, reject) => {
- fs.writeFile(path, data, "utf8", err => {
- if (err) {
- errorLog(err.message);
- reject(err);
- } else {
- resolve(true);
- }
- });
- });
- };
- log("请输入要生成的页面名称:");
- let componentName = "";
- process.stdin.on("data", async chunk => {
- const inputName = String(chunk)
- .trim()
- .toString();
-
- const componentDirectory = resolve("../src/views", inputName);
-
- const apisServiceDirectory = resolve("../src/apis/service", inputName);
-
-
-
- const listTablePath = resolve(componentDirectory, "index.vue");
-
- const addEditPath = resolve(componentDirectory, "addEdit.vue");
-
- const servicePath = resolve(apisServiceDirectory, "index.js");
-
-
-
- const hasComponentDirectory = fs.existsSync(componentDirectory);
- if (hasComponentDirectory) {
- errorLog(`${inputName}组件目录已存在,请重新输入`);
- return;
- } else {
- log(`正在生成 component 目录 ${componentDirectory}`);
- await dotExistDirectoryCreate(componentDirectory);
- }
-
- const hasService = fs.existsSync(apisServiceDirectory);
- if (hasService) {
- errorLog(`${inputName}service目录已存在,请重新输入`);
- return;
- } else {
- log(`正在生成 apis/service 目录 ${apisServiceDirectory}`);
- await dotExistDirectoryCreate(apisServiceDirectory);
- }
-
-
-
-
-
-
-
-
-
- try {
- if (inputName.includes("/")) {
- const inputArr = inputName.split("/");
- componentName = inputArr[inputArr.length - 1];
- } else {
- componentName = inputName;
- }
- log(`正在生成 views-列表 文件 ${listTablePath}`);
- await generateFile(listTablePath, indexPage(inputName, componentName));
- log(`正在生成 views-添加修改详情 文件 ${addEditPath}`);
- await generateFile(addEditPath, addEditPage(inputName, componentName));
- log(`正在生成 apis-service 文件 ${servicePath}`);
- await generateFile(servicePath, servicePage);
-
-
- successLog("生成成功");
- } catch (e) {
- errorLog(e.message);
- }
- process.stdin.emit("end");
- });
- process.stdin.on("end", () => {
- log("exit");
- process.exit();
- });
- function dotExistDirectoryCreate(directory) {
- return new Promise(resolve => {
- mkdirs(directory, function () {
- resolve(true);
- });
- });
- }
- function mkdirs(directory, callback) {
- var exists = fs.existsSync(directory);
- if (exists) {
- callback();
- } else {
- mkdirs(path.dirname(directory), function () {
- fs.mkdirSync(directory);
- callback();
- });
- }
- }
|