1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

update for merging settings object & better error handling

This commit is contained in:
Hiroyasu Nishiyama 2020-03-07 19:19:48 +09:00
parent c0d007ffa9
commit 82539fc420

View File

@ -60,26 +60,30 @@ nopt.invalidHandler = function(k,v,t) {
var parsedArgs = nopt(knownOpts,shortHands,process.argv,2)
/**
* Set property of specified object.
* @param {Object} obj - target object
* @param {string} path - "." separated property path
* @param {Object} val - value to be set
* Marge values in second object into first object.
*
* @param {Object} obj0 - target object
* @param {Object} obj1 - object to be merged
*/
function setProperty(obj, path, val) {
var paths = path.split(".");
if (paths.length > 0) {
var o = obj;
for (var i = 0; i < paths.length -1; i++) {
var path = paths[i];
if (!o.hasOwnProperty(path)) {
o[path] = {};
function mergeObject(obj0, obj1) {
function isObject(o) {
function typeOf(o) {
var toString = Object.prototype.toString;
return toString.call(o).slice(8, -1);
}
o = o[path];
return (typeOf(o) == "Object");
}
var key = paths[paths.length-1];
o[key] = val;
Object.entries(obj1).forEach(([key, val]) => {
if (obj0.hasOwnProperty(key) && isObject(obj0[key]) && isObject(val)) {
mergeObject(obj0[key], val);
}
else {
obj0[key] = val;
}
})
return obj0;
}
if (parsedArgs.help) {
console.log("Node-RED v"+RED.version());
@ -163,21 +167,21 @@ if (parsedArgs.define) {
if (match) {
if (!match[4]) {
var val = JSON.parse(match[3]);
setProperty(settings, match[2], val);
RED.util.setObjectProperty(settings, match[2], val, true);
}
else {
var obj = fs.readJsonSync(match[4]);
Object.entries(obj).forEach(([key, val]) => {
settings[key] = val;
});
mergeObject(settings, obj);
}
}
else {
throw new Error("Unexpected option: "+def);
throw new Error("unexpected syntax");
}
}
catch (e) {
console.log("Error processing option: "+def);
console.log(e);
process.exit();
}
});
}