From a4af7b8e21a2990dcca821f83f66e569a72d7cb9 Mon Sep 17 00:00:00 2001 From: Simon Hailes Date: Sun, 3 Nov 2019 09:10:32 +0000 Subject: [PATCH] localfilesystem - ensure folder is present before write (e.g. flows file not in user folder) --- .../lib/storage/localfilesystem/util.js | 31 ++++++++++++------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/packages/node_modules/@node-red/runtime/lib/storage/localfilesystem/util.js b/packages/node_modules/@node-red/runtime/lib/storage/localfilesystem/util.js index 267f8c933..b18c80643 100644 --- a/packages/node_modules/@node-red/runtime/lib/storage/localfilesystem/util.js +++ b/packages/node_modules/@node-red/runtime/lib/storage/localfilesystem/util.js @@ -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); + }); }); }); },