mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
Updated credentials storage so there is a .._cred.... file per flow.
Allows swapping flows more easily without having to re-enter credentials. Thus also added *_cred* to .gitignore
This commit is contained in:
parent
84093bcb6e
commit
a9e07f8b78
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,5 +1,6 @@
|
||||
node_modules
|
||||
credentials.json
|
||||
flows*.json
|
||||
*_cred*
|
||||
nodes/node-red-nodes/
|
||||
.npm
|
||||
|
@ -1,5 +1,5 @@
|
||||
/**
|
||||
* Copyright 2013 IBM Corp.
|
||||
* Copyright 2013, 2014 IBM Corp.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@ -28,7 +28,7 @@ var settings;
|
||||
var flowsFile;
|
||||
var flowsFullPath;
|
||||
var credentialsFile;
|
||||
|
||||
var oldCredentialsFile;
|
||||
var userDir;
|
||||
var libDir;
|
||||
var libFlowsDir;
|
||||
@ -56,7 +56,6 @@ function listFiles(dir) {
|
||||
|
||||
function getFileMeta(root,path) {
|
||||
var fn = fspath.join(root,path);
|
||||
|
||||
var fd = fs.openSync(fn,"r");
|
||||
var size = fs.fstatSync(fd).size;
|
||||
var meta = {};
|
||||
@ -84,6 +83,7 @@ function getFileMeta(root,path) {
|
||||
fs.closeSync(fd);
|
||||
return meta;
|
||||
}
|
||||
|
||||
function getFileBody(root,path) {
|
||||
var body = "";
|
||||
var fn = fspath.join(root,path);
|
||||
@ -149,13 +149,16 @@ var localfilesystem = {
|
||||
flowsFile = 'flows_'+require('os').hostname()+'.json';
|
||||
flowsFullPath = fspath.join(userDir,flowsFile);
|
||||
}
|
||||
credentialsFile = fspath.join(userDir,"credentials.json");
|
||||
var fsext = fspath.extname(flowsFile);
|
||||
credentialsFile = fspath.join(userDir,fspath.basename(flowsFile,fsext)+"_cred"+fsext);
|
||||
oldCredentialsFile = fspath.join(userDir,"credentials.json");
|
||||
|
||||
libDir = fspath.join(userDir,"lib");
|
||||
libFlowsDir = fspath.join(libDir,"flows");
|
||||
|
||||
return promiseDir(libFlowsDir);
|
||||
},
|
||||
|
||||
getFlows: function() {
|
||||
var defer = when.defer();
|
||||
fs.exists(flowsFullPath, function(exists) {
|
||||
@ -171,6 +174,7 @@ var localfilesystem = {
|
||||
});
|
||||
return defer.promise;
|
||||
},
|
||||
|
||||
saveFlows: function(flows) {
|
||||
return nodeFn.call(fs.writeFile, flowsFullPath, JSON.stringify(flows));
|
||||
},
|
||||
@ -182,10 +186,18 @@ var localfilesystem = {
|
||||
defer.resolve(nodeFn.call(fs.readFile, credentialsFile, 'utf8').then(function(data) {
|
||||
return JSON.parse(data)
|
||||
}));
|
||||
} else {
|
||||
fs.exists(oldCredentialsFile, function(exists) {
|
||||
if (exists) {
|
||||
defer.resolve(nodeFn.call(fs.readFile, oldCredentialsFile, 'utf8').then(function(data) {
|
||||
return JSON.parse(data)
|
||||
}));
|
||||
} else {
|
||||
defer.resolve({});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
return defer.promise;
|
||||
},
|
||||
|
||||
@ -199,7 +211,6 @@ var localfilesystem = {
|
||||
|
||||
getFlow: function(fn) {
|
||||
var defer = when.defer();
|
||||
|
||||
var file = fspath.join(libFlowsDir,fn+".json");
|
||||
fs.exists(file, function(exists) {
|
||||
if (exists) {
|
||||
@ -208,7 +219,6 @@ var localfilesystem = {
|
||||
defer.reject();
|
||||
}
|
||||
});
|
||||
|
||||
return defer.promise;
|
||||
},
|
||||
|
||||
@ -225,7 +235,6 @@ var localfilesystem = {
|
||||
return promiseDir(root).then(function () {
|
||||
return nodeFn.call(fs.lstat, rootPath).then(function(stats) {
|
||||
if (stats.isFile()) return getFileBody(root,path);
|
||||
|
||||
if (path.substr(-1) == '/') {
|
||||
path = path.substr(0,path.length-1);
|
||||
}
|
||||
@ -245,21 +254,19 @@ var localfilesystem = {
|
||||
files.push(meta);
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
return dirs.concat(files);
|
||||
});
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
saveLibraryEntry: function(type,path,meta,body) {
|
||||
var fn = fspath.join(libDir, type, path);
|
||||
|
||||
var headers = "";
|
||||
for (var i in meta) {
|
||||
headers += "// "+i+": "+meta[i]+"\n";
|
||||
}
|
||||
|
||||
return promiseDir(fspath.dirname(fn)).then(function () {
|
||||
nodeFn.call(fs.writeFile, fn, headers+body);
|
||||
});
|
||||
|
@ -83,8 +83,10 @@ describe('LocalFileSystem', function() {
|
||||
});
|
||||
|
||||
it('should handle missing credentials', function(done) {
|
||||
var credFile = path.join(userDir,"credentials.json");
|
||||
localfilesystem.init({userDir:userDir}).then(function() {
|
||||
var flowFile = 'test.json';
|
||||
var flowFilePath = path.join(userDir,flowFile);
|
||||
var credFile = path.join(userDir,"test_cred.json");
|
||||
localfilesystem.init({userDir:userDir, flowFile:flowFilePath}).then(function() {
|
||||
fs.existsSync(credFile).should.be.false;
|
||||
|
||||
localfilesystem.getCredentials().then(function(creds) {
|
||||
@ -97,7 +99,7 @@ describe('LocalFileSystem', function() {
|
||||
});
|
||||
|
||||
it('should handle credentials', function(done) {
|
||||
var credFile = path.join(userDir,"credentials.json");
|
||||
var credFile = path.join(userDir,"test_cred.json");
|
||||
|
||||
localfilesystem.init({userDir:userDir}).then(function() {
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user