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

Load flows file from userDir when appropriate

This commit is contained in:
Nick O'Leary 2015-02-26 11:30:20 +00:00
parent 4d48c72146
commit f78a71e8ed
2 changed files with 61 additions and 39 deletions

18
red.js
View File

@ -70,18 +70,24 @@ if (parsedArgs.settings) {
// User-specified userDir that contains a settings.js // User-specified userDir that contains a settings.js
settingsFile = path.join(parsedArgs.userDir,"settings.js"); settingsFile = path.join(parsedArgs.userDir,"settings.js");
} else { } else {
var userSettingsFile = path.join(process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE,".node-red","settings.js"); if (fs.existsSync(path.join(process.env.NODE_RED_HOME,".config.json"))) {
if (fs.existsSync(userSettingsFile)) { // NODE_RED_HOME contains user data - use its settings.js
// $HOME/.node-red/settings.js exists settingsFile = path.join(process.env.NODE_RED_HOME,"settings.js");
settingsFile = userSettingsFile;
} else { } else {
// Use default settings.js var userSettingsFile = path.join(process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE,".node-red","settings.js");
settingsFile = "./settings"; if (fs.existsSync(userSettingsFile)) {
// $HOME/.node-red/settings.js exists
settingsFile = userSettingsFile;
} else {
// Use default settings.js
settingsFile = "./settings";
}
} }
} }
try { try {
var settings = require(settingsFile); var settings = require(settingsFile);
settings.settingsFile = settingsFile;
} catch(err) { } catch(err) {
if (err.code == 'MODULE_NOT_FOUND') { if (err.code == 'MODULE_NOT_FOUND') {
console.log("Unable to load settings file: "+settingsFile); console.log("Unable to load settings file: "+settingsFile);

View File

@ -158,7 +158,23 @@ var localfilesystem = {
if (settings.flowFile) { if (settings.flowFile) {
flowsFile = settings.flowFile; flowsFile = settings.flowFile;
flowsFullPath = flowsFile;
if (flowsFile[0] == "/") {
// Absolute path
flowsFullPath = flowsFile;
} else if (flowsFile.substring(0,2) === "./") {
// Relative to cwd
flowsFullPath = fspath.join(process.cwd(),flowsFile);
} else {
if (fs.existsSync(fspath.join(process.cwd(),flowsFile))) {
// Found in cwd
flowsFullPath = fspath.join(process.cwd(),flowsFile);
} else {
// Use userDir
flowsFullPath = fspath.join(settings.userDir,flowsFile);
}
}
} else { } else {
flowsFile = 'flows_'+require('os').hostname()+'.json'; flowsFile = 'flows_'+require('os').hostname()+'.json';
flowsFullPath = fspath.join(settings.userDir,flowsFile); flowsFullPath = fspath.join(settings.userDir,flowsFile);
@ -170,7 +186,7 @@ var localfilesystem = {
credentialsFile = fspath.join(settings.userDir,ffBase+"_cred"+ffExt); credentialsFile = fspath.join(settings.userDir,ffBase+"_cred"+ffExt);
credentialsFileBackup = fspath.join(settings.userDir,"."+ffBase+"_cred"+ffExt+".backup"); credentialsFileBackup = fspath.join(settings.userDir,"."+ffBase+"_cred"+ffExt+".backup");
oldCredentialsFile = fspath.join(settings.userDir,"credentials.json"); oldCredentialsFile = fspath.join(settings.userDir,"credentials.json");
flowsFileBackup = fspath.join(ffDir,"."+ffName+".backup"); flowsFileBackup = fspath.join(ffDir,"."+ffName+".backup");
@ -186,20 +202,20 @@ var localfilesystem = {
}, },
getFlows: function() { getFlows: function() {
var defer = when.defer(); return when.promise(function(resolve) {
log.info("User Directory : "+settings.userDir); log.info("User Directory : "+settings.userDir);
fs.exists(flowsFullPath, function(exists) { log.info("Flows file : "+flowsFullPath);
if (exists) { fs.exists(flowsFullPath, function(exists) {
log.info("Loading flows : "+flowsFile); if (exists) {
defer.resolve(nodeFn.call(fs.readFile,flowsFullPath,'utf8').then(function(data) { resolve(nodeFn.call(fs.readFile,flowsFullPath,'utf8').then(function(data) {
return JSON.parse(data); return JSON.parse(data);
})); }));
} else { } else {
log.info("Creating new flows file : "+flowsFile ); log.info("Creating new flows file");
defer.resolve([]); resolve([]);
} }
});
}); });
return defer.promise;
}, },
saveFlows: function(flows) { saveFlows: function(flows) {
@ -218,25 +234,25 @@ var localfilesystem = {
}, },
getCredentials: function() { getCredentials: function() {
var defer = when.defer(); return when.promise(function(resolve) {
fs.exists(credentialsFile, function(exists) { fs.exists(credentialsFile, function(exists) {
if (exists) { if (exists) {
defer.resolve(nodeFn.call(fs.readFile, credentialsFile, 'utf8').then(function(data) { resolve(nodeFn.call(fs.readFile, credentialsFile, 'utf8').then(function(data) {
return JSON.parse(data) return JSON.parse(data)
})); }));
} else { } else {
fs.exists(oldCredentialsFile, function(exists) { fs.exists(oldCredentialsFile, function(exists) {
if (exists) { if (exists) {
defer.resolve(nodeFn.call(fs.readFile, oldCredentialsFile, 'utf8').then(function(data) { resolve(nodeFn.call(fs.readFile, oldCredentialsFile, 'utf8').then(function(data) {
return JSON.parse(data) return JSON.parse(data)
})); }));
} else { } else {
defer.resolve({}); resolve({});
} }
}); });
} }
});
}); });
return defer.promise;
}, },
saveCredentials: function(credentials) { saveCredentials: function(credentials) {