| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- #! /usr/bin/env node
- // Node Cli 应用入口文件必须要有这样的文件头
- // 如果是 Linux 或者 macos 系统还需要修改此文件的读写权限为 755
- // 具体是通过 chmod 755 cli.js 实现修改
- // console.log('working');
- // 脚手架的工作过程
- // 1.通过命令交互询问用户问题
- // 2.根据用户回答的结果生成文件
- const inquirer = require('inquirer');
- const fs = require('fs');
- const path = require('path');
- const ejs = require('ejs');
- const { tmpdir } = require('os');
- const { createPartiallyEmittedExpression } = require('typescript');
- // 读取json
- const list = JSON.parse(fs.readFileSync('./generate-code/pc_menu_企业风管.json'));
- // 目标目录
- // const destDir = process.cwd();
- const destDir = path.join(process.cwd(), 'src/pages');
- //检查某个目录是否存在
- function hasFile(url, code) {
- const temp = path.join(url, `/${code}`);
- try {
- //检查某个目录是否存在
- fs.statSync(temp);
- return true;
- } catch (error) {
- console.log(temp);
- console.log(error);
- return false;
- }
- }
- // 模板目录
- const tempDir = path.join(__dirname, 'templates');
- function writeFile(url, code) {
- if (!hasFile(url, code)) {
- const temp = path.join(url, `/${code}`);
- console.log(temp);
- return fs.promises.mkdir(temp);
- }
- return Promise.resolve(false);
- }
- list.forEach((el) => {
- const { code, children } = el;
- writeFile(destDir, code).then(() => {
- if (children) {
- children.forEach((item) => {
- writeFile(path.join(destDir, `/${code}`), item.code).then(() => {
- if (item.children) {
- item.children.forEach((e) => {
- const path1 = `/${code}/${item.code}`;
- writeFile(path.join(destDir, path1), e.code).then(() => {
- const path2 = `${path1}/${e.code}`;
- // index.vue
- if (!hasFile(path.join(destDir, path2), 'index.vue')) {
- const template = `
- `;
- fs.writeFileSync(path.join(destDir, `${path2}/index.vue`), template);
- }
- // components
- writeFile(path.join(destDir, `${path2}`), 'components').then(() => {
- if (e.children) {
- const path3 = `${path2}/components`;
- e.children.forEach((ele) => {
- writeFile(path.join(destDir, path3), ele.code).then((res) => {
- if (res) {
- if (
- !hasFile(
- path.join(destDir, `${path3}/${ele.code}`),
- 'index.vue'
- )
- ) {
- const template = `
- `;
- fs.writeFileSync(
- path.join(destDir, `${path3}${ele.code}/index.vue`),
- template
- ).then(res => {
- if (res) {
- }
- }
- }
- }
- });
- });
- }
- });
- });
- });
- }
- });
- });
- }
- });
- });
|