Skip to content

Commit 21e9fd2

Browse files
author
vad
committed
First version
0 parents  commit 21e9fd2

25 files changed

+5713
-0
lines changed

.babelrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"presets": ["es2015"],
3+
"comments": false
4+
}

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.idea
2+
node_modules
3+
/.idea
4+
.nyc_output
5+
coverage
6+

.npmignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
*.ts
2+
/typings
3+
example.js
4+
tsd.json
5+
simple.js
6+
node_modules
7+
/test
8+
.idea
9+
.idea/
10+
.nyc_output
11+
coverage

.nvmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
8.2.1

.travis.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
language: node_js
2+
node_js:
3+
- "8"
4+
cache:
5+
yarn: true
6+
directories:
7+
- "node_modules"

README.md

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# `fs` Monkey
2+
3+
Monkey-patches for file system related things.
4+
5+
> **Terms**
6+
>
7+
> An *fs-like* object is an object that implements methods that correspond
8+
> to [File System API of Node.js](https://nodejs.org/api/fs.html#fs_buffer_api), like:
9+
>
10+
> ```js
11+
> let vol = {
12+
> readFileSync: () => { /* ... */ },
13+
> // etc...
14+
> }
15+
> ```
16+
>
17+
> It is denoted below as the `vol` parameter.
18+
19+
20+
## `patchFs(vol[, fs])`
21+
22+
Patches Node's file system module `fs.js`.
23+
24+
- `vol` -- fs-like object
25+
- `fs` -- a file system to patch, defaults to `require('fs')`
26+
27+
```js
28+
import {vol} from '../../../memfs/lib';
29+
import {patchFs} from '../index';
30+
31+
vol.fromJSON({'/dir/foo': 'bar'});
32+
patchFs(vol);
33+
console.log(require('fs').readdirSync('/')); // [ 'dir' ]
34+
```
35+
36+
37+
## `patchRequire(vol[, Module])`
38+
39+
Patches Node's `require` function.
40+
41+
- `vol` -- fs-like object
42+
- `Module` -- a module to patch, defaults to `require('module')`
43+
44+
Monkey-patches the `require` function of Node.js, this way you can make
45+
Node.js to *require* modules from your custom file system.
46+
47+
It expects an object with three file system methods implemented that are
48+
needed for the `require` function to work.
49+
50+
```js
51+
patchRequire({
52+
readFileSync: () => {},
53+
realpathSync: () => {},
54+
statSync: () => {},
55+
});
56+
```
57+
58+
If you want to make Node.js to *require* your files from memory, you
59+
don't need to implement those functions yourself, just use the
60+
[`memfs`](https://github.com/streamich/memfs) package:
61+
62+
```js
63+
import {vol} from 'memfs';
64+
import {patchRequire} from 'fs-monkey';
65+
66+
vol.fromJSON({'/foo/bar.js': 'console.log("obi trice");'});
67+
patchRequire(vol);
68+
require('/foo/bar'); // obi trice
69+
```
70+
71+
Now the `require` function will only load the files from the `vol` file
72+
system, but not from the actual file system.
73+
74+
If you want the `require` function to load modules from both file
75+
systems, use the [`unionfs`](https://github.com/streamich/unionfs) package
76+
to combine both file system into a union:
77+
78+
```js
79+
import {vol} from 'memfs';
80+
import {patchRequire} from 'fs-monkey';
81+
import {ufs} from 'unionfs';
82+
import * as fs from 'fs';
83+
84+
vol.fromJSON({'/foo/bar.js': 'console.log("obi trice");'});
85+
ufs
86+
.use(vol)
87+
.use(fs);
88+
patchRequire(ufs);
89+
require('/foo/bar.js'); // obi trice
90+
```
91+
92+
93+
# License
94+
95+
This is free and unencumbered software released into the public domain.
96+
97+
Anyone is free to copy, modify, publish, use, compile, sell, or
98+
distribute this software, either in source code form or as a compiled
99+
binary, for any purpose, commercial or non-commercial, and by any
100+
means.
101+
102+
In jurisdictions that recognize copyright laws, the author or authors
103+
of this software dedicate any and all copyright interest in the
104+
software to the public domain. We make this dedication for the benefit
105+
of the public at large and to the detriment of our heirs and
106+
successors. We intend this dedication to be an overt act of
107+
relinquishment in perpetuity of all present and future rights to this
108+
software under copyright law.
109+
110+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
111+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
112+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
113+
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
114+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
115+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
116+
OTHER DEALINGS IN THE SOFTWARE.
117+
118+
For more information, please refer to <http://unlicense.org/>

gulpfile.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var gulp = require('gulp');
2+
var ts = require('gulp-typescript');
3+
4+
5+
gulp.task('build-ts', function () {
6+
return gulp.src('src/**/*.ts')
7+
.pipe(ts({
8+
"target": "es5",
9+
"module": "commonjs",
10+
"removeComments": false,
11+
"noImplicitAny": false,
12+
"sourceMap": false,
13+
}))
14+
.pipe(gulp.dest('lib'));
15+
});

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('lib/index');

lib/demo/fs-1.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
3+
var _lib = require('../../../memfs/lib');
4+
5+
var _index = require('../index');
6+
7+
_lib.vol.fromJSON({ '/dir/foo': 'bar' });
8+
(0, _index.patchFs)(_lib.vol);
9+
console.log(require('fs').readdirSync('/'));

lib/demo/require-1.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
3+
var _lib = require('../../../memfs/lib');
4+
5+
var _patchRequire = require('../patchRequire');
6+
7+
var _patchRequire2 = _interopRequireDefault(_patchRequire);
8+
9+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
10+
11+
_lib.vol.fromJSON({ '/foo/bar.js': 'console.log("obi trice");' });
12+
(0, _patchRequire2.default)(_lib.vol);
13+
14+
require('/foo/bar');

0 commit comments

Comments
 (0)