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

Add .delete property to File output node

Fix to close #162
This commit is contained in:
Dave C-J 2014-02-20 21:00:54 +00:00
parent 0bc4a3bbb1
commit 58774c366d
2 changed files with 34 additions and 26 deletions

View File

@ -37,10 +37,11 @@
<script type="text/x-red" data-help-name="file"> <script type="text/x-red" data-help-name="file">
<p>Writes <b>msg.payload</b> to the file specified, e.g. to create a log.</p> <p>Writes <b>msg.payload</b> to the file specified, e.g. to create a log.</p>
<p>The filename can be overridden by the <code>filename</code> property <p>The filename can be overridden by the <code>.filename</code> property
of the incoming message.</p> of the incoming message.</p>
<p>A newline is added to every message. But this can be turned off if required, for example, to allow binary files to be written.</p> <p>A newline is added to every message. But this can be turned off if required, for example, to allow binary files to be written.</p>
<p>The default behaviour is to append to the file. This can be changed to overwrite the file each time, for example if you want to output a "static" web page or report.</p> <p>The default behaviour is to append to the file. This can be changed to overwrite the file each time, for example if you want to output a "static" web page or report.</p>
<p>If a <code>.delete</code> property exists then the file will be deleted instead.</p>
</script> </script>
<script type="text/javascript"> <script type="text/javascript">

View File

@ -34,20 +34,27 @@ function FileNode(n) {
if (this.appendNewline) { if (this.appendNewline) {
data += "\n"; data += "\n";
} }
if (msg.hasOwnProperty('delete')) {
fs.unlink(filename, function (err) {
if (err) node.warn('Failed to delete file : '+err);
//console.log('Deleted file",filename);
});
}
else {
if (this.overwriteFile) { if (this.overwriteFile) {
fs.writeFile(filename, data, function (err) { fs.writeFile(filename, data, function (err) {
if (err) node.warn('Failed to write to file : '+err); if (err) node.warn('Failed to write to file : '+err);
//console.log('Message written to file',this.filename); //console.log('Message written to file',filename);
}); });
} }
else { else {
fs.appendFile(filename, data, function (err) { fs.appendFile(filename, data, function (err) {
if (err) node.warn('Failed to append to file : '+err); if (err) node.warn('Failed to append to file : '+err);
//console.log('Message appended to file',this.filename); //console.log('Message appended to file',filename);
}); });
} }
} }
}
}); });
} }
RED.nodes.registerType("file",FileNode); RED.nodes.registerType("file",FileNode);