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

Add cmd-line help and reduce node error output by default

Added nopt package dependency - npm update required!

Added -v cmdline option to show node module load errors, which are otherwise hidden by default with only a summary shown
This commit is contained in:
Nick O'Leary 2014-03-06 22:32:23 +00:00
parent 68d5ebf388
commit f7792c66b4
4 changed files with 95 additions and 45 deletions

View File

@ -22,21 +22,22 @@
], ],
"dependencies": { "dependencies": {
"express": "3.x", "express": "3.x",
"when": "~2.6.0", "when": "2.8.0",
"mqtt": "~0.3.3", "nopt": "2.2.0",
"ws": "~0.4.31", "mqtt": "~0.3.7",
"ws": "0.4.31",
"fs-extra": "~0.8.1", "fs-extra": "~0.8.1",
"clone": "~0.1.11", "clone": "0.1.11",
"mustache": "~0.7.2", "mustache": "~0.8.1",
"cron":"1.x", "cron":"~1.0.3",
"raw-body":"~1.1.2", "raw-body":"1.1.2",
"twitter-ng":"~0.6.2", "twitter-ng":"~0.6.2",
"oauth":"~0.9.10", "oauth":"~0.9.11",
"xml2js":"~0.2.8", "xml2js":"~0.2.8",
"sentiment":"~0.2.1", "sentiment":"~0.2.1",
"irc":"~0.3.6", "irc":"~0.3.6",
"follow-redirects":"~0.0.3", "follow-redirects":"~0.0.3",
"cors":"~2.1.1", "cors":"~2.2.0",
"mkdirp":"~0.3.5" "mkdirp":"~0.3.5"
}, },
"devDependencies": { "devDependencies": {

63
red.js
View File

@ -18,6 +18,8 @@ var https = require('https');
var util = require("util"); var util = require("util");
var express = require("express"); var express = require("express");
var crypto = require("crypto"); var crypto = require("crypto");
var nopt = require("nopt");
var path = require("path");
var RED = require("./red/red.js"); var RED = require("./red/red.js");
var server; var server;
@ -26,21 +28,53 @@ var app = express();
var settingsFile = "./settings"; var settingsFile = "./settings";
var flowFile; var flowFile;
for (var argp = 2;argp < process.argv.length;argp+=1) { var knownOpts = {
var v = process.argv[argp]; "settings":[path],
if (v == "--settings" || v == "-s") { "v": Boolean,
if (argp+1 == process.argv.length) { "help": Boolean
console.log("Missing argument to --settings"); };
return; var shortHands = {
} "s":["--settings"],
argp++; "?":["--help"]
settingsFile = process.argv[argp]; };
} else { nopt.invalidHandler = function(k,v,t) {
flowFile = v; // TODO: console.log(k,v,t);
}
} }
var parsedArgs = nopt(knownOpts,shortHands,process.argv,2)
if (parsedArgs.help) {
console.log("Usage: node red.js [-v] [-?] [--settings settings.js] [flows.json]");
console.log("");
console.log("Options:");
console.log(" -s, --settings FILE use specified settings file");
console.log(" -v enable verbose output");
console.log(" -?, --help show usage");
console.log("");
console.log("Documentation can be found at http://nodered.org");
process.exit();
}
if (parsedArgs.argv.remain.length > 0) {
flowFile = parsedArgs.argv.remain[0];
}
if (parsedArgs.settings) {
settingsFile = parsedArgs.settings;
}
try {
var settings = require(settingsFile); var settings = require(settingsFile);
} catch(err) {
if (err.code == 'MODULE_NOT_FOUND') {
console.log("Unable to load settings file "+settingsFile);
} else {
console.log(err);
}
process.exit();
}
if (parsedArgs.v) {
settings.verbose = true;
}
if (settings.https) { if (settings.https) {
server = https.createServer(settings.https,function(req,res){app(req,res);}); server = https.createServer(settings.https,function(req,res){app(req,res);});
@ -102,8 +136,7 @@ if (settings.httpStatic) {
app.use("/",express.static(settings.httpStatic)); app.use("/",express.static(settings.httpStatic));
} }
RED.start(); RED.start().then(function() {
var listenPath = 'http'+(settings.https?'s':'')+'://'+ var listenPath = 'http'+(settings.https?'s':'')+'://'+
(settings.uiHost == '0.0.0.0'?'127.0.0.1':settings.uiHost)+ (settings.uiHost == '0.0.0.0'?'127.0.0.1':settings.uiHost)+
':'+settings.uiPort+settings.httpAdminRoot; ':'+settings.uiPort+settings.httpAdminRoot;
@ -111,6 +144,8 @@ var listenPath = 'http'+(settings.https?'s':'')+'://'+
server.listen(settings.uiPort,settings.uiHost,function() { server.listen(settings.uiPort,settings.uiHost,function() {
util.log('[red] Server now running at '+listenPath); util.log('[red] Server now running at '+listenPath);
}); });
});
process.on('uncaughtException',function(err) { process.on('uncaughtException',function(err) {
if (err.errno === "EADDRINUSE") { if (err.errno === "EADDRINUSE") {

View File

@ -273,6 +273,7 @@ module.exports.load = function(settings) {
} }
function loadNodes(dir) { function loadNodes(dir) {
var errors = [];
fs.readdirSync(dir).sort().filter(function(fn){ fs.readdirSync(dir).sort().filter(function(fn){
var stats = fs.statSync(path.join(dir,fn)); var stats = fs.statSync(path.join(dir,fn));
if (stats.isFile()) { if (stats.isFile()) {
@ -280,26 +281,30 @@ module.exports.load = function(settings) {
try { try {
require(path.join(dir,fn)); require(path.join(dir,fn));
} catch(err) { } catch(err) {
util.log("["+fn+"] "+err); errors.push({fn:fn, err:err});
//util.log("["+fn+"] "+err);
//console.log(err.stack); //console.log(err.stack);
} }
} }
} else if (stats.isDirectory()) { } else if (stats.isDirectory()) {
// Ignore /.dirs/, /lib/ /node_modules/ // Ignore /.dirs/, /lib/ /node_modules/
if (!/^(\..*|lib|icons|node_modules)$/.test(fn)) { if (!/^(\..*|lib|icons|node_modules)$/.test(fn)) {
loadNodes(path.join(dir,fn)); errors = errors.concat(loadNodes(path.join(dir,fn)));
} else if (fn === "icons") { } else if (fn === "icons") {
events.emit("node-icon-dir",path.join(dir,fn)); events.emit("node-icon-dir",path.join(dir,fn));
} }
} }
}); });
return errors;
} }
loadNodes(__dirname+"/../nodes"); var errors = loadNodes(__dirname+"/../nodes");
scanForNodes(__dirname+"/../nodes"); scanForNodes(__dirname+"/../nodes");
if (settings.nodesDir) { if (settings.nodesDir) {
loadNodes(settings.nodesDir); errors = errors.concat(loadNodes(settings.nodesDir));
scanForNodes(settings.nodesDir); scanForNodes(settings.nodesDir);
} }
//console.log(errors);
return errors;
//events.emit("nodes-loaded"); //events.emit("nodes-loaded");
} }

View File

@ -16,6 +16,8 @@
var express = require('express'); var express = require('express');
var util = require('util'); var util = require('util');
var when = require('when');
var createUI = require("./ui"); var createUI = require("./ui");
var redNodes = require("./nodes"); var redNodes = require("./nodes");
@ -71,20 +73,25 @@ function createServer(_server,_settings) {
} }
function start() { function start() {
var defer = when.defer();
storage.init(settings).then(function() { storage.init(settings).then(function() {
console.log("\nWelcome to Node-RED\n===================\n"); console.log("\nWelcome to Node-RED\n===================\n");
util.log("[red] Loading palette nodes"); util.log("[red] Loading palette nodes");
var nodeErrors = redNodes.load(settings);
if (nodeErrors.length > 0) {
util.log("------------------------------------------"); util.log("------------------------------------------");
redNodes.load(settings); if (settings.verbose) {
util.log(""); for (var i=0;i<nodeErrors.length;i+=1) {
util.log('You may ignore any errors above here if they are for'); util.log("["+nodeErrors[i].fn+"] "+nodeErrors[i].err);
util.log('nodes you are not using. The nodes indicated will not'); }
util.log('be available in the main palette until any missing'); } else {
util.log('modules are installed, typically by running:'); util.log("[red] Failed to register "+nodeErrors.length+" node type"+(nodeErrors.length==1?"":"s"));
util.log(' npm install {the module name}'); util.log("[red] Run with -v for details");
util.log('or any other errors are resolved'); }
util.log("------------------------------------------"); util.log("------------------------------------------");
}
defer.resolve();
storage.getFlows().then(function(flows) { storage.getFlows().then(function(flows) {
if (flows.length > 0) { if (flows.length > 0) {
redNodes.setConfig(flows); redNodes.setConfig(flows);
@ -93,6 +100,8 @@ function start() {
util.log("[red] Error loading flows : "+err); util.log("[red] Error loading flows : "+err);
}); });
}); });
return defer.promise;
} }
function stop() { function stop() {