electron-kit
Version:
Utility kits for middle-scale electron app
229 lines (182 loc) • 5.95 kB
JavaScript
// Generated by CoffeeScript 1.10.0
(function() {
var ConfigManager, Emitter, _, __, deepDelete, fs, path;
_ = require("lodash");
__ = require("lodash-deep");
path = require("path");
fs = require("fs-plus");
Emitter = require("../utils/Emitter");
deepDelete = require("../utils/deepDelete");
module.exports = ConfigManager = (function() {
function ConfigManager(options) {
var configDirPath, configFileName, jsonIndent, saveThrottleMs;
if (options == null) {
options = {};
}
configDirPath = options.configDirPath, configFileName = options.configFileName, jsonIndent = options.jsonIndent, saveThrottleMs = options.saveThrottleMs;
this._configDirPath = configDirPath;
this._configFileName = configFileName;
this._jsonIndent = jsonIndent;
this._saveThrottleMs = saveThrottleMs;
if (this._configFileName == null) {
this._configFileName = "config.json";
}
if (this._saveThrottleMs == null) {
this._saveThrottleMs = 200;
}
if (this._jsonIndent == null) {
this._jsonIndent = " ";
}
this._emitter = new Emitter;
this._config = {};
this._configFilePath = path.join(this._configDirPath, this._configFileName);
this.save = _.throttle(this.save.bind(this), this._saveThrottleMs);
}
/**
* Initialize configure save directory
* @protected
*/
ConfigManager.prototype._initializeConfigDir = function(force) {
if (force == null) {
force = false;
}
if (fs.existsSync(this._configDirPath) === false || force) {
fs.makeTreeSync(this._configDirPath);
}
if (fs.existsSync(this._configFilePath) === false || force) {
fs.writeFileSync(this._configFilePath, "{}");
}
this._emitter.emit("did-init-config-directory");
this._initializeConfigDir = function() {};
};
/**
* Load config file
*/
ConfigManager.prototype.load = function() {
var rawJSON;
this._initializeConfigDir();
rawJSON = fs.readFileSync(this._configFilePath, {
encoding: "utf8"
});
this._config = JSON.parse(rawJSON);
this._emitter.emit("did-load-config-file");
};
/**
* Save current config to config file
*/
ConfigManager.prototype.save = function() {
var stringedJSON;
this._initializeConfigDir();
stringedJSON = JSON.stringify(this._config, null, this._jsonIndent);
fs.writeFileSync(this._configFilePath, stringedJSON, {
encoding: "utf8"
});
this._emitter.emit("did-save-config-file");
};
/**
* Set config value
* @param {String} keyPath Config key name (accept dot delimited key)
*/
ConfigManager.prototype.set = function(keyPath, value) {
var oldValue;
oldValue = this.get(keyPath);
if (_.isEqual(oldValue, value)) {
return;
}
__.deepSet(this._config, keyPath, value);
this._emitter.emit("did-change", {
key: keyPath,
newValue: value,
oldValue: oldValue
});
this.save();
};
/**
* Get configured value
* @param {String} keyPath Config key name (accept dot delimited key)
*/
ConfigManager.prototype.get = function(keyPath, defaultValue) {
var value;
value = __.deepGet(this._config, keyPath);
if (value === void 0) {
return defaultValue;
} else {
return value;
}
};
/**
* Unset config value
* @param {String} keyPath Config key name (accept dot delimited key)
*/
ConfigManager.prototype["delete"] = function(keyPath) {
var oldValue;
oldValue = this.get(keyPath);
deepDelete(this._config, keyPath);
this._emitter.emit("did-change", {
key: keyPath,
newValue: void 0,
oldValue: oldValue,
deleted: true
});
this.save();
};
/**
* Observe specified configure changed
* @param {String} keyPath Observing config key name (accept dot delimited key)
* @param {Function} observer callback function
*/
ConfigManager.prototype.observe = function(keyPath, observer) {
var oldValue;
if (keyPath == null) {
keyPath = null;
}
oldValue = this.get(keyPath);
return this.onDidChange((function(_this) {
return function() {
var newValue;
newValue = _this.get(keyPath);
if (!_.isEqual(newValue, oldValue)) {
observer(newValue, oldValue);
}
return oldValue = newValue;
};
})(this));
};
/**
* Unobserve specified configured change observer
* @param {String} keyPath Observing config key name (accept dot delimited key)
* @param {Function} observer callback function
*/
ConfigManager.prototype.unobserve = function(keyPath, observer) {
if (keyPath == null) {
keyPath = null;
}
return this.off("did-change", observer);
};
/**
* @param {Function} fn callback
*/
ConfigManager.prototype.onDidLoadConfigFile = function(fn) {
return this._emitter.on("did-load-config-file");
};
/**
* @param {Function} fn callback
*/
ConfigManager.prototype.onDidSaveConfigFile = function(fn) {
return this._emitter.on("did-save-config-file");
};
/**
* @param {Function} fn callback
*/
ConfigManager.prototype.onDidChange = function(fn) {
return this._emitter.on("did-change", fn);
};
/**
* @param {Function} fn callback
*/
ConfigManager.prototype.onDidInitializeConfigDirectory = function(fn) {
return this._emitter.on("did-init-config-directory");
};
return ConfigManager;
})();
}).call(this);