Skip to content
This repository has been archived by the owner on Aug 3, 2022. It is now read-only.

Commit

Permalink
Fix fs.access constants to support earlier versions of Node.
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik Brännström committed Nov 10, 2016
1 parent 927fa55 commit 2c52840
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions Store.es6.js
Expand Up @@ -44,24 +44,27 @@ const getObjectFromFile = function(id, cb) {
});
};

const FILE_EXISTS = fs.constants ? fs.constants.F_OK : fs.F_OK;
const FILE_IS_WRITABLE = fs.constants ? fs.constants.W_OK : fs.W_OK;

const canWriteToFile = (file, cb) => {
fs.access(file, fs.constants.F_OK, (err) => {
fs.access(file, FILE_EXISTS, (err) => {
if (err) {
return cb(null);
}

fs.access(file, fs.constants.W_OK, cb);
fs.access(file, FILE_IS_WRITABLE, cb);
});
};

const canWriteToFileSync = (file) => {
try {
fs.accessSync(file, fs.constants.F_OK);
fs.accessSync(file, FILE_EXISTS);
} catch (err) {
return;
}

fs.accessSync(file, fs.constants.W_OK);
fs.accessSync(file, FILE_IS_WRITABLE);
};

const saveObjectToFile = function(o, file, cb) {
Expand Down

0 comments on commit 2c52840

Please sign in to comment.