| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- 'use strict';
- const path = require('path');
- const fs = require('fs');
- const fsPro = require('fs-extra');
- const UglifyJS = require('uglify-js');
- class CodeCompress {
- constructor() {
- this.dirs = [
- 'app',
- 'electron',
- 'config'
- ];
- this.basePath = path.normalize(__dirname + '/..');
- this.backupCodeDir = path.join(this.basePath, 'run/backup_code');
- if (!fs.existsSync(this.backupCodeDir)) {
- this.mkdir(this.backupCodeDir);
- this.chmodPath(this.backupCodeDir, '777');
- }
- }
- /**
- * 备份 app、electron目录代码
- */
- backup () {
- console.log('[electron] [code_compress] [backup] start');
- this.rmBackup();
- for (let i = 0; i < this.dirs.length; i++) {
- // check code dir
- let codeDirPath = path.join(this.basePath, this.dirs[i]);
- if (!fs.existsSync(codeDirPath)) {
- console.log('[electron] [code_compress] [backup] ERROR: %s is not exist', codeDirPath);
- return
- }
- // copy
- let targetDir = path.join(this.backupCodeDir, this.dirs[i]);
- console.log('[electron] [code_compress] [backup] targetDir:', targetDir);
- if (!fs.existsSync(targetDir)) {
- this.mkdir(targetDir);
- this.chmodPath(targetDir, '777');
- }
- fsPro.copySync(codeDirPath, targetDir);
- }
- console.log('[electron] [code_compress] [backup] success');
- }
- /**
- * 还原代码
- */
- restore () {
- console.log('[electron] [code_compress] [restore] start');
- for (let i = 0; i < this.dirs.length; i++) {
- let codeDirPath = path.join(this.backupCodeDir, this.dirs[i]);
- let targetDir = path.join(this.basePath, this.dirs[i]);
- fsPro.copySync(codeDirPath, targetDir);
- }
- console.log('[electron] [code_compress] [restore] success');
- };
- /**
- * 压缩代码
- */
- compress () {
- console.log('[electron] [code_compress] [compress] start');
- for (let i = 0; i < this.dirs.length; i++) {
- let codeDirPath = path.join(this.basePath, this.dirs[i]);
- this.compressLoop(codeDirPath);
- }
- console.log('[electron] [code_compress] [compress] success');
- };
- compressLoop (dirPath) {
- let files = [];
- if (fs.existsSync(dirPath)) {
- files = fs.readdirSync(dirPath);
- files.forEach((file, index) => {
- let curPath = dirPath + '/' + file;
- if (fs.statSync(curPath).isDirectory()) {
- this.compressLoop(curPath);
- } else {
- if (path.extname(curPath) === '.js') {
- this.miniFile(curPath);
- }
- }
- });
- }
- }
- miniFile (file) {
- let code = fs.readFileSync(file, "utf8");
- const options = {
- mangle: {
- toplevel: false,
- },
- };
-
- let result = UglifyJS.minify(code, options);
- fs.writeFileSync(file, result.code, "utf8");
- }
- /**
- * 格式化参数
- */
- formatArgvs () {
- // argv
- let argvs = [];
- for (let i = 0; i < process.argv.length; i++) {
- const tmpArgv = process.argv[i]
- if (tmpArgv.indexOf('--') !== -1) {
- argvs.push(tmpArgv.substr(2))
- }
- }
- return argvs;
- }
- /**
- * 移除备份
- */
- rmBackup () {
- fs.rmdirSync(this.backupCodeDir, {recursive: true});
- return;
- }
- /**
- * 检查文件是否存在
- */
- fileExist (filePath) {
- try {
- return fs.statSync(filePath).isFile();
- } catch (err) {
- return false;
- }
- };
- mkdir (dirpath, dirname) {
- // 判断是否是第一次调用
- if (typeof dirname === 'undefined') {
- if (fs.existsSync(dirpath)) {
- return;
- }
- this.mkdir(dirpath, path.dirname(dirpath));
- } else {
- // 判断第二个参数是否正常,避免调用时传入错误参数
- if (dirname !== path.dirname(dirpath)) {
- this.mkdir(dirpath);
- return;
- }
- if (fs.existsSync(dirname)) {
- fs.mkdirSync(dirpath);
- } else {
- this.mkdir(dirname, path.dirname(dirname));
- fs.mkdirSync(dirpath);
- }
- }
- };
- chmodPath (path, mode) {
- let files = [];
- if (fs.existsSync(path)) {
- files = fs.readdirSync(path);
- files.forEach((file, index) => {
- const curPath = path + '/' + file;
- if (fs.statSync(curPath).isDirectory()) {
- this.chmodPath(curPath, mode); // 递归删除文件夹
- } else {
- fs.chmodSync(curPath, mode);
- }
- });
- fs.chmodSync(path, mode);
- }
- };
- }
- const cc = new CodeCompress();
- let argvs = cc.formatArgvs();
- console.log('[electron] [code_compress] argvs:', argvs);
- if (argvs.indexOf('compress') != -1) {
- cc.backup();
- cc.compress();
- } else if (argvs.indexOf('restore') != -1) {
- cc.restore();
- }
- module.exports = CodeCompress;
|