const fs = require('fs'); const path = require('fire-path'); const utils = require('./libs/utils'); const setting = require('./build-setting'); var child_process = require('child_process'); module.exports = (function (){ var _instance; function AssetsCompress (params) { return { _ready: false, _quality: null, _extList: [], init (actualPlatform) { this._ready = false; let assetsCompress = `assetsCompress`; let compress = setting.getInstance().getItem(assetsCompress); if (!compress) { Editor.warn('缺少配置,跳过资源压缩!'); return false; } if (!compress.enable) { Editor.warn('未开启资源压缩!'); return false; } // 默认全平台支持 let platform = compress.platform; // 平台配置 if (platform) { let supportList = platform.split(','); if (!supportList.includes(actualPlatform)) { Editor.warn(actualPlatform, '平台不支持资源压缩'); return false; } } this._quality = compress.quality || '65-80'; let exts = compress.exts || 'png'; this._extList = exts.split(','); this._ignoreFileUuids = []; let ignores = compress.ignores; if (ignores) { for (const aPath of ignores) { let fullpath = path.join(Editor.Project.path, aPath); let stat = fs.statSync(fullpath); if (stat.isDirectory()) { utils.findFileSync(fullpath, (filePath)=> { this.addIgnoreFile(filePath); }); } else if (stat.isFile()) { this.addIgnoreFile(fullpath); } } } this._ready = true; return true; }, isReady () { return this._ready; }, getAssetsdbPath (root) { let assetsdb = 'db://'; let assetsIdx = root.indexOf('assets'); if (assetsIdx !== -1) { assetsdb += root.substr(assetsIdx, root.length); } else { assetsdb += '/assets'; } return assetsdb; }, addIgnoreFile (filePath) { let index = filePath.lastIndexOf('.'); let ext = filePath.substr(index + 1); if (ext !== 'meta') { let uuid = Editor.assetdb._path2uuid[filePath]; this._ignoreFileUuids.push(uuid); } }, addCompressFile (filePath) { for (const uuid of this._ignoreFileUuids) { let include = filePath.includes(uuid); if (include) { return; } } let index = filePath.lastIndexOf('.'); let ext = filePath.substr(index + 1); if (this._extList.includes(ext)) { this._files.push(filePath); } }, genFiles (buildDest) { this._files = []; let rootPath = path.join(buildDest, 'res/raw-assets'); let stat = fs.statSync(rootPath); if (stat.isDirectory()) { utils.findFileSync(rootPath, (filePath)=> { this.addCompressFile(filePath); }); // let dirs = fs.readdirSync(rootPath); // dirs.forEach((ele, index) => { // let fullpath = path.join(rootPath, ele); // let info = fs.statSync(fullpath); // if (info.isDirectory()) { // utils.findFileSync(fullpath, (filePath)=> { // this.addCompressFile(filePath); // }); // } else { // this.addCompressFile(fullpath); // } // }); } }, compress (callback) { let pngquant = Editor.url('packages://build-helper/tools/pngquant/pngquant'); this._files.forEach((filePath, index) => { let cmd = pngquant + ' --quality=' + this._quality + ' --force 256 --skip-if-larger ' + filePath + ' -o ' + filePath; child_process.exec(cmd, {timeout: 3654321}, function (error, stdout, stderr) { if (stderr) { Editor.error('压缩' + filePath + '失败'); return; } callback (filePath); }); }); }, getFilesCount () { return this._files.length; } }; } return { getInstance (params) { if (!_instance){ _instance = AssetsCompress(params); } return _instance; } }; })();