index.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. let packageName = "proto-killer";
  2. let fs = require('fire-fs');
  3. let path = require('fire-path');
  4. let Electron = require('electron');
  5. let configUtil = Editor.require('packages://' + packageName + '/core/config-util.js');
  6. let protoItem = Editor.require('packages://' + packageName + '/panel/item/protoItem.js');
  7. let protoProcessor = Editor.require('packages://' + packageName + '/core/proto-processor.js');
  8. let projectPath = Editor.Project.path;
  9. Editor.Panel.extend({
  10. style: fs.readFileSync(Editor.url('packages://' + packageName + '/panel/index.css', 'utf8')) + "",
  11. template: fs.readFileSync(Editor.url('packages://' + packageName + '/panel/index.html', 'utf8')) + "",
  12. $: {
  13. logTextArea: '#logTextArea',
  14. },
  15. ready() {
  16. let logCtrl = this.$logTextArea;
  17. let logListScrollToBottom = function () {
  18. setTimeout(function () {
  19. logCtrl.scrollTop = logCtrl.scrollHeight;
  20. }, 10);
  21. };
  22. protoItem.init();
  23. this.plugin = new window.Vue({
  24. el: this.shadowRoot,
  25. data: {
  26. logView: '',
  27. protoRootPath: null,
  28. outputPath: null,
  29. protoFileArr: null,
  30. protoDataArr: null,
  31. },
  32. init() {
  33. // Editor.info('init...');
  34. },
  35. created() {
  36. // Editor.info('created...');
  37. this._initConfig();
  38. },
  39. methods: {
  40. _initConfig() {
  41. configUtil.initCfg((data) => {
  42. this.protoRootPath = data.protoRootPath;
  43. this.outputPath = data.outputPath;
  44. if (this.protoRootPath != "" && fs.existsSync(this.protoRootPath)) {
  45. this._onFindAllProtoFile(this.protoRootPath);
  46. }
  47. });
  48. },
  49. _saveConfig() {
  50. let data = {
  51. protoRootPath: this.protoRootPath,
  52. outputPath: this.outputPath
  53. };
  54. configUtil.saveCfg(data);
  55. },
  56. _addLog(str) {
  57. let time = new Date();
  58. // this.logView = "[" + time.toLocaleString() + "]: " + str + "\n" + this.logView;
  59. this.logView += "[" + time.toLocaleString() + "]: " + str + "\n";
  60. logListScrollToBottom();
  61. },
  62. // 选择目录
  63. onBtnClickSelectProtoRootPath() {
  64. let res = Editor.Dialog.openFile({
  65. title: "选择Proto的根目录",
  66. defaultPath: projectPath,
  67. properties: ['openDirectory'],
  68. });
  69. if (res !== -1) {
  70. let dir = res[0];
  71. if (dir !== this.protoRootPath) {
  72. this.protoRootPath = dir;
  73. this._onFindAllProtoFile(dir);
  74. this._saveConfig();
  75. }
  76. }
  77. },
  78. // 打开目录
  79. onBtnClickOpenProtoRootPath() {
  80. if (fs.existsSync(this.protoRootPath)) {
  81. Electron.shell.showItemInFolder(this.protoRootPath);
  82. Electron.shell.beep();
  83. } else {
  84. this._addLog("目录不存在:" + this.protoRootPath);
  85. }
  86. },
  87. // 刷新目录
  88. onBtnClickFreshProto() {
  89. this._onFindAllProtoFile(this.protoRootPath);
  90. },
  91. // 生成
  92. onBtnClickGen() {
  93. this.logView = '';
  94. let str = protoProcessor.deal(this.protoDataArr);
  95. this._writeToFile(str);
  96. },
  97. // 查找出目录下所有的proto文件
  98. _onFindAllProtoFile(dir) {
  99. if (dir) {
  100. let allFileArr = [];
  101. let protoFileArr = [];
  102. // 获取目录下所有的文件
  103. readDirSync(dir);
  104. // 过滤.json文件
  105. for (let key in allFileArr) {
  106. let file = allFileArr[key];
  107. let extName = path.extname(file);
  108. if (extName === '.proto') {
  109. protoFileArr.push(file);
  110. }
  111. }
  112. this.protoFileArr = protoFileArr;
  113. this.protoDataArr = [];
  114. for (let i = 0; i < protoFileArr.length; i++) {
  115. let itemFullPath = protoFileArr[i];
  116. let itemData = {
  117. isUse: true,
  118. fullPath: itemFullPath,
  119. name: 'name'
  120. };
  121. itemData.name = itemFullPath.substr(dir.length + 1, itemFullPath.length - dir.length);
  122. this.protoDataArr.push(itemData);
  123. }
  124. function readDirSync(dirPath) {
  125. let dirInfo = fs.readdirSync(dirPath);
  126. for (let i = 0; i < dirInfo.length; i++) {
  127. let item = dirInfo[i];
  128. let itemFullPath = path.join(dirPath, item);
  129. let info = fs.statSync(itemFullPath);
  130. if (info.isDirectory()) {
  131. // this._addLog('dir: ' + itemFullPath);
  132. readDirSync(itemFullPath);
  133. } else if (info.isFile()) {
  134. allFileArr.push(itemFullPath);
  135. // this._addLog('file: ' + itemFullPath);
  136. }
  137. }
  138. }
  139. }
  140. },
  141. _writeToFile(str) {
  142. let writePath = projectPath + `\\${this.outputPath}`;
  143. fs.writeFileSync(writePath, str, {
  144. encoding: 'utf8',
  145. flag: 'w'
  146. });
  147. this._addLog(`proto自动写入成功 ==> ${writePath}`);
  148. }
  149. }
  150. });
  151. },
  152. messages: {
  153. 'proto-killer:hello'(event) {
  154. }
  155. }
  156. });