Skip to content

Commit

Permalink
feat: add unpatch() method #4
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Dec 1, 2017
1 parent 65e6e86 commit 64799fc
Show file tree
Hide file tree
Showing 11 changed files with 3,189 additions and 200 deletions.
21 changes: 18 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
language: node_js
node_js:
- "8"
os:
- linux
cache:
yarn: true
directories:
- "node_modules"
- ~/.npm
notifications:
email: false
node_js:
- '8'
script:
- npm run test
- npm run build
matrix:
allow_failures: []
fast_finish: true
after_success:
- npm run semantic-release
branches:
except:
- /^v\d+\.\d+\.\d+$/
24 changes: 24 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <https://unlicense.org>
138 changes: 19 additions & 119 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
# `fs-monkey`
# fs-monkey

[![][npm-img]][npm-url]
[![][npm-img]][npm-url] [![][travis-badge]][travis-url]

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

**Terms**
- Rewrite `require` function to load Node's modules from memory.
- Or rewrite the whole `fs` filesystem module.

### Install

```shell
npm install --save fs-monkey
```

# Terms

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


`fs-monkey` **API**

- [`patchFs(vol[, fs])`](#patchfsvol-fs) - rewrites Node's filesystem module `fs` with *fs-like* object `vol`
- [`patchRequire(vol[, Module])`](#patchrequirevol-module) - rewrites `require` function, patches Node's `module` module to use a give *fs-like* object `vol` for module loading


# `patchFs(vol[, fs])`

Rewrites Node's filesystem module `fs` with *fs-like* object.

- `vol` - fs-like object
- `fs` *(optional)* - a filesystem to patch, defaults to `require('fs')`

```js
import {patchFs} from 'fs-monkey';

const myfs = {
readFileSync: () => 'hello world',
};

patchFs(myfs);
console.log(require('fs').readFileSync('/foo/bar')); // hello world
```

You don't need to create *fs-like* objects yourself, use [`memfs`](https://github.com/streamich/memfs)
to create a virtual filesystem for you:

```js
import {vol} from 'memfs';
import {patchFs} from 'fs-monkey';

vol.fromJSON({'/dir/foo': 'bar'});
patchFs(vol);
console.log(require('fs').readdirSync('/')); // [ 'dir' ]
```
# Reference

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

# `patchRequire(vol[, unixifyPaths[, Module]])`

Patches Node's `module` module to use a given *fs-like* object `vol` for module loading.

- `vol` - fs-like object
- `unixifyPaths` *(optional)* - whether to convert Windows paths to unix style paths, defaults to `false`.
- `Module` *(optional)* - a module to patch, defaults to `require('module')`

Monkey-patches the `require` function in Node, this way you can make
Node.js to *require* modules from your custom filesystem.

It expects an object with three filesystem methods implemented that are
needed for the `require` function to work.

```js
let vol = {
readFileSync: () => {},
realpathSync: () => {},
statSync: () => {},
};
```

If you want to make Node.js to *require* your files from memory, you
don't need to implement those functions yourself, just use the
[`memfs`](https://github.com/streamich/memfs) package:

```js
import {vol} from 'memfs';
import {patchRequire} from 'fs-monkey';

vol.fromJSON({'/foo/bar.js': 'console.log("obi trice");'});
patchRequire(vol);
require('/foo/bar'); // obi trice
```

Now the `require` function will only load the files from the `vol` file
system, but not from the actual filesystem on the disk.

If you want the `require` function to load modules from both file
systems, use the [`unionfs`](https://github.com/streamich/unionfs) package
to combine both filesystems into a union:

```js
import {vol} from 'memfs';
import {patchRequire} from 'fs-monkey';
import {ufs} from 'unionfs';
import * as fs from 'fs';

vol.fromJSON({'/foo/bar.js': 'console.log("obi trice");'});
ufs
.use(vol)
.use(fs);
patchRequire(ufs);
require('/foo/bar.js'); // obi trice
```

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


# License

This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <http://unlicense.org/>
[Unlicense](./LICENSE) - public domain.
29 changes: 29 additions & 0 deletions docs/api/patchFs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# `patchFs(vol[, fs])`

Rewrites Node's filesystem module `fs` with *fs-like* object.

- `vol` - fs-like object
- `fs` *(optional)* - a filesystem to patch, defaults to `require('fs')`

```js
import {patchFs} from 'fs-monkey';

const myfs = {
readFileSync: () => 'hello world',
};

patchFs(myfs);
console.log(require('fs').readFileSync('/foo/bar')); // hello world
```

You don't need to create *fs-like* objects yourself, use [`memfs`](https://github.com/streamich/memfs)
to create a virtual filesystem for you:

```js
import {vol} from 'memfs';
import {patchFs} from 'fs-monkey';

vol.fromJSON({'/dir/foo': 'bar'});
patchFs(vol);
console.log(require('fs').readdirSync('/')); // [ 'dir' ]
```
55 changes: 55 additions & 0 deletions docs/api/patchRequire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# `patchRequire(vol[, unixifyPaths[, Module]])`

Patches Node's `module` module to use a given *fs-like* object `vol` for module loading.

- `vol` - fs-like object
- `unixifyPaths` *(optional)* - whether to convert Windows paths to unix style paths, defaults to `false`.
- `Module` *(optional)* - a module to patch, defaults to `require('module')`

Monkey-patches the `require` function in Node, this way you can make
Node.js to *require* modules from your custom filesystem.

It expects an object with three filesystem methods implemented that are
needed for the `require` function to work.

```js
let vol = {
readFileSync: () => {},
realpathSync: () => {},
statSync: () => {},
};
```

If you want to make Node.js to *require* your files from memory, you
don't need to implement those functions yourself, just use the
[`memfs`](https://github.com/streamich/memfs) package:

```js
import {vol} from 'memfs';
import {patchRequire} from 'fs-monkey';

vol.fromJSON({'/foo/bar.js': 'console.log("obi trice");'});
patchRequire(vol);
require('/foo/bar'); // obi trice
```

Now the `require` function will only load the files from the `vol` file
system, but not from the actual filesystem on the disk.

If you want the `require` function to load modules from both file
systems, use the [`unionfs`](https://github.com/streamich/unionfs) package
to combine both filesystems into a union:

```js
import {vol} from 'memfs';
import {patchRequire} from 'fs-monkey';
import {ufs} from 'unionfs';
import * as fs from 'fs';

vol.fromJSON({'/foo/bar.js': 'console.log("obi trice");'});
ufs
.use(vol)
.use(fs);
patchRequire(ufs);
require('/foo/bar.js'); // obi trice
```
15 changes: 0 additions & 15 deletions gulpfile.js

This file was deleted.

46 changes: 14 additions & 32 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fs-monkey",
"version": "0.2.2",
"version": "0.0.0-development",
"description": "Monkey patches for file system related things.",
"main": "lib/index.js",
"keywords": [
Expand All @@ -19,46 +19,28 @@
},
"scripts": {
"build": "babel src --out-dir lib",
"test": "npm run test-coverage",
"test-basic": "mocha --compilers js:babel-core/register src/**/*.test.js",
"test-watch": "mocha --compilers js:babel-core/register src/**/*.test.js --watch",
"test-coverage": "nyc --per-file mocha --compilers js:babel-core/register --require source-map-support/register --full-trace --bail src/**/*.test.js"
},
"dependencies": {
"test": "jest",
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
},
"dependencies": {},
"devDependencies": {
"mocha": "3.4.2",
"chai": "4.1.0",
"jest": "21.2.1",
"babel-jest": "21.2.0",
"babel-core": "6",
"babel-cli": "6.24.1",
"babel-preset-es2015": "6.24.1",
"gulp": "3.9.1",
"gulp-typescript": "3.2.1",
"source-map-support": "0.4.15",
"nyc": "11.1.0",
"@types/mocha": "2.2.41",
"@types/chai": "4.0.1",
"semantic-release": "^8.2.0",
"@types/jest": "21.1.8",
"@types/node": "8.0.17"
},
"nyc": {
"per-file": true,
"include": [
"jest": {
"collectCoverageFrom": [
"src/**/*.js"
],
"exclude": [
"src/**/*.test.js"
],
"extension": [
".js"
],
"reporter": [
"text",
"json",
"lcov",
"text-summary"
],
"sourceMap": true,
"instrument": true,
"cache": true
"transform": {
"^.+\\.jsx?$": "babel-jest"
},
"testRegex": ".*(__tests__/|/test/unit/).*(test|spec)\\.(t|j)sx?$"
}
}
Loading

0 comments on commit 64799fc

Please sign in to comment.