Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#172 - Support alternative fs implementations. #173

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ Plugins are executed in a pipeline, and register which event they should be exec

## Tests

```
npm install yarn -g
```

``` javascript
npm test
```
Expand Down
42 changes: 22 additions & 20 deletions lib/NodeJsInputFileSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,30 @@
*/
"use strict";

const fs = require("graceful-fs");
const defaultFs = require("graceful-fs");

const fsMethods = [
"stat",
"statSync",
"readFile",
"readFileSync",
"readlink",
"readlinkSync"
];

class NodeJsInputFileSystem {
constructor(fs) {
var fileSystem = (this.fs = fs || defaultFs);
for (const key of fsMethods) {
Object.defineProperty(this, key, {
configurable: true,
writable: true,
value: fileSystem[key].bind(fileSystem)
});
}
}
readdir(path, callback) {
fs.readdir(path, (err, files) => {
this.fs.readdir(path, (err, files) => {
callback(
err,
files &&
Expand All @@ -20,7 +39,7 @@ class NodeJsInputFileSystem {
}

readdirSync(path) {
const files = fs.readdirSync(path);
const files = this.fs.readdirSync(path);
return (
files &&
files.map(file => {
Expand All @@ -30,21 +49,4 @@ class NodeJsInputFileSystem {
}
}

const fsMethods = [
"stat",
"statSync",
"readFile",
"readFileSync",
"readlink",
"readlinkSync"
];

for (const key of fsMethods) {
Object.defineProperty(NodeJsInputFileSystem.prototype, key, {
configurable: true,
writable: true,
value: fs[key].bind(fs)
});
}

module.exports = NodeJsInputFileSystem;
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"lint-staged": "^8.1.0",
"mocha": "^2.3.4",
"prettier": "^1.15.2",
"should": "^8.0.2"
"should": "^8.0.2",
"memfs": "^2.14.1"
},
"engines": {
"node": ">=6.9.0"
Expand Down
67 changes: 67 additions & 0 deletions test/NodeJsInputFileSystem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
var nodeJsInputFileSystem = require("../lib/NodeJsInputFileSystem");
var should = require("should");

describe("NodeJsInputFileSystem", function() {
this.timeout(3000);
beforeEach(function() {});
afterEach(function() {
//fs.purge();
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need remove this


it("shoud not require fs argument in constructor", function(done) {
var nodeFs = new nodeJsInputFileSystem();
should.exist(nodeFs);
done();
});

it("shoud allow fs argument in constructor", function(done) {
const volumeDir = "app";
//const unionfs = require("unionfs");
const memfs = require("memfs");
// mount files specified by "mockedFiles.js.amd" to "app" base directory.
const json = {
"./README.md": "1"
};
var vol = memfs.Volume.fromJSON(json, `${volumeDir}`);
//var ufs = unionfs.ufs.use(vol);
var nodeFs = new nodeJsInputFileSystem(vol);
should.exist(nodeFs);
done();
});

it("should call methods on fs provided", function(done) {
const volumeDir = "app";
//const unionfs = require("unionfs");
const memfs = require("memfs");
// mount files specified by "mockedFiles.js.amd" to "app" base directory.
const json = {
"./README.md": "1"
};
var vol = memfs.Volume.fromJSON(json, `${volumeDir}`);
var nodeFs = new nodeJsInputFileSystem(vol);

// stat
// statSync
nodeFs.stat("app/README.md", (err, stats) => {
should.exist(stats);
should.not.exist(err);
var statsSync = nodeFs.statSync("app/README.md");
should.exist(statsSync);
done();
});
});

it("should call methods on default fs when no fs provided", function(done) {
var nodeFs = new nodeJsInputFileSystem();

// stat
// statSync
nodeFs.stat("README.md", (err, stats) => {
should.exist(stats);
should.not.exist(err);
var statsSync = nodeFs.statSync("README.md");
should.exist(statsSync);
done();
});
});
});