Remove pre-req for mkdirp package.

(fs-extra has all the features needed already)
This commit is contained in:
dceejay 2015-04-14 18:41:48 +01:00
parent 105d38c885
commit 6f84526364
5 changed files with 42 additions and 92 deletions

View File

@ -16,7 +16,7 @@
module.exports = function(RED) { module.exports = function(RED) {
"use strict"; "use strict";
var fs = require("fs"); var fs = require("fs-extra");
function FileNode(n) { function FileNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
@ -37,7 +37,7 @@ module.exports = function(RED) {
} else if (msg.hasOwnProperty('delete')) { // remove warning at some point in future } else if (msg.hasOwnProperty('delete')) { // remove warning at some point in future
node.warn("Warning: Invalid delete. Please use specific delete option in config dialog."); node.warn("Warning: Invalid delete. Please use specific delete option in config dialog.");
//fs.unlink(filename, function (err) { //fs.unlink(filename, function (err) {
//if (err) { node.error('Failed to delete file : '+err,msg); } //if (err) { node.error('failed to delete file : '+err,msg); }
//}); //});
} else if (msg.payload && (typeof msg.payload != "undefined")) { } else if (msg.payload && (typeof msg.payload != "undefined")) {
var data = msg.payload; var data = msg.payload;
@ -50,13 +50,13 @@ module.exports = function(RED) {
// using "binary" not {encoding:"binary"} to be 0.8 compatible for a while // using "binary" not {encoding:"binary"} to be 0.8 compatible for a while
//fs.writeFile(filename, data, {encoding:"binary"}, function (err) { //fs.writeFile(filename, data, {encoding:"binary"}, function (err) {
fs.writeFile(filename, data, "binary", function (err) { fs.writeFile(filename, data, "binary", function (err) {
if (err) { node.error('Failed to write to file : '+err,msg); } if (err) { node.error('failed to write to file : '+err,msg); }
else if (RED.settings.verbose) { node.log('wrote to file: '+filename); } else if (RED.settings.verbose) { node.log('wrote to file: '+filename); }
}); });
} }
else if (this.overwriteFile === "delete") { else if (this.overwriteFile === "delete") {
fs.unlink(filename, function (err) { fs.unlink(filename, function (err) {
if (err) { node.error('Failed to delete file : '+err,msg); } if (err) { node.error('failed to delete file : '+err,msg); }
else if (RED.settings.verbose) { node.log("deleted file: "+filename); } else if (RED.settings.verbose) { node.log("deleted file: "+filename); }
}); });
} }
@ -64,7 +64,7 @@ module.exports = function(RED) {
// using "binary" not {encoding:"binary"} to be 0.8 compatible for a while longer // using "binary" not {encoding:"binary"} to be 0.8 compatible for a while longer
//fs.appendFile(filename, data, {encoding:"binary"}, function (err) { //fs.appendFile(filename, data, {encoding:"binary"}, function (err) {
fs.appendFile(filename, data, "binary", function (err) { fs.appendFile(filename, data, "binary", function (err) {
if (err) { node.error('Failed to append to file : '+err,msg); } if (err) { node.error('failed to append to file : '+err,msg); }
else if (RED.settings.verbose) { node.log('appended to file: '+filename); } else if (RED.settings.verbose) { node.log('appended to file: '+filename); }
}); });
} }

View File

@ -42,7 +42,6 @@
"sentiment":"0.2.3", "sentiment":"0.2.3",
"follow-redirects":"0.0.3", "follow-redirects":"0.0.3",
"cors":"2.5.3", "cors":"2.5.3",
"mkdirp":"0.5.0",
"cheerio":"0.18.0", "cheerio":"0.18.0",
"uglify-js":"2.4.16", "uglify-js":"2.4.16",
"nodemailer":"1.3.0", "nodemailer":"1.3.0",

View File

@ -14,12 +14,12 @@
* limitations under the License. * limitations under the License.
**/ **/
var fs = require('fs'); var fs = require('fs-extra');
var when = require('when'); var when = require('when');
var nodeFn = require('when/node/function'); var nodeFn = require('when/node/function');
var keys = require('when/keys'); var keys = require('when/keys');
var fspath = require("path"); var fspath = require("path");
var mkdirp = require("mkdirp"); var mkdirp = fs.mkdirs;
var log = require("../log"); var log = require("../log");
@ -102,7 +102,7 @@ function getFileBody(root,path) {
return body; return body;
} }
/** /**
* Write content to a file using UTF8 encoding. * Write content to a file using UTF8 encoding.
* This forces a fsync before completing to ensure * This forces a fsync before completing to ensure
* the write hits disk. * the write hits disk.
@ -124,9 +124,9 @@ function writeFile(path,content) {
var localfilesystem = { var localfilesystem = {
init: function(_settings) { init: function(_settings) {
settings = _settings; settings = _settings;
var promises = []; var promises = [];
if (!settings.userDir) { if (!settings.userDir) {
if (fs.existsSync(fspath.join(process.env.NODE_RED_HOME,".config.json"))) { if (fs.existsSync(fspath.join(process.env.NODE_RED_HOME,".config.json"))) {
settings.userDir = process.env.NODE_RED_HOME; settings.userDir = process.env.NODE_RED_HOME;
@ -135,10 +135,10 @@ var localfilesystem = {
promises.push(promiseDir(settings.userDir)); promises.push(promiseDir(settings.userDir));
} }
} }
if (settings.flowFile) { if (settings.flowFile) {
flowsFile = settings.flowFile; flowsFile = settings.flowFile;
if (flowsFile[0] == "/") { if (flowsFile[0] == "/") {
// Absolute path // Absolute path
flowsFullPath = flowsFile; flowsFullPath = flowsFile;
@ -154,7 +154,7 @@ var localfilesystem = {
flowsFullPath = fspath.join(settings.userDir,flowsFile); 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);
@ -163,23 +163,23 @@ var localfilesystem = {
var ffName = fspath.basename(flowsFullPath); var ffName = fspath.basename(flowsFullPath);
var ffBase = fspath.basename(flowsFullPath,ffExt); var ffBase = fspath.basename(flowsFullPath,ffExt);
var ffDir = fspath.dirname(flowsFullPath); var ffDir = fspath.dirname(flowsFullPath);
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");
sessionsFile = fspath.join(settings.userDir,".sessions.json"); sessionsFile = fspath.join(settings.userDir,".sessions.json");
libDir = fspath.join(settings.userDir,"lib"); libDir = fspath.join(settings.userDir,"lib");
libFlowsDir = fspath.join(libDir,"flows"); libFlowsDir = fspath.join(libDir,"flows");
globalSettingsFile = fspath.join(settings.userDir,".config.json"); globalSettingsFile = fspath.join(settings.userDir,".config.json");
promises.push(promiseDir(libFlowsDir)); promises.push(promiseDir(libFlowsDir));
return when.all(promises); return when.all(promises);
}, },
@ -204,9 +204,9 @@ var localfilesystem = {
if (fs.existsSync(flowsFullPath)) { if (fs.existsSync(flowsFullPath)) {
fs.renameSync(flowsFullPath,flowsFileBackup); fs.renameSync(flowsFullPath,flowsFileBackup);
} }
var flowData; var flowData;
if (settings.flowFilePretty) { if (settings.flowFilePretty) {
flowData = JSON.stringify(flows,null,4); flowData = JSON.stringify(flows,null,4);
} else { } else {
@ -249,7 +249,7 @@ var localfilesystem = {
} }
return writeFile(credentialsFile, credentialData); return writeFile(credentialsFile, credentialData);
}, },
getSettings: function() { getSettings: function() {
if (fs.existsSync(globalSettingsFile)) { if (fs.existsSync(globalSettingsFile)) {
return nodeFn.call(fs.readFile,globalSettingsFile,'utf8').then(function(data) { return nodeFn.call(fs.readFile,globalSettingsFile,'utf8').then(function(data) {
@ -290,7 +290,7 @@ var localfilesystem = {
saveSessions: function(sessions) { saveSessions: function(sessions) {
return writeFile(sessionsFile,JSON.stringify(sessions)); return writeFile(sessionsFile,JSON.stringify(sessions));
}, },
getLibraryEntry: function(type,path) { getLibraryEntry: function(type,path) {
var root = fspath.join(libDir,type); var root = fspath.join(libDir,type);
var rootPath = fspath.join(libDir,type,path); var rootPath = fspath.join(libDir,type,path);

View File

@ -17,9 +17,7 @@
var should = require("should"); var should = require("should");
var path = require('path'); var path = require('path');
var fs = require('fs-extra'); var fs = require('fs-extra');
var mkdirp = require('mkdirp');
var sinon = require('sinon'); var sinon = require('sinon');
var tailNode = require("../../../../nodes/core/storage/28-tail.js"); var tailNode = require("../../../../nodes/core/storage/28-tail.js");
var helper = require("../../helper.js"); var helper = require("../../helper.js");

View File

@ -16,8 +16,7 @@
var should = require("should"); var should = require("should");
var path = require('path'); var path = require('path');
var fn = require('fs-extra'); var fs = require('fs-extra');
var mkdirp = require('mkdirp');
var sinon = require("sinon"); var sinon = require("sinon");
var fileNode = require("../../../../nodes/core/storage/50-file.js"); var fileNode = require("../../../../nodes/core/storage/50-file.js");
var helper = require("../../helper.js"); var helper = require("../../helper.js");
@ -32,13 +31,13 @@ describe('file Nodes', function() {
var wait = 150; var wait = 150;
beforeEach(function(done) { beforeEach(function(done) {
//fn.writeFileSync(fileToTest, "File message line 1\File message line 2\n"); //fs.writeFileSync(fileToTest, "File message line 1\File message line 2\n");
helper.startServer(done); helper.startServer(done);
}); });
afterEach(function(done) { afterEach(function(done) {
helper.unload().then(function() { helper.unload().then(function() {
//fn.unlinkSync(fileToTest); //fs.unlinkSync(fileToTest);
helper.stopServer(done); helper.stopServer(done);
}); });
}); });
@ -58,9 +57,9 @@ describe('file Nodes', function() {
var n1 = helper.getNode("fileNode1"); var n1 = helper.getNode("fileNode1");
n1.emit("input", {payload:"test"}); n1.emit("input", {payload:"test"});
setTimeout(function() { setTimeout(function() {
var f = fn.readFileSync(fileToTest); var f = fs.readFileSync(fileToTest);
f.should.have.length(4); f.should.have.length(4);
fn.unlinkSync(fileToTest); fs.unlinkSync(fileToTest);
done(); done();
},wait); },wait);
}); });
@ -75,7 +74,7 @@ describe('file Nodes', function() {
n1.emit("input", {payload:true}); // boolean n1.emit("input", {payload:true}); // boolean
},50); },50);
setTimeout(function() { setTimeout(function() {
var f = fn.readFileSync(fileToTest).toString(); var f = fs.readFileSync(fileToTest).toString();
f.should.have.length(11); f.should.have.length(11);
f.should.equal("test2\ntrue\n"); f.should.equal("test2\ntrue\n");
done(); done();
@ -83,34 +82,13 @@ describe('file Nodes', function() {
}); });
}); });
it('should warn if msg.filename tries to override node', function(done) {
var flow = [{id:"fileNode1", type:"file", name: "fileNode", "filename":fileToTest, "appendNewline":true, "overwriteFile":false}];
helper.load(fileNode, flow, function() {
var n1 = helper.getNode("fileNode1");
n1.emit("input", {payload:{a:1,b:2}, filename:"/tmp/foo"}); // object
setTimeout(function() {
var f = fn.readFileSync(fileToTest).toString();
f.should.have.length(25);
f.should.equal("test2\ntrue\n{\"a\":1,\"b\":2}\n");
var logEvents = helper.log().args.filter(function(evt) {
return evt[0].type == "file";
});
//console.log(logEvents);
logEvents.should.have.length(1);
logEvents[0][0].should.have.a.property('msg');
logEvents[0][0].msg.toString().should.startWith("Warning: msg");
done();
},wait);
});
});
it('should use msg.filename if filename not set in node', function(done) { it('should use msg.filename if filename not set in node', function(done) {
var flow = [{id:"fileNode1", type:"file", name: "fileNode", "appendNewline":true, "overwriteFile":true}]; var flow = [{id:"fileNode1", type:"file", name: "fileNode", "appendNewline":true, "overwriteFile":true}];
helper.load(fileNode, flow, function() { helper.load(fileNode, flow, function() {
var n1 = helper.getNode("fileNode1"); var n1 = helper.getNode("fileNode1");
n1.emit("input", {payload:"fine", filename:fileToTest}); n1.emit("input", {payload:"fine", filename:fileToTest});
setTimeout(function() { setTimeout(function() {
var f = fn.readFileSync(fileToTest).toString(); var f = fs.readFileSync(fileToTest).toString();
f.should.have.length(5); f.should.have.length(5);
f.should.equal("fine\n"); f.should.equal("fine\n");
done(); done();
@ -118,30 +96,6 @@ describe('file Nodes', function() {
}); });
}); });
it('should warn and not delete the file if msg.delete set', function(done) {
var flow = [{id:"fileNode1", type:"file", name: "fileNode", "filename":fileToTest, "appendNewline":false, "overwriteFile":true}];
helper.load(fileNode, flow, function() {
var n1 = helper.getNode("fileNode1");
n1.emit("input", {payload:"fine",delete:true});
setTimeout(function() {
try {
var f = fn.readFileSync(fileToTest).toString();
var logEvents = helper.log().args.filter(function(evt) {
return evt[0].type == "file";
});
//console.log(logEvents);
logEvents.should.have.length(1);
logEvents[0][0].should.have.a.property('msg');
logEvents[0][0].msg.toString().should.startWith("Warning: Invalid");
done();
}
catch(e) {
done();
}
},wait);
});
});
it('should be able to delete the file', function(done) { it('should be able to delete the file', function(done) {
var flow = [{id:"fileNode1", type:"file", name: "fileNode", "filename":fileToTest, "appendNewline":false, "overwriteFile":"delete"}]; var flow = [{id:"fileNode1", type:"file", name: "fileNode", "filename":fileToTest, "appendNewline":false, "overwriteFile":"delete"}];
helper.load(fileNode, flow, function() { helper.load(fileNode, flow, function() {
@ -149,7 +103,7 @@ describe('file Nodes', function() {
n1.emit("input", {payload:"fine"}); n1.emit("input", {payload:"fine"});
setTimeout(function() { setTimeout(function() {
try { try {
var f = fn.readFileSync(fileToTest).toString(); var f = fs.readFileSync(fileToTest).toString();
f.should.not.equal("fine"); f.should.not.equal("fine");
//done(); //done();
} }
@ -168,7 +122,7 @@ describe('file Nodes', function() {
n1.emit("input", {payload:"nofile"}); n1.emit("input", {payload:"nofile"});
setTimeout(function() { setTimeout(function() {
try { try {
var f = fn.readFileSync(fileToTest).toString(); var f = fs.readFileSync(fileToTest).toString();
f.should.not.equal("fine"); f.should.not.equal("fine");
//done(); //done();
} }
@ -186,14 +140,13 @@ describe('file Nodes', function() {
}); });
}); });
it('ignore a null payload', function(done) { it('ignore a missing payload', function(done) {
var flow = [{id:"fileNode1", type:"file", name: "fileNode", "filename":fileToTest, "appendNewline":true, "overwriteFile":false}]; var flow = [{id:"fileNode1", type:"file", name: "fileNode", "filename":fileToTest, "appendNewline":true, "overwriteFile":false}];
helper.load(fileNode, flow, function() { helper.load(fileNode, flow, function() {
var n1 = helper.getNode("fileNode1"); var n1 = helper.getNode("fileNode1");
n1.emit("input", {payload:null});
setTimeout(function() { setTimeout(function() {
try { try {
var f = fn.readFileSync(fileToTest).toString(); var f = fs.readFileSync(fileToTest).toString();
f.should.not.equal("fine"); f.should.not.equal("fine");
//done(); //done();
} }
@ -206,13 +159,15 @@ describe('file Nodes', function() {
done(); done();
} }
},wait); },wait);
n1.emit("input", {topic:"test"});
}); });
}); });
it('should fail to write to a ro file', function(done) { it('should fail to write to a ro file', function(done) {
// Stub file write so we can make writes fail // Stub file write so we can make writes fail
var fs = require('fs'); var spy = sinon.stub(fs, 'writeFile', function(arg1,arg2,arg3,arg4) {
var spy = sinon.stub(fs, 'writeFile', function(arg,arg2,arg3,arg4){ arg4(new Error("Stub error message")); }); arg4(new Error("Stub error message"));
});
var flow = [{id:"fileNode1", type:"file", name: "fileNode", "filename":fileToTest, "appendNewline":false, "overwriteFile":true}]; var flow = [{id:"fileNode1", type:"file", name: "fileNode", "filename":fileToTest, "appendNewline":false, "overwriteFile":true}];
helper.load(fileNode, flow, function() { helper.load(fileNode, flow, function() {
@ -225,7 +180,7 @@ describe('file Nodes', function() {
//console.log(logEvents); //console.log(logEvents);
logEvents.should.have.length(1); logEvents.should.have.length(1);
logEvents[0][0].should.have.a.property('msg'); logEvents[0][0].should.have.a.property('msg');
logEvents[0][0].msg.toString().should.startWith("Failed to write"); logEvents[0][0].msg.toString().should.startWith("failed to write");
done(); done();
} }
catch(e) { done(e); } catch(e) { done(e); }
@ -237,7 +192,6 @@ describe('file Nodes', function() {
it('should fail to append to a ro file', function(done) { it('should fail to append to a ro file', function(done) {
// Stub file write so we can make writes fail // Stub file write so we can make writes fail
var fs = require('fs');
var spy = sinon.stub(fs, 'appendFile', function(arg,arg2,arg3,arg4){ arg4(new Error("Stub error message")); }); var spy = sinon.stub(fs, 'appendFile', function(arg,arg2,arg3,arg4){ arg4(new Error("Stub error message")); });
var flow = [{id:"fileNode1", type:"file", name: "fileNode", "filename":fileToTest, "appendNewline":true, "overwriteFile":false}]; var flow = [{id:"fileNode1", type:"file", name: "fileNode", "filename":fileToTest, "appendNewline":true, "overwriteFile":false}];
@ -251,7 +205,7 @@ describe('file Nodes', function() {
//console.log(logEvents); //console.log(logEvents);
logEvents.should.have.length(1); logEvents.should.have.length(1);
logEvents[0][0].should.have.a.property('msg'); logEvents[0][0].should.have.a.property('msg');
logEvents[0][0].msg.toString().should.startWith("Failed to append"); logEvents[0][0].msg.toString().should.startWith("failed to append");
done(); done();
} }
catch(e) { done(e); } catch(e) { done(e); }
@ -263,7 +217,6 @@ describe('file Nodes', function() {
it('should cope with failing to delete a file', function(done) { it('should cope with failing to delete a file', function(done) {
// Stub file write so we can make writes fail // Stub file write so we can make writes fail
var fs = require('fs');
var spy = sinon.stub(fs, 'unlink', function(arg,arg2){ arg2(new Error("Stub error message")); }); var spy = sinon.stub(fs, 'unlink', function(arg,arg2){ arg2(new Error("Stub error message")); });
var flow = [{id:"fileNode1", type:"file", name: "fileNode", "filename":fileToTest, "appendNewline":true, "overwriteFile":"delete"}]; var flow = [{id:"fileNode1", type:"file", name: "fileNode", "filename":fileToTest, "appendNewline":true, "overwriteFile":"delete"}];
@ -277,7 +230,7 @@ describe('file Nodes', function() {
//console.log(logEvents); //console.log(logEvents);
logEvents.should.have.length(1); logEvents.should.have.length(1);
logEvents[0][0].should.have.a.property('msg'); logEvents[0][0].should.have.a.property('msg');
logEvents[0][0].msg.toString().should.startWith("Failed to delete"); logEvents[0][0].msg.toString().should.startWith("failed to delete");
done(); done();
} }
catch(e) { done(e); } catch(e) { done(e); }
@ -297,13 +250,13 @@ describe('file Nodes', function() {
var wait = 150; var wait = 150;
beforeEach(function(done) { beforeEach(function(done) {
fn.writeFileSync(fileToTest, "File message line 1\File message line 2\n"); fs.writeFileSync(fileToTest, "File message line 1\File message line 2\n");
helper.startServer(done); helper.startServer(done);
}); });
afterEach(function(done) { afterEach(function(done) {
helper.unload().then(function() { helper.unload().then(function() {
fn.unlinkSync(fileToTest); fs.unlinkSync(fileToTest);
helper.stopServer(done); helper.stopServer(done);
}); });
}); });