2021-02-20 02:43:21 +00:00
|
|
|
const { FileSystem } = require('../src/index');
|
|
|
|
|
|
|
|
let Provider;
|
|
|
|
try {
|
2021-02-21 00:19:17 +00:00
|
|
|
Provider = require('fsProvider');
|
2021-02-20 02:43:21 +00:00
|
|
|
}
|
2021-02-21 00:21:20 +00:00
|
|
|
catch (err) {
|
2021-02-21 00:19:17 +00:00
|
|
|
Provider = require('./providers/default');
|
2021-02-20 02:43:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-04-18 14:42:47 +00:00
|
|
|
function proxyHasProp(target, prop) {
|
|
|
|
return prop in target;
|
|
|
|
}
|
|
|
|
|
2021-02-20 02:43:21 +00:00
|
|
|
const fsPromises = new Proxy(fsInstance.promises, {
|
|
|
|
get(target, prop) {
|
2021-04-18 14:42:47 +00:00
|
|
|
if (!proxyHasProp(target, prop)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-20 02:43:21 +00:00
|
|
|
return async (...args) => {
|
|
|
|
await fsReady;
|
|
|
|
return await target[prop](...args);
|
|
|
|
};
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const fs = new Proxy(fsInstance, {
|
|
|
|
get(target, prop) {
|
2021-04-18 14:42:47 +00:00
|
|
|
if (!proxyHasProp(target, prop)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-20 02:43:21 +00:00
|
|
|
if (prop === 'promises') {
|
|
|
|
return fsPromises;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (...args) => {
|
|
|
|
(async () => {
|
|
|
|
await fsReady;
|
|
|
|
target[prop](...args);
|
|
|
|
})();
|
|
|
|
};
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-04-18 14:44:22 +00:00
|
|
|
module.exports = fs;
|