code_compress.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. 'use strict';
  2. const path = require('path');
  3. const fs = require('fs');
  4. const fsPro = require('fs-extra');
  5. const UglifyJS = require('uglify-js');
  6. class CodeCompress {
  7. constructor() {
  8. this.dirs = [
  9. 'app',
  10. 'electron',
  11. 'config'
  12. ];
  13. this.basePath = path.normalize(__dirname + '/..');
  14. this.backupCodeDir = path.join(this.basePath, 'run/backup_code');
  15. }
  16. /**
  17. * 备份 app、electron目录代码
  18. */
  19. backup () {
  20. console.log('[electron] [code_compress] [backup] start');
  21. this.rmBackup();
  22. for (let i = 0; i < this.dirs.length; i++) {
  23. // check code dir
  24. let codeDirPath = path.join(this.basePath, this.dirs[i]);
  25. if (!fs.existsSync(codeDirPath)) {
  26. console.log('[electron] [code_compress] [backup] ERROR: %s is not exist', codeDirPath);
  27. return
  28. }
  29. // copy
  30. let targetDir = path.join(this.backupCodeDir, this.dirs[i]);
  31. console.log('[electron] [code_compress] [backup] targetDir:', targetDir);
  32. if (!fs.existsSync(targetDir)) {
  33. this.mkdir(targetDir);
  34. this.chmodPath(targetDir, '777');
  35. }
  36. fsPro.copySync(codeDirPath, targetDir);
  37. }
  38. console.log('[electron] [code_compress] [backup] success');
  39. }
  40. /**
  41. * 还原代码
  42. */
  43. restore () {
  44. console.log('[electron] [code_compress] [restore] start');
  45. for (let i = 0; i < this.dirs.length; i++) {
  46. let codeDirPath = path.join(this.backupCodeDir, this.dirs[i]);
  47. let targetDir = path.join(this.basePath, this.dirs[i]);
  48. fsPro.copySync(codeDirPath, targetDir);
  49. }
  50. console.log('[electron] [code_compress] [restore] success');
  51. };
  52. /**
  53. * 压缩代码
  54. */
  55. compress () {
  56. console.log('[electron] [code_compress] [compress] start');
  57. for (let i = 0; i < this.dirs.length; i++) {
  58. let codeDirPath = path.join(this.basePath, this.dirs[i]);
  59. this.compressLoop(codeDirPath);
  60. }
  61. console.log('[electron] [code_compress] [compress] success');
  62. };
  63. compressLoop (dirPath) {
  64. let files = [];
  65. if (fs.existsSync(dirPath)) {
  66. files = fs.readdirSync(dirPath);
  67. files.forEach((file, index) => {
  68. let curPath = dirPath + '/' + file;
  69. if (fs.statSync(curPath).isDirectory()) {
  70. this.compressLoop(curPath);
  71. } else {
  72. if (path.extname(curPath) === '.js') {
  73. this.miniFile(curPath);
  74. }
  75. }
  76. });
  77. }
  78. }
  79. miniFile (file) {
  80. let code = fs.readFileSync(file, "utf8");
  81. const options = {
  82. mangle: {
  83. toplevel: false,
  84. },
  85. };
  86. let result = UglifyJS.minify(code, options);
  87. fs.writeFileSync(file, result.code, "utf8");
  88. }
  89. /**
  90. * 格式化参数
  91. */
  92. formatArgvs () {
  93. // argv
  94. let argvs = [];
  95. for (let i = 0; i < process.argv.length; i++) {
  96. const tmpArgv = process.argv[i]
  97. if (tmpArgv.indexOf('--') !== -1) {
  98. argvs.push(tmpArgv.substr(2))
  99. }
  100. }
  101. return argvs;
  102. }
  103. /**
  104. * 移除备份
  105. */
  106. rmBackup () {
  107. fs.rmdirSync(this.backupCodeDir, {recursive: true});
  108. return;
  109. }
  110. /**
  111. * 检查文件是否存在
  112. */
  113. fileExist (filePath) {
  114. try {
  115. return fs.statSync(filePath).isFile();
  116. } catch (err) {
  117. return false;
  118. }
  119. };
  120. mkdir (dirpath, dirname) {
  121. // 判断是否是第一次调用
  122. if (typeof dirname === 'undefined') {
  123. if (fs.existsSync(dirpath)) {
  124. return;
  125. }
  126. this.mkdir(dirpath, path.dirname(dirpath));
  127. } else {
  128. // 判断第二个参数是否正常,避免调用时传入错误参数
  129. if (dirname !== path.dirname(dirpath)) {
  130. this.mkdir(dirpath);
  131. return;
  132. }
  133. if (fs.existsSync(dirname)) {
  134. fs.mkdirSync(dirpath);
  135. } else {
  136. this.mkdir(dirname, path.dirname(dirname));
  137. fs.mkdirSync(dirpath);
  138. }
  139. }
  140. };
  141. chmodPath (path, mode) {
  142. let files = [];
  143. if (fs.existsSync(path)) {
  144. files = fs.readdirSync(path);
  145. files.forEach((file, index) => {
  146. const curPath = path + '/' + file;
  147. if (fs.statSync(curPath).isDirectory()) {
  148. this.chmodPath(curPath, mode); // 递归删除文件夹
  149. } else {
  150. fs.chmodSync(curPath, mode);
  151. }
  152. });
  153. fs.chmodSync(path, mode);
  154. }
  155. };
  156. }
  157. const cc = new CodeCompress();
  158. let argvs = cc.formatArgvs();
  159. console.log('[electron] [code_compress] argvs:', argvs);
  160. if (argvs.indexOf('compress') != -1) {
  161. cc.backup();
  162. cc.compress();
  163. } else if (argvs.indexOf('restore') != -1) {
  164. cc.restore();
  165. }
  166. module.exports = CodeCompress;