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

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");

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);
}); });
}); });