Skip to content

Commit

Permalink
first commit v0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Aug 15, 2017
0 parents commit c9c56b3
Show file tree
Hide file tree
Showing 17 changed files with 643 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"presets": ["es2015"],
"comments": false,
"ignore": [
"spec.js",
"test.js"
]
}
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.idea
node_modules
/.idea
.nyc_output
coverage
package-lock.json
yarn.lock
13 changes: 13 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
*.ts
/typings
example.js
tsd.json
simple.js
node_modules
/test
.idea
.idea/
.nyc_output
coverage
package-lock.json
yarn.lock
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
8.2.1
16 changes: 16 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
sudo: false
os:
- linux
language: node_js
node_js:
- '8'
- '7'
- '6'
- '5'
matrix:
allow_failures: []
fast_finish: true
cache:
yarn: true
directories:
- "node_modules"
141 changes: 141 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
# spyfs [![][npm-img]][npm-url]

Spies on filesystem calls.

Install:

npm install --save spyfs

or

yarn add spyfs

Create a new file system that spies:

```js
import * as fs from 'fs';
import {spy} from 'spyfs';


const sfs = spy(fs);
```

Subscribe to all actions happening on that filesystem:

```js
sfs.subscribe(promise => {
// ...
});
```

Now every time somebody uses `sfs` the subscription callback will be called.

You can also subscribe by providing a listener at creation:

```js
const sfs = spy(fs, promise => {
// ...
});
```

### Use `async/await`

`spyfs` returns *actions* which are instances of `Promise` constructor,
so you can use *asynchronous* functions for convenience:

```js
const sfs = spy(fs, async function(action) {
console.log(await action); // prints directory files...
});

sfs.readdir(__dirname, () => {});
```

### Use with [`memfs`][memfs]

You can use `spyfs` with any *fs-like* object, including [`memfs`][memfs]:

```
import {fs} from 'memfs';
import {spy} from "../src/index";
const sfs = spy(fs, async function(action) {
console.log(await action); // bar
});
sfs.writeFile('/foo', 'bar', () => {});
sfs.readFile('/foo', 'utf8', () => {});
```

### Action properties

`spyfs` actions have extra properties that tell you more about the action
being executed:

- `action.method` *(string)* - name of filesystem method called.
- `action.isAsync` *(boolean)* - whether the filesystem method called was asynchronous.
- `action.args` *(Array)* - list of arguments with which the method was called (sans callback).

```js
const sfs = spy(fs, action => {
console.log(action.method); // readdir, readdirSync
console.log(action.isAsync); // true, false
console.log(action.args); // [ '/' ], [ '/' ]
});

sfs.readdir('/', () => {});
sfs.readdirSync('/');
```

### Subscribe to events

The returned filesystem object is also an event emitter, you can subscribe
to specific filesystem actions using the `.on()` method:

```js
sfs.on('readdirSync', async function(action) {
console.log(action.args, await action);
});

sfs.readdirSync('/');
```


[npm-url]: https://www.npmjs.com/package/spyfs
[npm-img]: https://img.shields.io/npm/v/spyfs.svg
[memfs]: https://github.com/streamich/memfs
[unionfs]: https://github.com/streamich/unionfs
[linkfs]: https://github.com/streamich/linkfs
[spyfs]: https://github.com/streamich/spyfs
[fs-monkey]: https://github.com/streamich/fs-monkey





# 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/>
17 changes: 17 additions & 0 deletions demo/basic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as fs from 'fs';
import {spy} from '../src';


const sfs = spy(fs);
sfs.subscribe(async function(action) {
const result = await action;
console.log(result);
});

sfs.on('readFile', async function(action) {
const result = await action;
console.log(result);
});

sfs.readFile('./index.js', 'utf8', () => {});

11 changes: 11 additions & 0 deletions demo/events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import * as fs from 'fs';
import {spy} from "../src/index";


const sfs = spy(fs);

sfs.on('readdirSync', async function(action) {
console.log(action.args, await action);
});

sfs.readdirSync('/');
10 changes: 10 additions & 0 deletions demo/memfs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {fs} from 'memfs';
import {spy} from "../src/index";


const sfs = spy(fs, async function(action) {
console.log(await action);
});

sfs.writeFile('/foo', 'bar', () => {});
sfs.readFile('/foo', 'utf8', () => {});
13 changes: 13 additions & 0 deletions demo/promise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import * as fs from 'fs';
import {spy} from "../src/index";


const sfs = spy(fs, action => {
action.catch(err => {
console.log(err.message); // ENOENT: no such file or directory, access '/foo'
})
});

try {
sfs.accessSync('/foo');
} catch (err) {}
12 changes: 12 additions & 0 deletions demo/props.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import * as fs from 'fs';
import {spy} from "../src/index";


const sfs = spy(fs, action => {
console.log(action.method); // readdir, readdirSync
console.log(action.isAsync); // true, false
console.log(action.args); // [ '/' ], [ '/' ]
});

sfs.readdir('/', () => {});
sfs.readdirSync('/');
9 changes: 9 additions & 0 deletions demo/readdir.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as fs from 'fs';
import {spy} from "../src/index";


const sfs = spy(fs, async function(action) {
console.log(await action); // prints directory files...
});

sfs.readdir(__dirname, () => {});
15 changes: 15 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var gulp = require('gulp');
var ts = require('gulp-typescript');


gulp.task('build-ts', function () {
return gulp.src('src/**/*.ts')
.pipe(ts({
"target": "es5",
"module": "commonjs",
"removeComments": false,
"noImplicitAny": false,
"sourceMap": false,
}))
.pipe(gulp.dest('lib'));
});
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('lib/index');
Loading

0 comments on commit c9c56b3

Please sign in to comment.