code_compress.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. console.log('[electron] [code_compress] moving frontend asset to egg public dir');
  7. console.log('[electron] [code_compress] start');
  8. class codeCompress {
  9. constructor(options = {}) {
  10. this.dirs = [
  11. 'app',
  12. 'electron'
  13. ];
  14. }
  15. /**
  16. * 备份 app、electron目录代码
  17. */
  18. backup () {
  19. let backupCodeDir = path.normalize(__dirname + '/../backup_code');
  20. if (!fs.existsSync(backupCodeDir)) {
  21. this.mkdir(backupCodeDir);
  22. this.chmodPath(backupCodeDir, '777');
  23. }
  24. for (let i = 0; i < this.dirs.length; i++) {
  25. // check code dir
  26. let codeDirPath = path.normalize(__dirname + '/../' + this.dirs[i]);
  27. if (!this.fileExist(codeDirPath)) {
  28. console.log('[electron] [code_compress] ERROR %s not exist', codeDirPath);
  29. return
  30. }
  31. // copy
  32. let targetDir = path.join(backupCodeDir, this.dirs[i]);
  33. fsPro.copySync(codeDirPath, targetDir);
  34. }
  35. }
  36. /**
  37. * 检查文件是否存在
  38. */
  39. fileExist (filePath) {
  40. try {
  41. return fs.statSync(filePath).isFile();
  42. } catch (err) {
  43. return false;
  44. }
  45. };
  46. mkdir (dirpath, dirname) {
  47. // 判断是否是第一次调用
  48. if (typeof dirname === 'undefined') {
  49. if (fs.existsSync(dirpath)) {
  50. return;
  51. }
  52. this.mkdir(dirpath, path.dirname(dirpath));
  53. } else {
  54. // 判断第二个参数是否正常,避免调用时传入错误参数
  55. if (dirname !== path.dirname(dirpath)) {
  56. this.mkdir(dirpath);
  57. return;
  58. }
  59. if (fs.existsSync(dirname)) {
  60. fs.mkdirSync(dirpath);
  61. } else {
  62. this.mkdir(dirname, path.dirname(dirname));
  63. fs.mkdirSync(dirpath);
  64. }
  65. }
  66. console.log('==> dir end', dirpath);
  67. };
  68. chmodPath (path, mode) {
  69. let files = [];
  70. if (fs.existsSync(path)) {
  71. files = fs.readdirSync(path);
  72. files.forEach((file, index) => {
  73. const curPath = path + '/' + file;
  74. if (fs.statSync(curPath).isDirectory()) {
  75. this.chmodPath(curPath, mode); // 递归删除文件夹
  76. } else {
  77. fs.chmodSync(curPath, mode);
  78. }
  79. });
  80. fs.chmodSync(path, mode);
  81. }
  82. };
  83. }