Comparing version 0.2.2 to 0.3.0
@@ -12,2 +12,14 @@ 'use strict'; | ||
var fs = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : require('fs'); | ||
var bkp = {}; | ||
var patch = function patch(key, newValue) { | ||
bkp[key] = fs[key]; | ||
fs[key] = newValue; | ||
}; | ||
var patchMethod = function patchMethod(key) { | ||
return patch(key, vol[key].bind(vol)); | ||
}; | ||
var _iteratorNormalCompletion = true; | ||
@@ -21,3 +33,3 @@ var _didIteratorError = false; | ||
if (typeof vol[prop] !== 'undefined') fs[prop] = vol[prop]; | ||
if (typeof vol[prop] !== 'undefined') patch(prop, vol[prop]); | ||
} | ||
@@ -40,15 +52,15 @@ } catch (err) { | ||
if (typeof vol.StatWatcher === 'function') { | ||
fs.StatWatcher = vol.StatWatcher.bind(null, vol); | ||
patch('StatWatcher', vol.FSWatcher.bind(null, vol)); | ||
} | ||
if (typeof vol.FSWatcher === 'function') { | ||
fs.FSWatcher = vol.FSWatcher.bind(null, vol); | ||
patch('FSWatcher', vol.StatWatcher.bind(null, vol)); | ||
} | ||
if (typeof vol.ReadStream === 'function') { | ||
fs.ReadStream = vol.ReadStream.bind(null, vol); | ||
patch('ReadStream', vol.ReadStream.bind(null, vol)); | ||
} | ||
if (typeof vol.WriteStream === 'function') { | ||
fs.WriteStream = vol.WriteStream.bind(null, vol); | ||
patch('WriteStream', vol.WriteStream.bind(null, vol)); | ||
} | ||
if (typeof vol._toUnixTimestamp === 'function') fs._toUnixTimestamp = vol._toUnixTimestamp.bind(vol); | ||
if (typeof vol._toUnixTimestamp === 'function') patchMethod('_toUnixTimestamp'); | ||
@@ -63,3 +75,3 @@ var _iteratorNormalCompletion2 = true; | ||
if (typeof vol[method] === 'function') fs[method] = vol[method].bind(vol); | ||
if (typeof vol[method] === 'function') patchMethod(method); | ||
} | ||
@@ -89,3 +101,3 @@ } catch (err) { | ||
if (typeof vol[_method] === 'function') fs[_method] = vol[_method].bind(vol); | ||
if (typeof vol[_method] === 'function') patchMethod(_method); | ||
} | ||
@@ -106,2 +118,9 @@ } catch (err) { | ||
} | ||
return function unpatch() { | ||
console.log(bkp); | ||
for (var key in bkp) { | ||
fs[key] = bkp[key]; | ||
} | ||
}; | ||
}; |
@@ -1,64 +0,1 @@ | ||
{ | ||
"name": "fs-monkey", | ||
"version": "0.2.2", | ||
"description": "Monkey patches for file system related things.", | ||
"main": "lib/index.js", | ||
"keywords": [ | ||
"fs", | ||
"file", | ||
"file system", | ||
"monkey", | ||
"fsmonkey", | ||
"monkeyfs", | ||
"monkeypatch", | ||
"patch" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/streamich/fs-monkey.git" | ||
}, | ||
"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": { | ||
}, | ||
"devDependencies": { | ||
"mocha": "3.4.2", | ||
"chai": "4.1.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", | ||
"@types/node": "8.0.17" | ||
}, | ||
"nyc": { | ||
"per-file": true, | ||
"include": [ | ||
"src/**/*.js" | ||
], | ||
"exclude": [ | ||
"src/**/*.test.js" | ||
], | ||
"extension": [ | ||
".js" | ||
], | ||
"reporter": [ | ||
"text", | ||
"json", | ||
"lcov", | ||
"text-summary" | ||
], | ||
"sourceMap": true, | ||
"instrument": true, | ||
"cache": true | ||
} | ||
} | ||
{"name":"fs-monkey","version":"0.3.0","description":"Monkey patches for file system related things.","main":"lib/index.js","keywords":["fs","file","file system","monkey","fsmonkey","monkeyfs","monkeypatch","patch"],"repository":{"type":"git","url":"https://github.com/streamich/fs-monkey.git"},"scripts":{"build":"babel src --out-dir lib","test":"jest","semantic-release":"semantic-release pre && npm publish && semantic-release post"},"dependencies":{},"devDependencies":{"jest":"21.2.1","babel-jest":"21.2.0","babel-core":"6","babel-cli":"6.24.1","babel-preset-es2015":"6.24.1","source-map-support":"0.4.15","semantic-release":"^8.2.0","@types/jest":"21.1.8","@types/node":"8.0.17"},"jest":{"collectCoverageFrom":["src/**/*.js"],"transform":{"^.+\\.jsx?$":"babel-jest"},"testRegex":".*(__tests__/|/test/unit/).*(test|spec)\\.(t|j)sx?$"}} |
138
README.md
@@ -1,10 +0,18 @@ | ||
# `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 | ||
@@ -23,97 +31,12 @@ [filesystem API](https://nodejs.org/api/fs.html). | ||
`fs-monkey` **API** | ||
# Reference | ||
- [`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`](./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 | ||
# `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' ] | ||
``` | ||
# `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 | ||
@@ -123,25 +46,2 @@ | ||
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. |
@@ -5,7 +5,15 @@ import {fsProps, fsAsyncMethods, fsSyncMethods} from './util/lists'; | ||
export default function patchFs(vol, fs = require('fs')) { | ||
const bkp = {}; | ||
const patch = (key, newValue) => { | ||
bkp[key] = fs[key]; | ||
fs[key] = newValue; | ||
}; | ||
const patchMethod = key => patch(key, vol[key].bind(vol)); | ||
// General properties | ||
for(let prop of fsProps) | ||
if(typeof vol[prop] !== 'undefined') | ||
fs[prop] = vol[prop]; | ||
patch(prop, vol[prop]); | ||
@@ -17,12 +25,12 @@ | ||
if(typeof vol.StatWatcher === 'function') { | ||
fs.StatWatcher = vol.StatWatcher.bind(null, vol); | ||
patch('StatWatcher', vol.FSWatcher.bind(null, vol)); | ||
} | ||
if(typeof vol.FSWatcher === 'function') { | ||
fs.FSWatcher = vol.FSWatcher.bind(null, vol); | ||
patch('FSWatcher', vol.StatWatcher.bind(null, vol)); | ||
} | ||
if(typeof vol.ReadStream === 'function') { | ||
fs.ReadStream = vol.ReadStream.bind(null, vol); | ||
patch('ReadStream', vol.ReadStream.bind(null, vol)); | ||
} | ||
if(typeof vol.WriteStream === 'function') { | ||
fs.WriteStream = vol.WriteStream.bind(null, vol); | ||
patch('WriteStream', vol.WriteStream.bind(null, vol)); | ||
} | ||
@@ -33,3 +41,3 @@ | ||
if(typeof vol._toUnixTimestamp === 'function') | ||
fs._toUnixTimestamp = vol._toUnixTimestamp.bind(vol); | ||
patchMethod('_toUnixTimestamp'); | ||
@@ -40,7 +48,13 @@ | ||
if(typeof vol[method] === 'function') | ||
fs[method] = vol[method].bind(vol); | ||
patchMethod(method); | ||
for(let method of fsSyncMethods) | ||
if(typeof vol[method] === 'function') | ||
fs[method] = vol[method].bind(vol); | ||
patchMethod(method); | ||
// Give user back a method to revert the changes. | ||
return function unpatch () { | ||
console.log(bkp); | ||
for (const key in bkp) fs[key] = bkp[key]; | ||
}; | ||
}; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No License Found
License(Experimental) License information could not be found.
Found 1 instance in 1 package
37926
9
29
0
891
46