builder.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #!/usr/bin/env node
  2. 'use strict';
  3. /**
  4. * 代码生成器
  5. *
  6. * @author CaiAoLin
  7. * @date 2018/2/1
  8. * @version
  9. */
  10. const path = require('path');
  11. const fs = require('fs');
  12. const command = [];
  13. const params = {};
  14. const rootDir = path.resolve(__dirname, '..');
  15. const appDir = rootDir + '\\app\\';
  16. const controllerDir = appDir + 'controller\\';
  17. const serviceDir = appDir + 'service\\';
  18. const viewDir = appDir + 'view\\';
  19. const templateDir = rootDir + '\\tool\\templates\\';
  20. process.argv.forEach(function(param, index) {
  21. // 前两个命令忽略 (node 以及当前js目录)
  22. if (index <= 1) {
  23. return false;
  24. }
  25. if (index === 2) {
  26. // 记录命令
  27. command.push(param);
  28. } else {
  29. const paramInfo = param.split(':');
  30. params[paramInfo[0]] = paramInfo.length !== 2 ? '' : paramInfo[1];
  31. }
  32. });
  33. if (command.length <= 0) {
  34. console.log('命令错误!');
  35. }
  36. const moduleCommand = command[0];
  37. selectModule(moduleCommand);
  38. /**
  39. * 模块选择
  40. *
  41. * @param {String} moduleCommand - 模块命令
  42. * @return {void}
  43. */
  44. function selectModule(moduleCommand) {
  45. const commandInfo = moduleCommand.split(':');
  46. if (commandInfo.length !== 2) {
  47. console.log('命令参数错误');
  48. return;
  49. }
  50. // 读入模板数据
  51. const templateFile = templateDir + commandInfo[0] + '.txt';
  52. const templateContent = fs.existsSync(templateFile) ? fs.readFileSync(templateFile) : '';
  53. switch (commandInfo[0]) {
  54. case 'controller':
  55. addFile(commandInfo[1], templateContent, 'controller');
  56. break;
  57. case 'service':
  58. addFile(commandInfo[1], templateContent, 'service');
  59. break;
  60. case 'all':
  61. const controllerContent = fs.readFileSync(templateDir + 'controller.txt');
  62. const serviceContent = fs.readFileSync(templateDir + 'service.txt');
  63. const viewContent = fs.readFileSync(templateDir + 'view.txt');
  64. addFile(commandInfo[1], controllerContent, 'controller');
  65. addFile(commandInfo[1], serviceContent, 'service');
  66. addFile(commandInfo[1], viewContent, 'view');
  67. break;
  68. default:
  69. break;
  70. }
  71. }
  72. /**
  73. * 新增文件
  74. *
  75. * @param {String} name - 文件名
  76. * @param {String} content - 写入内容
  77. * @param {String} type - 文件类型
  78. * @return {void}
  79. */
  80. function addFile(name, content, type) {
  81. // 模板变量定义
  82. const current = new Date();
  83. const templateData = {
  84. date: current.getFullYear() + '/' + (current.getMonth() + 1) + '/' + current.getDate(),
  85. };
  86. let fileName = '';
  87. let writeDir = '';
  88. switch (type) {
  89. case 'controller':
  90. fileName = name + '_controller.js';
  91. // 处理文件大小写
  92. templateData.name = convertName(name);
  93. writeDir = controllerDir + fileName;
  94. break;
  95. case 'service':
  96. fileName = name + '.js';
  97. // 处理文件大小写
  98. templateData.name = convertName(name);
  99. templateData.table = params.table === undefined ? name.toLowerCase() : params.table;
  100. writeDir = serviceDir + fileName;
  101. break;
  102. case 'view':
  103. // 判断是否有对应的文件夹
  104. const folderName = params.folder === undefined ? name.toLowerCase() : params.folder;
  105. const folder = viewDir + folderName;
  106. if (!fs.existsSync(folder)) {
  107. fs.mkdirSync(folder);
  108. }
  109. fileName = name + '.ejs';
  110. writeDir = folder + '\\' + fileName;
  111. break;
  112. default:
  113. console.log('不支持对应的模块');
  114. return;
  115. }
  116. // 替换模板变量
  117. content = templateVarReplace(content, templateData);
  118. // 写入文件
  119. fs.writeFileSync(writeDir, content);
  120. }
  121. /**
  122. * 替换模板变量
  123. *
  124. * @param {String} content - 模板文件内容
  125. * @param {Object} templateData - 变量替换内容
  126. * @return {String} - 返回替换后的内容
  127. */
  128. function templateVarReplace(content, templateData) {
  129. content = content.toString();
  130. if (templateData.name !== undefined) {
  131. content = content.replace(/{{NAME}}/g, templateData.name);
  132. }
  133. if (templateData.date !== undefined) {
  134. content = content.replace(/{{DATE}}/g, templateData.date);
  135. }
  136. if (templateData.table !== undefined) {
  137. content = content.replace(/{{TABLE}}/g, templateData.table);
  138. }
  139. return content;
  140. }
  141. /**
  142. * 处理文件名(把test_test转换为大驼峰模式TestTest)
  143. *
  144. * @param {String} content - 待转换内容
  145. * @return {String} - 转换后的结果
  146. */
  147. function convertName(content) {
  148. let result = '';
  149. const contentArray = content.split('_');
  150. for (const tmp of contentArray) {
  151. result += tmp.substring(0, 1).toUpperCase() + tmp.substring(1);
  152. }
  153. return result;
  154. }