code_compress.js 4.6 KB

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