code_compress.js 4.3 KB

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