cli.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #! /usr/bin/env node
  2. // Node Cli 应用入口文件必须要有这样的文件头
  3. // 如果是 Linux 或者 macos 系统还需要修改此文件的读写权限为 755
  4. // 具体是通过 chmod 755 cli.js 实现修改
  5. // console.log('working');
  6. // 脚手架的工作过程
  7. // 1.通过命令交互询问用户问题
  8. // 2.根据用户回答的结果生成文件
  9. const inquirer = require('inquirer');
  10. const fs = require('fs');
  11. const path = require('path');
  12. const ejs = require('ejs');
  13. const { tmpdir } = require('os');
  14. const { createPartiallyEmittedExpression } = require('typescript');
  15. // 读取json
  16. const list = JSON.parse(fs.readFileSync('./generate-code/pc_menu_企业风管.json'));
  17. // 目标目录
  18. // const destDir = process.cwd();
  19. const destDir = path.join(process.cwd(), 'src/pages');
  20. //检查某个目录是否存在
  21. function hasFile(url, code) {
  22. const temp = path.join(url, `/${code}`);
  23. try {
  24. //检查某个目录是否存在
  25. fs.statSync(temp);
  26. return true;
  27. } catch (error) {
  28. console.log(temp);
  29. console.log(error);
  30. return false;
  31. }
  32. }
  33. // 模板目录
  34. const tempDir = path.join(__dirname, 'templates');
  35. function writeFile(url, code) {
  36. if (!hasFile(url, code)) {
  37. const temp = path.join(url, `/${code}`);
  38. console.log(temp);
  39. return fs.promises.mkdir(temp);
  40. }
  41. return Promise.resolve(false);
  42. }
  43. list.forEach((el) => {
  44. const { code, children } = el;
  45. writeFile(destDir, code).then(() => {
  46. if (children) {
  47. children.forEach((item) => {
  48. writeFile(path.join(destDir, `/${code}`), item.code).then(() => {
  49. if (item.children) {
  50. item.children.forEach((e) => {
  51. const path1 = `/${code}/${item.code}`;
  52. writeFile(path.join(destDir, path1), e.code).then(() => {
  53. const path2 = `${path1}/${e.code}`;
  54. // index.vue
  55. if (!hasFile(path.join(destDir, path2), 'index.vue')) {
  56. const template = `
  57. `;
  58. fs.writeFileSync(path.join(destDir, `${path2}/index.vue`), template);
  59. }
  60. // components
  61. writeFile(path.join(destDir, `${path2}`), 'components').then(() => {
  62. if (e.children) {
  63. const path3 = `${path2}/components`;
  64. e.children.forEach((ele) => {
  65. writeFile(path.join(destDir, path3), ele.code).then((res) => {
  66. if (res) {
  67. if (
  68. !hasFile(
  69. path.join(destDir, `${path3}/${ele.code}`),
  70. 'index.vue'
  71. )
  72. ) {
  73. const template = `
  74. `;
  75. fs.writeFileSync(
  76. path.join(destDir, `${path3}${ele.code}/index.vue`),
  77. template
  78. ).then(res => {
  79. if (res) {
  80. }
  81. }
  82. }
  83. }
  84. });
  85. });
  86. }
  87. });
  88. });
  89. });
  90. }
  91. });
  92. });
  93. }
  94. });
  95. });