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