CfgUtil.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. let fs = require('fire-fs');
  2. let path = require('fire-path');
  3. let electron = require('electron');
  4. module.exports = {
  5. cfgData: {
  6. excelRootPath: null,// excel根路径
  7. jsFileName: null,
  8. jsonAllFileName: null,
  9. isMergeJson: null,
  10. isFormatJsCode: null,
  11. isFormatJson: null,
  12. },
  13. initCfg(cb) {
  14. let configFilePath = this._getAppCfgPath();
  15. let b = fs.existsSync(configFilePath);
  16. if (b) {
  17. console.log("cfg path: " + configFilePath);
  18. fs.readFile(configFilePath, 'utf-8', function (err, data) {
  19. if (!err) {
  20. let saveData = JSON.parse(data.toString());
  21. self.cfgData = saveData;
  22. if (cb) {
  23. cb(saveData);
  24. }
  25. }
  26. }.bind(self));
  27. } else {
  28. if (cb) {
  29. cb(null);
  30. }
  31. }
  32. },
  33. saveCfgByData(data) {
  34. this.cfgData.excelRootPath = data.excelRootPath;
  35. this.cfgData.jsFileName = data.jsFileName;
  36. this.cfgData.jsonAllFileName = data.jsonAllFileName;
  37. this.cfgData.isMergeJson = data.isMergeJson;
  38. this.cfgData.isFormatJsCode = data.isFormatJsCode;
  39. this.cfgData.isFormatJson = data.isFormatJson;
  40. this._save();
  41. },
  42. _save() {
  43. let savePath = this._getAppCfgPath();
  44. fs.writeFileSync(savePath, JSON.stringify(this.cfgData));
  45. console.log("save ok!");
  46. },
  47. _getAppCfgPath() {
  48. let userDataPath = null;
  49. if (electron.remote) {
  50. userDataPath = electron.remote.app.getPath('userData');
  51. } else {
  52. userDataPath = electron.app.getPath('userData');
  53. }
  54. let tar = Editor.libraryPath;
  55. tar = tar.replace(/\\/g, '-');
  56. tar = tar.replace(/:/g, '-');
  57. tar = tar.replace(/\//g, '-');
  58. return path.join(userDataPath, "excel-fucker-" + tar + ".json");
  59. },
  60. };