fslockjs
Version:
Easy to use file system queue with locking and events. Provide Asynchronous utilities for Directories and File
30 lines (26 loc) • 784 B
JavaScript
import fs from 'fs';
import CannotReadFileNotFound from '../../errors/CannotReadFileNotFound.js';
async function read(p, options = {}) {
const isFile = await this.exists(p);
if (!isFile) throw new CannotReadFileNotFound(`CannotReadFileNotFound({path: ${p}}`);
return new Promise(async (res, rej) => {
let output;
// const lock = await slocket(p);
try{
const data = fs.readFileSync(p, options);
// lock.release();
if (Buffer.isBuffer(data)) output = data.toString('utf8');
output = output.replace(/^\uFEFF/, '');
let obj;
try {
obj = JSON.parse(output, options ? options.reviver : null);
} catch (err2) {
rej(err2);
}
res(obj);
}catch (e) {
rej(e);
}
});
}
export default read;