|
Last change
on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago |
|
Fix frontend appearance
|
-
Property mode
set to
100644
|
|
File size:
938 bytes
|
| Line | |
|---|
| 1 | # `patchFs(vol[, fs])`
|
|---|
| 2 |
|
|---|
| 3 | Rewrites Node's filesystem module `fs` with *fs-like* object.
|
|---|
| 4 |
|
|---|
| 5 | - `vol` - fs-like object
|
|---|
| 6 | - `fs` *(optional)* - a filesystem to patch, defaults to `require('fs')`
|
|---|
| 7 |
|
|---|
| 8 | ```js
|
|---|
| 9 | import {patchFs} from 'fs-monkey';
|
|---|
| 10 |
|
|---|
| 11 | const myfs = {
|
|---|
| 12 | readFileSync: () => 'hello world',
|
|---|
| 13 | };
|
|---|
| 14 |
|
|---|
| 15 | patchFs(myfs);
|
|---|
| 16 | console.log(require('fs').readFileSync('/foo/bar')); // hello world
|
|---|
| 17 | ```
|
|---|
| 18 |
|
|---|
| 19 | You don't need to create *fs-like* objects yourself, use [`memfs`](https://github.com/streamich/memfs)
|
|---|
| 20 | to create a virtual filesystem for you:
|
|---|
| 21 |
|
|---|
| 22 | ```js
|
|---|
| 23 | import {vol} from 'memfs';
|
|---|
| 24 | import {patchFs} from 'fs-monkey';
|
|---|
| 25 |
|
|---|
| 26 | vol.fromJSON({'/dir/foo': 'bar'});
|
|---|
| 27 | patchFs(vol);
|
|---|
| 28 | console.log(require('fs').readdirSync('/')); // [ 'dir' ]
|
|---|
| 29 | ```
|
|---|
| 30 |
|
|---|
| 31 | Promises API is supported as well:
|
|---|
| 32 |
|
|---|
| 33 | ```js
|
|---|
| 34 | import {vol} from 'memfs';
|
|---|
| 35 | import {patchFs} from 'fs-monkey';
|
|---|
| 36 |
|
|---|
| 37 | vol.fromJSON({'/dir/foo': 'bar'});
|
|---|
| 38 | patchFs(vol);
|
|---|
| 39 | require('fs').promises.readFile('/dir/foo', 'UTF-8').then(console.log); // bar
|
|---|
| 40 | ```
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.