Skip to content

Commit

Permalink
start impl
Browse files Browse the repository at this point in the history
  • Loading branch information
Contra committed Dec 23, 2013
0 parents commit 35f230b
Show file tree
Hide file tree
Showing 19 changed files with 334 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.DS_Store
*.log
node_modules
build
*.node
components
6 changes: 6 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.DS_Store
*.log
node_modules
build
*.node
components
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
language: node_js
node_js:
- 0.7
- 0.8
- 0.9
- 0.10
- 0.11
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright (c) 2013 Fractal <[email protected]>

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.
90 changes: 90 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
[![Build Status](https://travis-ci.org/wearefractal/vinyl-fs.png?branch=master)](https://travis-ci.org/wearefractal/vinyl-fs)

[![NPM version](https://badge.fury.io/js/vinyl-fs.png)](http://badge.fury.io/js/vinyl-fs)

## Information

<table>
<tr>
<td>Package</td><td>vinyl-fs</td>
</tr>
<tr>
<td>Description</td>
<td>Vinyl adapter for the file system</td>
</tr>
<tr>
<td>Node Version</td>
<td>>= 0.4</td>
</tr>
</table>

## Usage

```javascript
var es = require('event-stream');
var fs = require('vinyl-fs');

var log = function(file, cb) {
console.log(file.path);
cb(null, file);
};

fs.src(["./js/**/*.js", "!./js/vendor/*.js"])
.pipe(es.map(log))
.pipe(fs.dest("./output"));
```

## API

### src(globs[, opt])

- Takes a glob string or an array of glob strings as the first argument.
- Possible options for the second argument:
- buffer - `true` or `false` if you want to buffer the file.
- Default value is `true`
- `false` will make file.contents a paused Stream
- read - `true` or `false` if you want the file to be read or not. Useful for stuff like `rm`ing files.
- Default value is `true`
- `false` will disable writing the file to disk via `.dest()`
- Any glob-related options are documented in [glob-stream] and [node-glob]
- Returns a Readable/Writable stream.
- On write the stream will simply pass items through.
- This stream emits matching [vinyl] File objects

### dest(folder[, opt])

- Takes a folder path as the first argument.
- Possible options for the second argument:
- cwd - Specify the working directory the folder is relative to. Default is `process.cwd()`
- Returns a Readable/Writable stream.
- On write the stream will save the [vinyl] File to disk at the folder/cwd specified.
- After writing the file to disk it will be emitted from the stream so you can keep piping these around

## LICENSE

(MIT License)

Copyright (c) 2013 Fractal <[email protected]>

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.

[glob-stream]: https://github.com/wearefractal/glob-stream
[node-glob]: https://github.com/isaacs/node-glob
[vinyl]: https://github.com/wearefractal/vinyl
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
src: require('./lib/createInputStream'),
dest: require('./lib/createOutputStream')
};
9 changes: 9 additions & 0 deletions lib/createInputStream/bufferFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var fs = require('fs');

module.exports = function (file, cb) {
fs.readFile(file.path, function (err, data) {
if (err) return cb(err);
file.contents = data;
cb(null, file);
});
};
23 changes: 23 additions & 0 deletions lib/createInputStream/getContents.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
var es = require('event-stream');
var fs = require('fs');

var readDir = require('./readDir');
var bufferFile = require('./bufferFile');
var streamFile = require('./streamFile');

module.exports = function(opt) {
return es.map(function (file, cb) {
// don't fail to read a directory
if (file.stat.isDirectory()) {
return readDir(file, cb);
}

// read and pass full contents
if (opt.buffer) {
return bufferFile(file, cb);
}

// dont buffer anything - just pass streams
return streamFile(file, cb);
});
};
12 changes: 12 additions & 0 deletions lib/createInputStream/getStats.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var es = require('event-stream');
var fs = require('fs');

module.exports = function(opt) {
return es.map(function (file, cb) {
fs.stat(file.path, function (err, stat) {
if (err) return cb(err);
file.stat = stat;
cb(null, file);
});
});
};
25 changes: 25 additions & 0 deletions lib/createInputStream/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
var es = require('event-stream');
var gs = require('glob-stream');

var File = require('gulp-util').File;
var getContents = require('./getContents');
var getStats = require('./getStats');

module.exports = function(glob, opt) {
if (!opt) opt = {};
if (typeof opt.read !== 'boolean') opt.read = true;
if (typeof opt.buffer !== 'boolean') opt.buffer = true;

var globStream = gs.create(glob, opt);
var formatStream = es.map(function(file, cb){
cb(null, new File(file));
});

var stream = globStream
.pipe(formatStream)
.pipe(getStats(opt));

if (!opt.read) return stream; // no reading required

return stream.pipe(getContents(opt));
};
4 changes: 4 additions & 0 deletions lib/createInputStream/readDir.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = function (file, cb) {
file.isDirectory = true;
cb(null, file);
};
12 changes: 12 additions & 0 deletions lib/createInputStream/streamFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var fs = require('fs');
var semver = require('semver');

module.exports = function (file, cb) {
file.contents = fs.createReadStream(file.path);

// pause it so it trickles through and gets listeners
// then dest will resume it
file.contents.pause();

cb(null, file);
};
26 changes: 26 additions & 0 deletions lib/createOutputStream/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
var es = require('event-stream');
var path = require('path');
var mkdirp = require('mkdirp');
var writeFile = require('./writeContents');
var writeDir = require('./writeDir');

module.exports = function(folder, opt) {
if (!opt) opt = {};
// TODO: support opt.cwd

folder = path.resolve(folder);

// TODO: clean this crap up
// createOutputStream should be a mirror of createInputStream file-wise
function saveFile (file, cb) {
var writePath = path.join(folder, file.relative);
var writeFolder = path.dirname(writePath);

mkdirp(writeFolder, function(err){
if (err) return cb(err);
writeFile(writePath, file, cb);
});
}
var stream = es.map(saveFile);
return stream;
};
1 change: 1 addition & 0 deletions lib/createOutputStream/writeBuffer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
writeBuffer.js
38 changes: 38 additions & 0 deletions lib/createOutputStream/writeContents.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
var fs = require('fs');
var semver = require('semver');
var gutil = require('gulp-util');

module.exports = function (writePath, file, cb) {
// if no contents then do nothing
if (file.isNull()) return cb(null, file);

// if directory then mkdirp it
// probably redundant since we are already doing an mkdirp
if (file.stat && file.stat.isDirectory()) {
writeDir(writePath, file, function(err){
if (err) return cb(err);
cb(null, file);
});
return;
}

// stream it to disk yo
if (file.isStream()) {
var outStream = fs.createWriteStream(writePath);
file.contents.once('end', function(){
cb(null, file);
});
file.contents.pipe(outStream);
file.contents.resume();
return;
}

if (file.isBuffer()) {
// write it like normal
fs.writeFile(writePath, file.contents, function(err){
if (err) return cb(err);
cb(null, file);
});
return;
}
};
9 changes: 9 additions & 0 deletions lib/createOutputStream/writeDir.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var mkdirp = require('mkdirp');

module.exports = function (writePath, file, cb) {
// create directory
mkdirp(writePath, function (err) {
if (err) return cb(err);
cb(null, file);
});
};
Empty file.
29 changes: 29 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "vinyl-fs",
"description": "Vinyl adapter for the file system",
"version": "0.0.1",
"homepage": "http://github.com/wearefractal/vinyl-fs",
"repository": "git://github.com/wearefractal/vinyl-fs.git",
"author": "Fractal <[email protected]> (http://wearefractal.com/)",
"main": "./index.js",
"dependencies": {
"vinyl": "~0.1.0",
"event-stream": "~3.0.20"
},
"devDependencies": {
"mocha": "*",
"should": "*"
},
"scripts": {
"test": "mocha --reporter spec"
},
"engines": {
"node": ">= 0.4.0"
},
"licenses": [
{
"type": "MIT",
"url": "http://github.com/wearefractal/vinyl-fs/raw/master/LICENSE"
}
]
}
13 changes: 13 additions & 0 deletions test/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var vfs = require('../');
var should = require('should');
require('mocha');

describe('vinyl-fs src', function() {
describe('src(glob)', function() {
it('should emit files', function(done) {
should.exist(true);
true.should.equal["true"];
done();
});
});
});

0 comments on commit 35f230b

Please sign in to comment.