Skip to content

Commit

Permalink
Clean up sync-fs exports
Browse files Browse the repository at this point in the history
  • Loading branch information
alxndrsn committed Jun 1, 2017
1 parent 51c2061 commit 66c750b
Showing 1 changed file with 26 additions and 20 deletions.
46 changes: 26 additions & 20 deletions src/lib/sync-fs.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,37 @@
const fs = require('fs');
const warn = require('../lib/log').warn;

function read(path) {
try {
return fs.readFileSync(path, { encoding:'utf8' });
} catch(e) {
warn(`Error reading file: ${path}`);
throw e;
}
}

function readJson(path) {
try {
return JSON.parse(read(path));
} catch(e) {
warn(`Error parsing JSON in: ${path}`);
throw e;
}
}

function withoutExtension(fileName) {
const extensionStart = fileName.lastIndexOf('.');
return extensionStart === -1 ? fileName : fileName.substring(0, extensionStart);
}

module.exports = {
exists: fs.existsSync,
mkdir: path => { try { fs.mkdirSync(path); } catch(e) { /* yum yum */ } },
read: path => {
try {
return fs.readFileSync(path, { encoding:'utf8' });
} catch(e) {
warn(`Error reading file: ${path}`);
throw e;
}
},
readJson: path => {
try {
return JSON.parse(module.exports.read(path));
} catch(e) {
warn(`Error parsing JSON in: ${path}`);
throw e;
}
},
read: read,
readJson: readJson,
readBinary: path => fs.readFileSync(path),
readdir: fs.readdirSync,
withoutExtension: fileName => {
const extensionStart = fileName.lastIndexOf('.');
return extensionStart === -1 ? fileName : fileName.substring(0, extensionStart);
},
withoutExtension: withoutExtension,
write: (path, content) => fs.writeFileSync(path, content, 'utf8'),
writeBinary: (path, content) => fs.writeFileSync(path, content),
writeJson: (path, json) => module.exports.write(path, JSON.stringify(json, null, 2) + '\n'),
Expand Down

0 comments on commit 66c750b

Please sign in to comment.