cli.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. // 在终端 执行 generate-code
  10. const inquirer = require('inquirer');
  11. const fs = require('fs');
  12. const path = require('path');
  13. const ejs = require('ejs');
  14. // 读取json
  15. const list = JSON.parse(fs.readFileSync('./generate-code/pc_menu_企业风管.json'));
  16. // 目标目录
  17. // const destDir = process.cwd();
  18. const destDir = path.join(process.cwd(), 'src');
  19. /*** ===================== 生成弹窗名枚举 ==================== ****/
  20. const enumPath = path.join(destDir, '/common/constants/modalNameEnum.ts');
  21. let name = `export enum ModalEnum {
  22. notice = 'notice', // 消息
  23. logout = 'logout', // 退出登录
  24. commomOrder = 'commom-order', // 下单通用界面
  25. detail = 'detail', // 详情`;
  26. function getModalEnum(arr, callback) {
  27. arr.forEach((el) => {
  28. const { code, title, children, type } = el;
  29. if (type === 2) {
  30. // 按钮类型
  31. fs.promises.readFile(enumPath).then((res) => {
  32. console.log(code);
  33. if (!name.includes(code)) {
  34. name += `
  35. ${code} = '${code}', // ${title}
  36. `;
  37. callback();
  38. }
  39. getModalEnum(children, callback);
  40. });
  41. } else {
  42. getModalEnum(children, callback);
  43. }
  44. });
  45. }
  46. getModalEnum(list, () => {
  47. const temp =
  48. name +
  49. `
  50. }`;
  51. fs.writeFileSync(enumPath, temp, { flag: 'w+' }, (err) => {
  52. // !err && console.log('写入文件成功!');
  53. });
  54. });
  55. /*** ===================== 生成单据组件名枚举 ==================== ****/
  56. const enumOrderPath = path.join(destDir, '/common/constants/enumOrderComponents.ts');
  57. let orderName = `// 组件名枚举
  58. export enum enumOrderComponents {`;
  59. function getOrderName(arr, callback) {
  60. arr.forEach((el) => {
  61. const { code, title, children, type } = el;
  62. if (type === 4) {
  63. // 单据类型
  64. fs.promises.readFile(enumOrderPath).then((res) => {
  65. if (!orderName.includes(code)) {
  66. orderName += `
  67. ${code} = '${code}', // ${title}
  68. `;
  69. callback();
  70. }
  71. getOrderName(children, callback);
  72. });
  73. } else {
  74. getOrderName(children, callback);
  75. }
  76. });
  77. }
  78. getOrderName(list, () => {
  79. const temp =
  80. orderName +
  81. `
  82. }`;
  83. fs.writeFileSync(enumOrderPath, temp, { flag: 'w+' }, (err) => {
  84. // !err && console.log('写入文件成功!');
  85. });
  86. });