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

localfilesystem - ensure folder is present before write (e.g. flows file not in user folder)

This commit is contained in:
Simon Hailes 2019-11-03 09:10:32 +00:00
parent 72deee5d74
commit a4af7b8e21

View File

@ -15,6 +15,7 @@
**/
var fs = require('fs-extra');
var fspath = require('path');
var when = require('when');
var nodeFn = require('when/node/function');
@ -79,25 +80,31 @@ module.exports = {
* the write hits disk.
*/
writeFile: function(path,content,backupPath) {
if (backupPath) {
if (backupPath) {
if (fs.existsSync(path)) {
fs.renameSync(path,backupPath);
}
}
return when.promise(function(resolve,reject) {
var stream = fs.createWriteStream(path);
stream.on('open',function(fd) {
stream.write(content,'utf8',function() {
fs.fsync(fd,function(err) {
if (err) {
log.warn(log._("storage.localfilesystem.fsync-fail",{path: path, message: err.toString()}));
}
stream.end(resolve);
fs.ensureDir(fspath.dirname(path), (err)=>{
if (err) {
reject(err);
return;
}
var stream = fs.createWriteStream(path);
stream.on('open',function(fd) {
stream.write(content,'utf8',function() {
fs.fsync(fd,function(err) {
if (err) {
log.warn(log._("storage.localfilesystem.fsync-fail",{path: path, message: err.toString()}));
}
stream.end(resolve);
});
});
});
});
stream.on('error',function(err) {
reject(err);
stream.on('error',function(err) {
reject(err);
});
});
});
},