1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
'use strict';
function makeSync(fs, name) {
const fn = fs[`${name}Sync`];
return function () {
const callback = arguments[arguments.length - 1];
const args = Array.prototype.slice.call(arguments, 0, -1);
let ret;
try {
ret = fn.apply(fs, args);
} catch (err) {
return callback(err);
}
callback(null, ret);
};
}
function syncFs(fs) {
const fns = ['mkdir', 'realpath', 'stat', 'rmdir', 'utimes'];
const obj = {};
// Create the sync versions of the methods that we need
fns.forEach((name) => {
obj[name] = makeSync(fs, name);
});
// Copy the rest of the functions
for (const key in fs) {
if (!obj[key]) {
obj[key] = fs[key];
}
}
return obj;
}
module.exports = syncFs;
|