Skip to content

Commit

Permalink
Add fs Promises API support
Browse files Browse the repository at this point in the history
  • Loading branch information
mille-nium committed Mar 21, 2019
1 parent 8f31db6 commit d701512
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
6 changes: 5 additions & 1 deletion .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
env:
es6: true
node: true
parserOptions:
ecmaVersion: 8
extends:
- eslint:recommended
- plugin:prettier/recommended
Expand Down Expand Up @@ -104,7 +106,9 @@ rules:
- always
space-before-function-paren:
- error
- never
- anonymous: never
named: never
asyncArrow: always
space-in-parens:
- error
- never
Expand Down
26 changes: 20 additions & 6 deletions sandboxed-fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ const twoPathFunctionsWrapper = (func, path, allowTmp) => (p1, p2, ...args) =>
...args
);

const asyncWrapper = wrapper => (...wrapperArgs) => async (...args) =>
wrapper(...wrapperArgs)(...args);

const functionTypes = {
pathFunctions: {
names: [
Expand All @@ -110,16 +113,19 @@ const functionTypes = {
'utimes',
],
wrapper: pathFunctionsWrapper,
asyncWrapper: asyncWrapper(pathFunctionsWrapper),
hasSyncCounterpart: true,
},
stringPathFunctions: {
names: ['mkdtemp'],
wrapper: stringPathFunctionsWrapper,
asyncWrapper: asyncWrapper(stringPathFunctionsWrapper),
hasSyncCounterpart: true,
},
pathFunctionsWithNative: {
names: ['realpath'],
wrapper: pathFunctionsWithNativeWrapper,
asyncWrapper: asyncWrapper(pathFunctionsWithNativeWrapper),
hasSyncCounterpart: true,
},
pathNonSyncFunctions: {
Expand All @@ -136,27 +142,35 @@ const functionTypes = {
fileFunctions: {
names: ['appendFile', 'readFile', 'writeFile'],
wrapper: fileFunctionsWrapper,
asyncWrapper: asyncWrapper(fileFunctionsWrapper),
hasSyncCounterpart: true,
},
twoPathFunctions: {
names: ['copyFile', 'link', 'rename', 'symlink'],
wrapper: twoPathFunctionsWrapper,
asyncWrapper: asyncWrapper(twoPathFunctionsWrapper),
hasSyncCounterpart: true,
},
};

sandboxedFs.bind = (path, allowTmp = true) => {
const wrapped = Object.assign({}, fs);
const promises = Object.assign({}, fs.promises);
const wrapped = Object.assign({}, fs, { promises });

for (const typeName of Object.keys(functionTypes)) {
const type = functionTypes[typeName];
for (const name of type.names) {
const fn = fs[name];
if (!fn) continue;
wrapped[name] = type.wrapper(fn, path, allowTmp);
if (type.hasSyncCounterpart) {
const syncName = name + 'Sync';
wrapped[syncName] = type.wrapper(fs[syncName], path, allowTmp);
const promiseFn = fs.promises[name];
if (fn) {
wrapped[name] = type.wrapper(fn, path, allowTmp);
if (type.hasSyncCounterpart) {
const syncName = name + 'Sync';
wrapped[syncName] = type.wrapper(fs[syncName], path, allowTmp);
}
}
if (promiseFn) {
wrapped.promises[name] = type.asyncWrapper(promiseFn, path, allowTmp);
}
}
}
Expand Down

0 comments on commit d701512

Please # to comment.