You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 lines
700 B

1 month ago
  1. # `patchFs(vol[, fs])`
  2. Rewrites Node's filesystem module `fs` with *fs-like* object.
  3. - `vol` - fs-like object
  4. - `fs` *(optional)* - a filesystem to patch, defaults to `require('fs')`
  5. ```js
  6. import {patchFs} from 'fs-monkey';
  7. const myfs = {
  8. readFileSync: () => 'hello world',
  9. };
  10. patchFs(myfs);
  11. console.log(require('fs').readFileSync('/foo/bar')); // hello world
  12. ```
  13. You don't need to create *fs-like* objects yourself, use [`memfs`](https://github.com/streamich/memfs)
  14. to create a virtual filesystem for you:
  15. ```js
  16. import {vol} from 'memfs';
  17. import {patchFs} from 'fs-monkey';
  18. vol.fromJSON({'/dir/foo': 'bar'});
  19. patchFs(vol);
  20. console.log(require('fs').readdirSync('/')); // [ 'dir' ]
  21. ```