build-setting.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. const fs = require('fire-fs');
  2. const path = require('fire-path');
  3. module.exports = (function (){
  4. var _instance;
  5. function init (params) {
  6. return {
  7. _config: {},
  8. _versionConfig: {},
  9. getConfig () {
  10. return this._config;
  11. },
  12. setConfig (config) {
  13. this._config = config;
  14. },
  15. getConfigFilePath () {
  16. return path.join(Editor.Project.path, 'settings', 'buildhelper.json');
  17. },
  18. loadJsonFile (filePath) {
  19. let obj = null;
  20. try {
  21. obj = JSON.parse(fs.readFileSync(filePath, 'utf8'));
  22. } catch (err) {
  23. Editor.log(err);
  24. obj = {};
  25. }
  26. return obj;
  27. },
  28. saveJsonFile (filePath, data) {
  29. const dirPath = path.dirname(filePath);
  30. fs.ensureDir(dirPath, function () {
  31. fs.writeFileSync(filePath, data);
  32. });
  33. },
  34. load () {
  35. let filePath = this.getConfigFilePath();
  36. this._config = this.loadJsonFile(filePath);
  37. },
  38. save () {
  39. let filePath = this.getConfigFilePath();
  40. this.saveJsonFile(filePath, JSON.stringify(this._config, '', '\t'));
  41. },
  42. getItem (key) {
  43. return this._config[key];
  44. },
  45. setItem (key, value) {
  46. this._config[key] = value;
  47. },
  48. loadVersionConfig () {
  49. Editor.Scene.callSceneScript('build-helper', 'getVersionConfig', (err, ns) => {
  50. if (err) {
  51. Editor.error('编译助手(build-helper)获取版本信息失败!');
  52. Editor.log('编译助手(build-helper)将重新获取版本信息!');
  53. setTimeout(()=> {
  54. this.loadVersionConfig();
  55. }, 1000);
  56. return;
  57. }
  58. this.setVersionConfig(ns);
  59. Editor.log('编译助手(build-helper)获取版本信息成功!', ns);
  60. });
  61. },
  62. setVersionConfig (cfg) {
  63. this._versionConfig = cfg;
  64. },
  65. getVersionConfig () {
  66. return this._versionConfig;
  67. }
  68. };
  69. }
  70. return {
  71. getInstance (params) {
  72. if (!_instance){
  73. _instance = init(params);
  74. }
  75. return _instance;
  76. }
  77. };
  78. })();