From ceed84f3770786d8d603edf94c6c2bfb3181ac91 Mon Sep 17 00:00:00 2001 From: bcheidemann Date: Sat, 20 Feb 2021 02:43:21 +0000 Subject: [PATCH] feat: add shim for fs --- shims/fs.js | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 shims/fs.js diff --git a/shims/fs.js b/shims/fs.js new file mode 100644 index 0000000..832847a --- /dev/null +++ b/shims/fs.js @@ -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, +};