mirror of
				https://github.com/node-red/node-red.git
				synced 2025-03-01 10:36:34 +00:00 
			
		
		
		
	update for merging settings object & better error handling
This commit is contained in:
		
							
								
								
									
										46
									
								
								packages/node_modules/node-red/red.js
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										46
									
								
								packages/node_modules/node-red/red.js
									
									
									
									
										vendored
									
									
								
							| @@ -60,27 +60,31 @@ 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] = {}; | ||||
|             } | ||||
|             o = o[path]; | ||||
| function mergeObject(obj0, obj1) { | ||||
|     function isObject(o) { | ||||
|         function typeOf(o) { | ||||
|             var toString = Object.prototype.toString; | ||||
|             return toString.call(o).slice(8, -1); | ||||
|         } | ||||
|         var key = paths[paths.length-1]; | ||||
|         o[key] = val; | ||||
|         return (typeOf(o) == "Object"); | ||||
|     } | ||||
|     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()); | ||||
|     console.log("Usage: node-red [-v] [-?] [--settings settings.js] [--userDir DIR]"); | ||||
| @@ -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(); | ||||
|         } | ||||
|     }); | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user