feat: add shim for fs

This commit is contained in:
bcheidemann 2021-02-20 02:43:21 +00:00
parent 2a581726c6
commit ceed84f377
1 changed files with 56 additions and 0 deletions

56
shims/fs.js Normal file
View File

@ -0,0 +1,56 @@
const { FileSystem } = require('../src/index');
let Provider;
try {
Provider = require('fsprovider');
}
catch {
Provider = require('./providers/default');
}
const provider = new Provider();
let onFsReady;
let onFsError;
let fsReady = new Promise((resolve, reject) => {
onFsReady = resolve;
onFsError = reject;
});
var fsInstance = new FileSystem({ provider }, (err) => {
if (err) {
onFsError(err);
} else {
onFsReady(true);
}
});
const fsPromises = new Proxy(fsInstance.promises, {
get(target, prop) {
return async (...args) => {
await fsReady;
return await target[prop](...args);
};
},
});
const fs = new Proxy(fsInstance, {
get(target, prop) {
if (prop === 'promises') {
return fsPromises;
}
return (...args) => {
(async () => {
await fsReady;
target[prop](...args);
})();
};
},
});
module.exports = {
__esModule: true,
default: fs,
};