mirror of
https://github.com/mtan93/homebridge.git
synced 2026-03-08 05:31:55 +00:00
* 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
49 lines
1.2 KiB
JavaScript
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));
|
|
} |