修复config.json可能报错

This commit is contained in:
ckcz123 2020-10-21 11:49:42 +08:00
parent 3ba9be2925
commit 08c632a5c5

View File

@ -11,8 +11,14 @@ editor_config.prototype.load = function(callback) {
_this.config = {}; _this.config = {};
_this.save(callback); _this.save(callback);
} else { } else {
try {
_this.config = JSON.parse(d); _this.config = JSON.parse(d);
if (callback) callback(); if (callback) callback();
} catch (e) {
console.error(e);
_this.config = {};
_this.save(callback);
}
} }
}); });
} }
@ -32,11 +38,12 @@ editor_config.prototype.save = function(callback) {
if (this._isWriting) return; if (this._isWriting) return;
try { try {
this._isWriting = true; this._isWriting = true;
fs.writeFile(this.address, JSON.stringify(this.config) ,'utf-8', (function(e) { var _this = this;
this._isWriting = false; fs.writeFile(this.address, JSON.stringify(this.config) ,'utf-8', function(e) {
_this._isWriting = false;
if (e) console.error("写入配置文件失败"); if (e) console.error("写入配置文件失败");
if (callback instanceof Function) callback(); if (callback instanceof Function) callback();
}).bind(this)) })
} catch (e) { } catch (e) {
this._isWriting = false; this._isWriting = false;
console.error(e); console.error(e);