Files
homebridge/lib/config.js
Nick Farina dbedf7fe01 Convert to ES5, add web server
* No compilation step
 * Beginnings of web interface
 * Simple express server; React-based frontend
 * CommonJS style across codebase; auto-converts to RequireJS for browser
 * Using diffsync for realtime UI
 * "Provider" -> "Plugin"
 * Plugins expose one or more Providers
2015-08-11 22:27:59 -07:00

49 lines
1.2 KiB
JavaScript

var fs = require('fs');
'use strict';
module.exports = {
Config: Config
}
/**
* API for plugins to manage their own configuration settings
*/
function Config(path, data) {
this.path = path;
this.data = data || {};
}
Config.prototype.get = function(key) {
this._validateKey(key);
var pluginName = key.split('.')[0];
var keyName = key.split('.')[1];
return this.data[pluginName] && this.data[pluginName][keyName];
}
Config.prototype.set = function(key, value) {
this._validateKey(key);
var pluginName = key.split('.')[0];
var keyName = key.split('.')[1];
this.data[pluginName] = this.data[pluginName] || {};
this.data[pluginName][keyName] = value;
this.save();
}
Config.prototype._validateKey = function(key) {
if (key.split(".").length != 2)
throw new Error("The config key '" + key + "' is invalid. Configuration keys must be in the form [my-plugin].[myKey]");
}
Config.load = function(configPath) {
// load up the previous config if found
if (fs.existsSync(configPath))
return new Config(configPath, JSON.parse(fs.readFileSync(configPath)));
else
return new Config(configPath); // empty initial config
}
Config.prototype.save = function() {
fs.writeFileSync(this.path, JSON.stringify(this.data, null, 2));
}