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

Fix ENOENT error on first start when no user dir (#1711)

* Fix ENOENT error on first start when no user dir

Write backup using `copySync` and move it below the `fsync` to ensure file is present when backup is made.

* Check for path to exist before attempting backup
This commit is contained in:
Nathan Allen 2018-05-03 04:40:51 -04:00 committed by Nick O'Leary
parent 53e3e08d70
commit 6fa0d671c0

View File

@ -80,28 +80,26 @@ module.exports = {
*/ */
writeFile: function(path,content,backupPath) { writeFile: function(path,content,backupPath) {
if (backupPath) { if (backupPath) {
try { if (fs.existsSync(path)) {
fs.renameSync(path,backupPath); fs.renameSync(path,backupPath);
} catch(err) { }
console.log(err); }
} return when.promise(function(resolve,reject) {
} var stream = fs.createWriteStream(path);
return when.promise(function(resolve,reject) { stream.on('open',function(fd) {
var stream = fs.createWriteStream(path); stream.write(content,'utf8',function() {
stream.on('open',function(fd) { fs.fsync(fd,function(err) {
stream.write(content,'utf8',function() { if (err) {
fs.fsync(fd,function(err) { log.warn(log._("storage.localfilesystem.fsync-fail",{path: path, message: err.toString()}));
if (err) { }
log.warn(log._("storage.localfilesystem.fsync-fail",{path: path, message: err.toString()})); stream.end(resolve);
} });
stream.end(resolve); });
}); });
}); stream.on('error',function(err) {
}); reject(err);
stream.on('error',function(err) { });
reject(err); });
});
});
}, },
readFile: readFile, readFile: readFile,