Skip to content

Commit 2bb5466

Browse files
authored
Merge pull request #5 from streamich/feat/unpatch
feat: add unpatch() method
2 parents 65e6e86 + 3a276e5 commit 2bb5466

File tree

11 files changed

+233
-200
lines changed

11 files changed

+233
-200
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ node_modules
55
coverage
66
package-lock.json
77
/lib/
8+
yarn.lock

.travis.yml

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
11
language: node_js
2-
node_js:
3-
- "8"
2+
os:
3+
- linux
44
cache:
55
yarn: true
66
directories:
7-
- "node_modules"
7+
- ~/.npm
8+
notifications:
9+
email: false
10+
node_js:
11+
- '8'
12+
script:
13+
- npm run test
14+
- npm run build
15+
matrix:
16+
allow_failures: []
17+
fast_finish: true
18+
after_success:
19+
- npm run semantic-release
20+
branches:
21+
except:
22+
- /^v\d+\.\d+\.\d+$/

LICENSE

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
This is free and unencumbered software released into the public domain.
2+
3+
Anyone is free to copy, modify, publish, use, compile, sell, or
4+
distribute this software, either in source code form or as a compiled
5+
binary, for any purpose, commercial or non-commercial, and by any
6+
means.
7+
8+
In jurisdictions that recognize copyright laws, the author or authors
9+
of this software dedicate any and all copyright interest in the
10+
software to the public domain. We make this dedication for the benefit
11+
of the public at large and to the detriment of our heirs and
12+
successors. We intend this dedication to be an overt act of
13+
relinquishment in perpetuity of all present and future rights to this
14+
software under copyright law.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19+
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22+
OTHER DEALINGS IN THE SOFTWARE.
23+
24+
For more information, please refer to <https://unlicense.org>

README.md

Lines changed: 19 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1-
# `fs-monkey`
1+
# fs-monkey
22

3-
[![][npm-img]][npm-url]
3+
[![][npm-img]][npm-url] [![][travis-badge]][travis-url]
44

5-
Monkey-patches for filesystem related things. Rewrite `require` function,
6-
load Node's modules from memory. Or rewrite the whole `fs` filesystem module.
5+
Monkey-patches for filesystem related things.
76

8-
**Terms**
7+
- Rewrite `require` function to load Node's modules from memory.
8+
- Or rewrite the whole `fs` filesystem module.
9+
10+
### Install
11+
12+
```shell
13+
npm install --save fs-monkey
14+
```
15+
16+
# Terms
917

1018
An *fs-like* object is an object that implements methods of Node's
1119
[filesystem API](https://nodejs.org/api/fs.html).
@@ -20,126 +28,18 @@ let vol = {
2028
```
2129

2230

23-
`fs-monkey` **API**
24-
25-
- [`patchFs(vol[, fs])`](#patchfsvol-fs) - rewrites Node's filesystem module `fs` with *fs-like* object `vol`
26-
- [`patchRequire(vol[, Module])`](#patchrequirevol-module) - rewrites `require` function, patches Node's `module` module to use a give *fs-like* object `vol` for module loading
27-
28-
29-
# `patchFs(vol[, fs])`
30-
31-
Rewrites Node's filesystem module `fs` with *fs-like* object.
32-
33-
- `vol` - fs-like object
34-
- `fs` *(optional)* - a filesystem to patch, defaults to `require('fs')`
35-
36-
```js
37-
import {patchFs} from 'fs-monkey';
38-
39-
const myfs = {
40-
readFileSync: () => 'hello world',
41-
};
42-
43-
patchFs(myfs);
44-
console.log(require('fs').readFileSync('/foo/bar')); // hello world
45-
```
46-
47-
You don't need to create *fs-like* objects yourself, use [`memfs`](https://github.com/streamich/memfs)
48-
to create a virtual filesystem for you:
49-
50-
```js
51-
import {vol} from 'memfs';
52-
import {patchFs} from 'fs-monkey';
53-
54-
vol.fromJSON({'/dir/foo': 'bar'});
55-
patchFs(vol);
56-
console.log(require('fs').readdirSync('/')); // [ 'dir' ]
57-
```
31+
# Reference
5832

33+
- [`patchFs`](./docs/api/patchFs.md) - rewrites Node's filesystem module `fs` with *fs-like* object `vol`
34+
- [`patchRequire`](./docs/api/patchRequire.md) - rewrites `require` function, patches Node's `module` module to use the give *fs-like* object `vol` for module loading
5935

60-
# `patchRequire(vol[, unixifyPaths[, Module]])`
61-
62-
Patches Node's `module` module to use a given *fs-like* object `vol` for module loading.
63-
64-
- `vol` - fs-like object
65-
- `unixifyPaths` *(optional)* - whether to convert Windows paths to unix style paths, defaults to `false`.
66-
- `Module` *(optional)* - a module to patch, defaults to `require('module')`
67-
68-
Monkey-patches the `require` function in Node, this way you can make
69-
Node.js to *require* modules from your custom filesystem.
70-
71-
It expects an object with three filesystem methods implemented that are
72-
needed for the `require` function to work.
73-
74-
```js
75-
let vol = {
76-
readFileSync: () => {},
77-
realpathSync: () => {},
78-
statSync: () => {},
79-
};
80-
```
81-
82-
If you want to make Node.js to *require* your files from memory, you
83-
don't need to implement those functions yourself, just use the
84-
[`memfs`](https://github.com/streamich/memfs) package:
85-
86-
```js
87-
import {vol} from 'memfs';
88-
import {patchRequire} from 'fs-monkey';
89-
90-
vol.fromJSON({'/foo/bar.js': 'console.log("obi trice");'});
91-
patchRequire(vol);
92-
require('/foo/bar'); // obi trice
93-
```
94-
95-
Now the `require` function will only load the files from the `vol` file
96-
system, but not from the actual filesystem on the disk.
97-
98-
If you want the `require` function to load modules from both file
99-
systems, use the [`unionfs`](https://github.com/streamich/unionfs) package
100-
to combine both filesystems into a union:
101-
102-
```js
103-
import {vol} from 'memfs';
104-
import {patchRequire} from 'fs-monkey';
105-
import {ufs} from 'unionfs';
106-
import * as fs from 'fs';
107-
108-
vol.fromJSON({'/foo/bar.js': 'console.log("obi trice");'});
109-
ufs
110-
.use(vol)
111-
.use(fs);
112-
patchRequire(ufs);
113-
require('/foo/bar.js'); // obi trice
114-
```
11536

11637
[npm-img]: https://img.shields.io/npm/v/fs-monkey.svg
11738
[npm-url]: https://www.npmjs.com/package/fs-monkey
39+
[travis-url]: https://travis-ci.org/streamich/fs-monkey
40+
[travis-badge]: https://travis-ci.org/streamich/fs-monkey.svg?branch=master
11841

11942

12043
# License
12144

122-
This is free and unencumbered software released into the public domain.
123-
124-
Anyone is free to copy, modify, publish, use, compile, sell, or
125-
distribute this software, either in source code form or as a compiled
126-
binary, for any purpose, commercial or non-commercial, and by any
127-
means.
128-
129-
In jurisdictions that recognize copyright laws, the author or authors
130-
of this software dedicate any and all copyright interest in the
131-
software to the public domain. We make this dedication for the benefit
132-
of the public at large and to the detriment of our heirs and
133-
successors. We intend this dedication to be an overt act of
134-
relinquishment in perpetuity of all present and future rights to this
135-
software under copyright law.
136-
137-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
138-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
139-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
140-
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
141-
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
142-
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
143-
OTHER DEALINGS IN THE SOFTWARE.
144-
145-
For more information, please refer to <http://unlicense.org/>
45+
[Unlicense](./LICENSE) - public domain.

docs/api/patchFs.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
```

docs/api/patchRequire.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# `patchRequire(vol[, unixifyPaths[, Module]])`
2+
3+
Patches Node's `module` module to use a given *fs-like* object `vol` for module loading.
4+
5+
- `vol` - fs-like object
6+
- `unixifyPaths` *(optional)* - whether to convert Windows paths to unix style paths, defaults to `false`.
7+
- `Module` *(optional)* - a module to patch, defaults to `require('module')`
8+
9+
Monkey-patches the `require` function in Node, this way you can make
10+
Node.js to *require* modules from your custom filesystem.
11+
12+
It expects an object with three filesystem methods implemented that are
13+
needed for the `require` function to work.
14+
15+
```js
16+
let vol = {
17+
readFileSync: () => {},
18+
realpathSync: () => {},
19+
statSync: () => {},
20+
};
21+
```
22+
23+
If you want to make Node.js to *require* your files from memory, you
24+
don't need to implement those functions yourself, just use the
25+
[`memfs`](https://github.com/streamich/memfs) package:
26+
27+
```js
28+
import {vol} from 'memfs';
29+
import {patchRequire} from 'fs-monkey';
30+
31+
vol.fromJSON({'/foo/bar.js': 'console.log("obi trice");'});
32+
patchRequire(vol);
33+
require('/foo/bar'); // obi trice
34+
```
35+
36+
Now the `require` function will only load the files from the `vol` file
37+
system, but not from the actual filesystem on the disk.
38+
39+
If you want the `require` function to load modules from both file
40+
systems, use the [`unionfs`](https://github.com/streamich/unionfs) package
41+
to combine both filesystems into a union:
42+
43+
```js
44+
import {vol} from 'memfs';
45+
import {patchRequire} from 'fs-monkey';
46+
import {ufs} from 'unionfs';
47+
import * as fs from 'fs';
48+
49+
vol.fromJSON({'/foo/bar.js': 'console.log("obi trice");'});
50+
ufs
51+
.use(vol)
52+
.use(fs);
53+
patchRequire(ufs);
54+
require('/foo/bar.js'); // obi trice
55+
```

gulpfile.js

Lines changed: 0 additions & 15 deletions
This file was deleted.

package.json

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fs-monkey",
3-
"version": "0.2.2",
3+
"version": "0.0.0-development",
44
"description": "Monkey patches for file system related things.",
55
"main": "lib/index.js",
66
"keywords": [
@@ -19,46 +19,28 @@
1919
},
2020
"scripts": {
2121
"build": "babel src --out-dir lib",
22-
"test": "npm run test-coverage",
23-
"test-basic": "mocha --compilers js:babel-core/register src/**/*.test.js",
24-
"test-watch": "mocha --compilers js:babel-core/register src/**/*.test.js --watch",
25-
"test-coverage": "nyc --per-file mocha --compilers js:babel-core/register --require source-map-support/register --full-trace --bail src/**/*.test.js"
26-
},
27-
"dependencies": {
22+
"test": "jest",
23+
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
2824
},
25+
"dependencies": {},
2926
"devDependencies": {
30-
"mocha": "3.4.2",
31-
"chai": "4.1.0",
27+
"jest": "21.2.1",
28+
"babel-jest": "21.2.0",
3229
"babel-core": "6",
3330
"babel-cli": "6.24.1",
3431
"babel-preset-es2015": "6.24.1",
35-
"gulp": "3.9.1",
36-
"gulp-typescript": "3.2.1",
3732
"source-map-support": "0.4.15",
38-
"nyc": "11.1.0",
39-
"@types/mocha": "2.2.41",
40-
"@types/chai": "4.0.1",
33+
"semantic-release": "^8.2.0",
34+
"@types/jest": "21.1.8",
4135
"@types/node": "8.0.17"
4236
},
43-
"nyc": {
44-
"per-file": true,
45-
"include": [
37+
"jest": {
38+
"collectCoverageFrom": [
4639
"src/**/*.js"
4740
],
48-
"exclude": [
49-
"src/**/*.test.js"
50-
],
51-
"extension": [
52-
".js"
53-
],
54-
"reporter": [
55-
"text",
56-
"json",
57-
"lcov",
58-
"text-summary"
59-
],
60-
"sourceMap": true,
61-
"instrument": true,
62-
"cache": true
41+
"transform": {
42+
"^.+\\.jsx?$": "babel-jest"
43+
},
44+
"testRegex": ".*(__tests__/|/test/unit/).*(test|spec)\\.(t|j)sx?$"
6345
}
6446
}

0 commit comments

Comments
 (0)