CfgUtil.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. var FS = require('fire-fs');
  2. let configFileUrl = 'packages://selection-resources/config.json';
  3. let defaultConfig = {
  4. OldVersionPath:"",
  5. NewVersionPath:"",
  6. FinalPath:"",
  7. };
  8. let self = module.exports = {
  9. cfgData: {
  10. OldVersionPath:"",
  11. NewVersionPath:"",
  12. FinalPath:"",
  13. },
  14. commSet(key,value){
  15. this.cfgData[key] = value;
  16. this.saveConfig();
  17. },
  18. saveConfig() {
  19. let filePathUrl = Editor.url(configFileUrl);
  20. FS.writeFile(filePathUrl, JSON.stringify(this.cfgData), function (error) {
  21. if (!error) {
  22. Editor.log("保存配置成功");
  23. }
  24. }.bind(this));
  25. },
  26. initCfg(cb) {
  27. let filePathUrl = Editor.url(configFileUrl);
  28. if (FS.existsSync(filePathUrl)) {
  29. FS.readFile(filePathUrl, 'utf-8', function (err, data) {
  30. if (!err) {
  31. let saveData = JSON.parse(data.toString());
  32. self.cfgData = saveData;
  33. if (cb) {
  34. cb(saveData);
  35. }
  36. }
  37. }.bind(self));
  38. } else {
  39. // 写入配置
  40. let stringData = JSON.stringify(defaultConfig, null, '\t')
  41. FS.writeFileSync(filePathUrl, stringData);
  42. Editor.log("配置不存在文件");
  43. if (cb) {
  44. cb(defaultConfig);
  45. }
  46. }
  47. }
  48. };