const fs = require('fs'); const path = require('fire-path'); const setting = require('./build-setting'); const utils = require('./libs/utils'); module.exports = (function (){ var _instance; function AssetsAndroidPack (params) { return { _ready: false, _dest: null, _temp: null, _list: [], init (actualPlatform) { this._ready = false; let androidPack = `androidPack`; let pack = setting.getInstance().getItem(androidPack); if (!pack) { Editor.warn('没有配置信息'); return false; } if (!pack.enable) { Editor.warn('未开启安卓打包!'); return false; } if (actualPlatform !== 'android') { Editor.warn(actualPlatform, '平台不支持安卓打包'); return false; } if (!pack.dest || !pack.list) { Editor.warn('配置信息不全'); return false; } let destPath = path.join(Editor.Project.path, pack.dest); let stat = fs.statSync(destPath); if (!stat.isDirectory()) { Editor.error('目标根目录不存在'); return false; } let tempPath; if (pack.temp) { let aPath = path.join(Editor.Project.path, pack.temp); let stat = fs.statSync(aPath); if (stat.isDirectory()) { tempPath = aPath; } } if (pack.list.length === 0) { Editor.error('没有需要拷贝的资源'); return false; } this._dest = destPath; this._temp = tempPath; this._list = pack.list; this._ready = true; return true; }, isReady () { return this._ready; }, copyAssets (callback) { for (const aPath of this._list) { let index = aPath.lastIndexOf('/'); let destPath = this._dest + aPath.substr(index); // 临时工程目录 let tempPath; if (this._temp) { tempPath = this._temp + aPath.substr(index); } let srcPath = path.join(Editor.Project.path, aPath); let stat = fs.statSync(srcPath); if (stat.isDirectory()) { if (fs.existsSync(destPath)) { utils.rmdirSync(destPath); } fs.mkdirSync(destPath); utils.copyFolder(srcPath, destPath); // 临时工程目录 if (tempPath) { if (fs.existsSync(tempPath)) { utils.rmdirSync(tempPath); } fs.mkdirSync(tempPath); utils.copyFolder(srcPath, tempPath); } } else { fs.copyFileSync(srcPath, destPath); // 临时工程目录 if (tempPath) { fs.copyFileSync(srcPath, tempPath); } } callback(srcPath); } }, getCopyCount () { return this._list.length; } }; } return { getInstance (params) { if (!_instance){ _instance = AssetsAndroidPack(params); } return _instance; } }; })();