Skip to content

Commit

Permalink
Use write-file-atomic to avoid corruption
Browse files Browse the repository at this point in the history
Fixes #20.
  • Loading branch information
kevva committed Aug 4, 2015
1 parent 6c60513 commit 9867a7a
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var mkdirp = require('mkdirp');
var uuid = require('uuid');
var xdgBasedir = require('xdg-basedir');
var osTmpdir = require('os-tmpdir');
var writeFileAtomic = require('write-file-atomic');

var user = (osenv.user() || uuid.v4()).replace(/\\/g, '');
var configDir = xdgBasedir.config || path.join(osTmpdir(), user, '.config');
Expand Down Expand Up @@ -38,7 +39,7 @@ Configstore.prototype = Object.create(Object.prototype, {

// empty the file if it encounters invalid JSON
if (err.name === 'SyntaxError') {
fs.writeFileSync(this.path, '', writeFileOptions);
writeFileAtomic.sync(this.path, '', writeFileOptions);
return {};
}

Expand All @@ -51,7 +52,7 @@ Configstore.prototype = Object.create(Object.prototype, {
// could have been deleted in the meantime
mkdirp.sync(path.dirname(this.path), defaultPathMode);

fs.writeFileSync(this.path, JSON.stringify(val, null, '\t'), writeFileOptions);
writeFileAtomic.sync(this.path, JSON.stringify(val, null, '\t'), writeFileOptions);
} catch (err) {
// improve the message of permission errors
if (err.code === 'EACCES') {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"os-tmpdir": "^1.0.0",
"osenv": "^0.1.0",
"uuid": "^2.0.1",
"write-file-atomic": "^1.1.2",
"xdg-basedir": "^2.0.0"
},
"devDependencies": {
Expand Down

0 comments on commit 9867a7a

Please sign in to comment.