const fs = require('fire-fs'); const path = require('fire-path'); module.exports = (function (){ var _instance; function init (params) { return { _config: {}, _versionConfig: {}, getConfig () { return this._config; }, setConfig (config) { this._config = config; }, getConfigFilePath () { return path.join(Editor.Project.path, 'settings', 'buildhelper.json'); }, loadJsonFile (filePath) { let obj = null; try { obj = JSON.parse(fs.readFileSync(filePath, 'utf8')); } catch (err) { Editor.log(err); obj = {}; } return obj; }, saveJsonFile (filePath, data) { const dirPath = path.dirname(filePath); fs.ensureDir(dirPath, function () { fs.writeFileSync(filePath, data); }); }, load () { let filePath = this.getConfigFilePath(); this._config = this.loadJsonFile(filePath); }, save () { let filePath = this.getConfigFilePath(); this.saveJsonFile(filePath, JSON.stringify(this._config, '', '\t')); }, getItem (key) { return this._config[key]; }, setItem (key, value) { this._config[key] = value; }, loadVersionConfig () { Editor.Scene.callSceneScript('build-helper', 'getVersionConfig', (err, ns) => { if (err) { Editor.error('编译助手(build-helper)获取版本信息失败!'); Editor.log('编译助手(build-helper)将重新获取版本信息!'); setTimeout(()=> { this.loadVersionConfig(); }, 1000); return; } this.setVersionConfig(ns); Editor.log('编译助手(build-helper)获取版本信息成功!', ns); }); }, setVersionConfig (cfg) { this._versionConfig = cfg; }, getVersionConfig () { return this._versionConfig; } }; } return { getInstance (params) { if (!_instance){ _instance = init(params); } return _instance; } }; })();