From cc45413e2d3cf45ec34c088e6deae7c26adebb1a Mon Sep 17 00:00:00 2001 From: Koen Kanters Date: Mon, 18 Feb 2019 19:46:19 +0100 Subject: [PATCH] Add settings.set api. --- lib/util/settings.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/lib/util/settings.js b/lib/util/settings.js index 6c70984c..7d16ae1d 100644 --- a/lib/util/settings.js +++ b/lib/util/settings.js @@ -75,6 +75,25 @@ function read() { return yaml.safeLoad(fs.readFileSync(file, 'utf8')); } +function set(path, value) { + let obj = settings; + + for (let i = 0; i < path.length; i++) { + const key = path[i]; + if (i === path.length - 1) { + obj[key] = value; + } else { + if (!obj[key]) { + obj[key] = {}; + } + + obj = obj[key]; + } + } + + writeRead(); +} + function addDevice(ieeeAddr) { if (!settings.devices) { settings.devices = {}; @@ -126,6 +145,7 @@ function changeFriendlyName(old, new_) { module.exports = { get: () => objectAssignDeep.noMutate(defaults, settings), write: () => write(), + set: (path, value) => set(path, value), getDevice: (ieeeAddr) => settings.devices ? settings.devices[ieeeAddr] : null, getGroup: (ID) => settings.groups ? settings.groups[ID]: null,