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

Change default data dir

Changes the default location for user data to $HOME/.node-red.
This commit is contained in:
Nick O'Leary 2015-02-25 14:23:59 +00:00
parent 54b0debb3b
commit ce1cd1ab9c
6 changed files with 161 additions and 88 deletions

34
red.js Normal file → Executable file
View File

@ -1,5 +1,6 @@
#!/usr/bin/env node
/**
* Copyright 2013 IBM Corp.
* Copyright 2013, 2015 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -20,21 +21,24 @@ var express = require("express");
var crypto = require("crypto");
var nopt = require("nopt");
var path = require("path");
var fs = require("fs");
var RED = require("./red/red.js");
var server;
var app = express();
var settingsFile = "./settings";
var settingsFile;
var flowFile;
var knownOpts = {
"settings":[path],
"userDir":[path],
"v": Boolean,
"help": Boolean
};
var shortHands = {
"s":["--settings"],
"u":["--userDir"],
"?":["--help"]
};
nopt.invalidHandler = function(k,v,t) {
@ -45,10 +49,11 @@ var parsedArgs = nopt(knownOpts,shortHands,process.argv,2)
if (parsedArgs.help) {
console.log("Node-RED v"+RED.version());
console.log("Usage: node red.js [-v] [-?] [--settings settings.js] [flows.json]");
console.log("Usage: node red.js [-v] [-?] [--settings settings.js] [--userDir DIR] [flows.json]");
console.log("");
console.log("Options:");
console.log(" -s, --settings FILE use specified settings file");
console.log(" -u, --userDir DIR use specified user directory");
console.log(" -v enable verbose output");
console.log(" -?, --help show usage");
console.log("");
@ -60,13 +65,27 @@ if (parsedArgs.argv.remain.length > 0) {
}
if (parsedArgs.settings) {
// User-specified settings file
settingsFile = parsedArgs.settings;
} else if (parsedArgs.userDir && fs.existsSync(path.join(parsedArgs.userDir,"settings.js"))) {
// User-specified userDir that contains a settings.js
settingsFile = path.join(parsedArgs.userDir,"settings.js");
} else {
var userSettingsFile = path.join(process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE,".node-red","settings.js");
if (fs.existsSync(userSettingsFile)) {
// $HOME/.node-red/settings.js exists
settingsFile = userSettingsFile;
} else {
// Use default settings.js
settingsFile = "./settings";
}
}
try {
var settings = require(settingsFile);
} catch(err) {
if (err.code == 'MODULE_NOT_FOUND') {
console.log("Unable to load settings file "+settingsFile);
console.log("Unable to load settings file: "+settingsFile);
} else {
console.log(err);
}
@ -117,7 +136,12 @@ if (settings.httpNodeRoot !== false) {
settings.uiPort = settings.uiPort||1880;
settings.uiHost = settings.uiHost||"0.0.0.0";
settings.flowFile = flowFile || settings.flowFile;
if (flowFile) {
settings.flowFile = flowFile;
}
if (parsedArgs.userDir) {
settings.userDir = parsedArgs.userDir;
}
RED.init(server,settings);

View File

@ -504,28 +504,19 @@ function getNodeFiles(dir) {
return result;
}
/**
* Scans the node_modules path for nodes
* @param moduleName the name of the module to be found
* @return a list of node modules: {dir,package}
*/
function scanTreeForNodesModules(moduleName) {
var dir = __dirname+"/../../nodes";
function scanDirForNodesModules(dir,moduleName) {
var results = [];
var up = path.resolve(path.join(dir,".."));
while (up !== dir) {
var pm = path.join(dir,"node_modules");
try {
var files = fs.readdirSync(pm);
var files = fs.readdirSync(dir);
for (var i=0;i<files.length;i++) {
var fn = files[i];
if (!registry.getNodeModuleInfo(fn)) {
if (!moduleName || fn == moduleName) {
var pkgfn = path.join(pm,fn,"package.json");
var pkgfn = path.join(dir,fn,"package.json");
try {
var pkg = require(pkgfn);
if (pkg['node-red']) {
var moduleDir = path.join(pm,fn);
var moduleDir = path.join(dir,fn);
results.push({dir:moduleDir,package:pkg});
}
} catch(err) {
@ -541,7 +532,30 @@ function scanTreeForNodesModules(moduleName) {
}
} catch(err) {
}
return results;
}
/**
* Scans the node_modules path for nodes
* @param moduleName the name of the module to be found
* @return a list of node modules: {dir,package}
*/
function scanTreeForNodesModules(moduleName) {
var dir = __dirname+"/../../nodes";
var results = [];
var userDir;
if (settings.userDir) {
userDir = path.join(settings.userDir,"node_modules");
results = results.concat(scanDirForNodesModules(userDir,moduleName));
}
var up = path.resolve(path.join(dir,".."));
while (up !== dir) {
var pm = path.join(dir,"node_modules");
if (pm != userDir) {
results = results.concat(scanDirForNodesModules(pm,moduleName));
}
dir = up;
up = path.resolve(path.join(dir,".."));
}
@ -661,14 +675,19 @@ function load(defaultNodesDir,disableNodePathScan) {
return when.promise(function(resolve,reject) {
// Find all of the nodes to load
var nodeFiles;
var dir;
if(defaultNodesDir) {
nodeFiles = getNodeFiles(path.resolve(defaultNodesDir));
} else {
nodeFiles = getNodeFiles(__dirname+"/../../nodes");
}
if (settings.userDir) {
dir = path.join(settings.userDir,"nodes");
nodeFiles = nodeFiles.concat(getNodeFiles(dir));
}
if (settings.nodesDir) {
var dir = settings.nodesDir;
dir = settings.nodesDir;
if (typeof settings.nodesDir == "string") {
dir = [dir];
}

View File

@ -17,6 +17,8 @@
var express = require('express');
var when = require('when');
var child_process = require('child_process');
var path = require("path");
var fs = require("fs");
var redNodes = require("./nodes");
var comms = require("./comms");
@ -141,7 +143,7 @@ function reportRemovedModules(removedNodes) {
log.info("Removed node types:");
for (var j=0;j<removedNodes.length;j++) {
for (var i=0;i<removedNodes[j].types.length;i++) {
log.info(" - "+(removedNodes[i].module?removedNodes[i].module+":":"")+removedNodes[j].types[i]);
log.info(" - "+(removedNodes[j].module?removedNodes[j].module+":":"")+removedNodes[j].types[i]);
}
}
return removedNodes;
@ -155,7 +157,12 @@ function installModule(module) {
return;
}
log.info("Installing module: "+module);
var child = child_process.exec('npm install --production '+module, function(err, stdin, stdout) {
var installDir = settings.userDir || process.env.NODE_RED_HOME || ".";
var child = child_process.exec('npm install --production '+module,
{
cwd: installDir
},
function(err, stdin, stdout) {
if (err) {
var lookFor404 = new RegExp(" 404 .*"+module+"$","m");
if (lookFor404.test(stdout)) {
@ -174,7 +181,8 @@ function installModule(module) {
log.info("Installed module: "+module);
resolve(redNodes.addModule(module).then(reportAddedModules));
}
});
}
);
});
}
@ -184,9 +192,19 @@ function uninstallModule(module) {
reject(new Error("Invalid module name"));
return;
}
var installDir = settings.userDir || process.env.NODE_RED_HOME || ".";
var moduleDir = path.join(installDir,"node_modules",module);
if (!fs.existsSync(moduleDir)) {
return reject(new Error("Unabled to uninstall "+module+"."));
}
var list = redNodes.removeModule(module);
log.info("Removing module: "+module);
var child = child_process.exec('npm remove '+module, function(err, stdin, stdout) {
var child = child_process.exec('npm remove '+module,
{
cwd: installDir
},
function(err, stdin, stdout) {
if (err) {
log.warn("Removal of module "+module+" failed:");
log.warn("------------------------------------------");
@ -198,7 +216,8 @@ function uninstallModule(module) {
reportRemovedModules(list);
resolve(list);
}
});
}
);
});
}

View File

@ -25,13 +25,12 @@ var storage = null;
var persistentSettings = {
init: function(settings) {
userSettings = settings;
for (var i in settings) {
if (settings.hasOwnProperty(i)) {
(function() {
var j = i;
persistentSettings.__defineGetter__(j,function() { return userSettings[j]; });
persistentSettings.__defineSetter__(j,function() { throw new Error("Property '"+i+"' is read-only"); });
persistentSettings.__defineSetter__(j,function() { throw new Error("Property '"+j+"' is read-only"); });
})();
}
}

View File

@ -32,7 +32,6 @@ var flowsFileBackup;
var credentialsFile;
var credentialsFileBackup;
var oldCredentialsFile;
var userDir;
var libDir;
var libFlowsDir;
var globalSettingsFile;
@ -145,38 +144,50 @@ function writeFile(path,content) {
var localfilesystem = {
init: function(_settings) {
settings = _settings;
userDir = settings.userDir || process.env.NODE_RED_HOME;
var promises = [];
if (!settings.userDir) {
if (fs.existsSync(fspath.join(process.env.NODE_RED_HOME,".config.json"))) {
settings.userDir = process.env.NODE_RED_HOME;
} else {
settings.userDir = fspath.join(process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE,".node-red");
promises.push(promiseDir(settings.userDir));
}
}
if (settings.flowFile) {
flowsFile = settings.flowFile;
flowsFullPath = flowsFile;
} else {
flowsFile = 'flows_'+require('os').hostname()+'.json';
flowsFullPath = fspath.join(userDir,flowsFile);
flowsFullPath = fspath.join(settings.userDir,flowsFile);
}
var ffExt = fspath.extname(flowsFullPath);
var ffName = fspath.basename(flowsFullPath);
var ffBase = fspath.basename(flowsFullPath,ffExt);
var ffDir = fspath.dirname(flowsFullPath);
credentialsFile = fspath.join(userDir,ffBase+"_cred"+ffExt);
credentialsFileBackup = fspath.join(userDir,"."+ffBase+"_cred"+ffExt+".backup");
credentialsFile = fspath.join(settings.userDir,ffBase+"_cred"+ffExt);
credentialsFileBackup = fspath.join(settings.userDir,"."+ffBase+"_cred"+ffExt+".backup");
oldCredentialsFile = fspath.join(userDir,"credentials.json");
oldCredentialsFile = fspath.join(settings.userDir,"credentials.json");
flowsFileBackup = fspath.join(ffDir,"."+ffName+".backup");
libDir = fspath.join(userDir,"lib");
libDir = fspath.join(settings.userDir,"lib");
libFlowsDir = fspath.join(libDir,"flows");
globalSettingsFile = fspath.join(settings.userDir,".config.json");
globalSettingsFile = fspath.join(userDir,".config.json");
promises.push(promiseDir(libFlowsDir));
return promiseDir(libFlowsDir);
return when.all(promises);
},
getFlows: function() {
var defer = when.defer();
log.info("User Directory : "+settings.userDir);
fs.exists(flowsFullPath, function(exists) {
if (exists) {
log.info("Loading flows : "+flowsFile);
@ -184,7 +195,7 @@ var localfilesystem = {
return JSON.parse(data);
}));
} else {
log.info("Flows file not found : "+flowsFile );
log.info("Creating new flows file : "+flowsFile );
defer.resolve([]);
}
});

View File

@ -17,6 +17,7 @@ var should = require("should");
var when = require("when");
var sinon = require("sinon");
var child_process = require('child_process');
var fs = require("fs");
var comms = require("../../red/comms");
var redNodes = require("../../red/nodes");
@ -133,7 +134,7 @@ describe("red/server", function() {
});
it("rejects when npm returns a 404", function(done) {
var exec = sinon.stub(child_process,"exec",function(cmd,cb) {
var exec = sinon.stub(child_process,"exec",function(cmd,opt,cb) {
cb(new Error(),""," 404 this_wont_exist");
});
@ -145,14 +146,13 @@ describe("red/server", function() {
});
});
it("rejects with generic error", function(done) {
var exec = sinon.stub(child_process,"exec",function(cmd,cb) {
var exec = sinon.stub(child_process,"exec",function(cmd,opt,cb) {
cb(new Error("test_error"),"","");
});
server.installModule("this_wont_exist").then(function() {
done(new Error("Unexpected success"));
}).otherwise(function(err) {
err.message.should.be.eql("Install failed");
done();
}).finally(function() {
exec.restore();
@ -160,7 +160,7 @@ describe("red/server", function() {
});
it("succeeds when module is found", function(done) {
var nodeInfo = {module:"foo",types:["a"]};
var exec = sinon.stub(child_process,"exec",function(cmd,cb) {
var exec = sinon.stub(child_process,"exec",function(cmd,opt,cb) {
cb(null,"","");
});
var addModule = sinon.stub(redNodes,"addModule",function(md) {
@ -198,14 +198,13 @@ describe("red/server", function() {
var removeModule = sinon.stub(redNodes,"removeModule",function(md) {
return when.resolve(nodeInfo);
});
var exec = sinon.stub(child_process,"exec",function(cmd,cb) {
var exec = sinon.stub(child_process,"exec",function(cmd,opt,cb) {
cb(new Error("test_error"),"","");
});
server.uninstallModule("this_wont_exist").then(function() {
done(new Error("Unexpected success"));
}).otherwise(function(err) {
err.message.should.be.eql("Removal failed");
done();
}).finally(function() {
exec.restore();
@ -217,9 +216,10 @@ describe("red/server", function() {
var removeModule = sinon.stub(redNodes,"removeModule",function(md) {
return nodeInfo;
});
var exec = sinon.stub(child_process,"exec",function(cmd,cb) {
var exec = sinon.stub(child_process,"exec",function(cmd,opt,cb) {
cb(null,"","");
});
var exists = sinon.stub(fs,"existsSync", function(fn) { return true; });
server.uninstallModule("this_wont_exist").then(function(info) {
info.should.eql(nodeInfo);
@ -232,6 +232,7 @@ describe("red/server", function() {
}).finally(function() {
exec.restore();
removeModule.restore();
exists.restore();
});
});
});