Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
SBoudrias committed Oct 16, 2014
0 parents commit 11fdc31
Show file tree
Hide file tree
Showing 11 changed files with 270 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# http://editorconfig.org
root = true

[*]
indent_style = space
indent_size = 2
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
44 changes: 44 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"node" : true,
"bitwise" : true,
"boss" : true,
"camelcase" : true,
"debug" : true,
"eqeqeq" : true,
"eqnull" : true,
"expr" : true,
"immed" : true,
"iterator" : true,
"lastsemic" : true,
"laxbreak" : true,
"laxcomma" : true,
"loopfunc" : true,
"maxlen" : 120,
"multistr" : true,
"newcap" : true,
"noarg" : true,
"onecase" : true,
"onevar" : false,
"plusplus" : false,
"proto" : true,
"quotmark" : "single",
"regexdash" : true,
"regexp" : true,
"scripturl" : true,
"shadow" : true,
"smarttabs" : true,
"strict" : false,
"sub" : true,
"supernew" : true,
"undef" : true,
"validthis" : true,
"withstmt" : true,
"globals": {
"describe": true,
"it": true,
"before": true,
"after": true,
"beforeEach": true,
"afterEach": true
}
}
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
language: node_js
node_js:
- "0.10"
20 changes: 20 additions & 0 deletions LICENSE-MIT
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2013 Simon Boudrias

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

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 OR
COPYRIGHT HOLDERS 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.
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
mem-fs
=============

Simple in-memory vinyl file store.


Usage
-------------

### Loading a file

You access a file using `store#get()` method. If the file is in memory, it will be used. Otherwise, we'll load the file from the file-system.

```js
var store = require('mem-fs').create();

store.get('/test/file.txt');
```

### Adding/updating a file

You update file references by using `store#add()` method. This method take a `vinyl` file object as parameter.

```js
var File = require('vinyl');
var store = require('mem-fs').create();

var coffeeFile = new File({
cwd: '/',
base: '/test/',
path: '/test/file.coffee',
contents: new Buffer('test = 123')
});

store.add(coffeeFile);
```

### Iterating over the file system

Using `store#each(cb(file, index))`, you can iterate over every file stored in the file system.
44 changes: 44 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use strict';
var path = require('path');
var vinylFile = require('vinyl-file');
var File = require('vinyl');

exports.create = function () {
var store = {};

function load(filepath) {
var file;
try {
file = vinylFile.readSync(filepath);
} catch (err) {
file = new File({
cwd: process.cwd(),
base: path.basename(filepath),
path: filepath,
contents: new Buffer('')
});
file.state = 'unexistent';
}
store[filepath] = file;
return file;
}

return {
get: function (filepath) {
filepath = path.resolve(filepath);
return store[filepath] || load(filepath);
},

add: function (file) {
store[file.path] = file;
return this;
},

each: function (onEach) {
Object.keys(store).forEach(function (key, index) {
onEach(store[key], index);
});
return this;
}
};
};
26 changes: 26 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "mem-fs",
"version": "1.0.0",
"description": "Simple memory based file-system",
"main": "index.js",
"scripts": {
"test": "mocha"
},
"repository": {
"type": "git",
"url": "https://github.com/SBoudrias/mem-fs"
},
"author": "Simon Boudrias",
"license": "MIT",
"bugs": {
"url": "https://github.com/SBoudrias/mem-fs/issues"
},
"homepage": "https://github.com/SBoudrias/mem-fs",
"dependencies": {
"vinyl-file": "^1.1.0"
},
"devDependencies": {
"vinyl": "^0.4.3",
"mocha": "*"
}
}
1 change: 1 addition & 0 deletions test/fixtures/file-a.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo
1 change: 1 addition & 0 deletions test/fixtures/file-b.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo
78 changes: 78 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
'use strict';

var assert = require('assert');
var path = require('path');
var File = require('vinyl');

var memFs = require('..');

var fixtureA = 'fixtures/file-a.txt';
var fixtureB = 'fixtures/file-b.txt';
var absentFile = 'fixture/does-not-exist.txt';
var coffeeFile = new File({
cwd: '/',
base: '/test/',
path: '/test/file.coffee',
contents: new Buffer('test = 123')
});

describe('mem-fs', function () {
beforeEach(function() {
process.chdir(__dirname);
this.store = memFs.create();
});

describe('#get() / #add()', function () {
it('load file from disk', function () {
var file = this.store.get(fixtureA);
assert.equal(file.contents.toString(), 'foo\n');
assert.equal(file.cwd, process.cwd());
assert.equal(file.path, path.resolve(fixtureA));
});

it('get/modify/add a file', function () {
var file = this.store.get(fixtureA);
file.contents = new Buffer('bar');
this.store.add(file);
var file2 = this.store.get(fixtureA);
assert.equal(file2.contents.toString(), 'bar');
});

it('retrive file from memory', function () {
this.store.add(coffeeFile);
var file = this.store.get('/test/file.coffee');
assert.equal(file.contents.toString(), 'test = 123');
});

it('returns empty file reference if file does not exist', function () {
var file = this.store.get(absentFile);
assert.equal(file.contents.toString(), '');
assert.equal(file.path, path.resolve(absentFile));
assert.equal(file.state, 'unexistent');
});
});

describe('#add()', function () {
it('is chainable', function () {
assert.equal(this.store.add(coffeeFile), this.store);
});
});

describe('#each()', function () {
beforeEach(function() {
this.store.get(fixtureA);
this.store.get(fixtureB);
});

it('iterates over every file', function () {
var files = [fixtureA, fixtureB];
this.store.each(function (file, index) {
assert.equal(path.resolve(files[index]), file.path);
});
});

it('is chainable', function () {
assert.equal(this.store.each(function () {}), this.store);
});
});
});

0 comments on commit 11fdc31

Please sign in to comment.