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

add option support for overwriting settiings.js

This commit is contained in:
Hiroyasu Nishiyama 2020-02-16 23:07:05 +09:00
parent 4f9395e881
commit c0d007ffa9

View File

@ -39,7 +39,8 @@ var knownOpts = {
"title": String,
"userDir": [path],
"verbose": Boolean,
"safe": Boolean
"safe": Boolean,
"define": [String, Array]
};
var shortHands = {
"?":["--help"],
@ -49,7 +50,8 @@ var shortHands = {
// doesn't get treated as --title
"t":["--help"],
"u":["--userDir"],
"v":["--verbose"]
"v":["--verbose"],
"D":["--define"]
};
nopt.invalidHandler = function(k,v,t) {
// TODO: console.log(k,v,t);
@ -57,6 +59,28 @@ 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
*/
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];
}
var key = paths[paths.length-1];
o[key] = val;
}
}
if (parsedArgs.help) {
console.log("Node-RED v"+RED.version());
console.log("Usage: node-red [-v] [-?] [--settings settings.js] [--userDir DIR]");
@ -69,6 +93,7 @@ if (parsedArgs.help) {
console.log(" -u, --userDir DIR use specified user directory");
console.log(" -v, --verbose enable verbose output");
console.log(" --safe enable safe mode");
console.log(" -D, --define X=Y overwrite value in settings file");
console.log(" -?, --help show this help");
console.log("");
console.log("Documentation can be found at http://nodered.org");
@ -130,6 +155,33 @@ try {
process.exit();
}
if (parsedArgs.define) {
var defs = parsedArgs.define;
defs.forEach(function (def) {
try {
var match = /^(([^=]+)=(.+)|@(.*))$/.exec(def);
if (match) {
if (!match[4]) {
var val = JSON.parse(match[3]);
setProperty(settings, match[2], val);
}
else {
var obj = fs.readJsonSync(match[4]);
Object.entries(obj).forEach(([key, val]) => {
settings[key] = val;
});
}
}
else {
throw new Error("Unexpected option: "+def);
}
}
catch (e) {
console.log(e);
}
});
}
if (parsedArgs.verbose) {
settings.verbose = true;
}