mirror of
https://github.com/mtan93/homebridge.git
synced 2026-03-20 05:10:56 +00:00
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
This commit is contained in:
@@ -1,40 +1,49 @@
|
||||
import fs from 'fs';
|
||||
var fs = require('fs');
|
||||
|
||||
export class Config {
|
||||
|
||||
constructor(path, data = {}) {
|
||||
this.path = path;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
get(key) {
|
||||
this.validateKey(key);
|
||||
let [providerName, keyName] = key.split(".");
|
||||
return this.data[providerName] && this.data[providerName][keyName];
|
||||
}
|
||||
|
||||
set(key, value) {
|
||||
this.validateKey(key);
|
||||
let [providerName, keyName] = key.split(".");
|
||||
this.data[providerName] = this.data[providerName] || {};
|
||||
this.data[providerName][keyName] = value;
|
||||
this.save();
|
||||
}
|
||||
|
||||
validateKey(key) {
|
||||
if (key.split(".").length != 2)
|
||||
throw new Error(`The config key '${key}' is invalid. Configuration keys must be in the form [my-provider].[myKey]`);
|
||||
}
|
||||
|
||||
static load(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
|
||||
}
|
||||
|
||||
save() {
|
||||
fs.writeFileSync(this.path, JSON.stringify(this.data, null, 2));
|
||||
}
|
||||
'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));
|
||||
}
|
||||
Reference in New Issue
Block a user