Skip to content

Commit

Permalink
Merge pull request #3 from retrohacker/safe-write
Browse files Browse the repository at this point in the history
Writes config to temp file then renames to dest
  • Loading branch information
feross authored Jul 22, 2016
2 parents 5311a5a + c05d039 commit 14a70d2
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,15 @@ ApplicationConfig.prototype.write = function (data, cb) {
mkdirp(directoryPath, function (err) {
if (err) { return cb(err) }

fs.writeFile(self.filePath, JSON.stringify(data, null, 2), cb)
var tempFilePath =
self.filePath +
Math.random().toString().substr(2) +
Date.now().toString()
fs.writeFile(tempFilePath, JSON.stringify(data, null, 2), function (err) {
if (err) { return cb(err) }

fs.rename(tempFilePath, self.filePath, cb)
})
})
}

Expand Down

0 comments on commit 14a70d2

Please sign in to comment.