Skip to content

Commit

Permalink
New: First implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
yocontra authored and phated committed Sep 27, 2016
0 parents commit f8933ca
Show file tree
Hide file tree
Showing 18 changed files with 930 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
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
language: node_js
node_js:
- "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 <contact@wearefractal.com>

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

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

## Information

<table>
<tr>
<td>Package</td><td>vinyl</td>
</tr>
<tr>
<td>Description</td>
<td>A virtual file format</td>
</tr>
<tr>
<td>Node Version</td>
<td>>= 0.9</td>
</tr>
</table>

## File

```javascript
var File = require('vinyl');

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

### constructor(options)

#### options.cwd

Type: `String`
Default: `process.cwd()`

#### options.base

Used for relative pathing. Typically where a glob starts.

Type: `String`
Default: `options.cwd`

#### options.path

Full path to the file.

Type: `String`
Default: `null`

#### options.stat

The result of an fs.stat call. See [fs.Stats](http://nodejs.org/api/fs.html#fs_class_fs_stats) for more information.

Type: `fs.Stats`
Default: `null`

#### options.contents

File contents.

Type: `Buffer, Stream, or null`
Default: `null`

### isBuffer()

Returns true if file.contents is a Buffer.

### isStream()

Returns true if file.contents is a Stream.

### isNull()

Returns true if file.contents is null.

### clone()

Returns a new File object with all attributes cloned.

### pipe(stream)

If file.contents is a Buffer, it will write it to the stream.

If file.contents is a Stream, it will pipe it to the stream.

If file.contents is null, it will do nothing.

Returns the stream.

### inspect()

Returns a pretty String interpretation of the File. Useful for console.log.

### relative

Returns path.relative for the file base and file path.

Example:

```javascript
var file = new File({
cwd: "/",
base: "/test/",
path: "/test/file.coffee"
});

console.log(file.relative); // file.coffee
```


## LICENSE

(MIT License)

Copyright (c) 2013 Fractal <contact@wearefractal.com>

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.
109 changes: 109 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
var path = require('path');

var clone = require('lodash.clone');

var isBuffer = require('./lib/isBuffer');
var isStream = require('./lib/isStream');
var isNull = require('./lib/isNull');
var inspectStream = require('./lib/inspectStream');
var cloneBuffer = require('./lib/cloneBuffer');

function File(file) {
if (!file) file = {};

this.cwd = file.cwd || process.cwd();
this.base = file.base || this.cwd;
this.path = file.path || null;

// stat = fs stats object
this.stat = file.stat || null;

// contents = stream, buffer, or null if not read
this.contents = file.contents || null;
}

File.prototype.isBuffer = function() {
return isBuffer(this.contents);
};

File.prototype.isStream = function() {
return isStream(this.contents);
};

File.prototype.isNull = function() {
return isNull(this.contents);
};

File.prototype.clone = function() {
var clonedStat = clone(this.stat);
var clonedContents = this.isBuffer() ? cloneBuffer(this.contents) : this.contents;

return new File({
cwd: this.cwd,
base: this.base,
path: this.path,
stat: clonedStat,
contents: clonedContents
});
};

File.prototype.pipe = function(stream) {
if (this.isStream()) {
return this.contents.pipe(stream);
}
if (this.isBuffer()) {
stream.write(this.contents);
return stream;
}

// must be null, dont do anything
return stream;
};

File.prototype.inspect = function() {
var inspect = [];

// use relative path if possible
var filePath = (this.base && this.path) ? this.relative : this.path;

if (filePath) {
inspect.push('"'+filePath+'"');
}

if (this.isBuffer()) {
inspect.push(this.contents.inspect());
}

if (this.isStream()) {
inspect.push(inspectStream(this.contents));
}

return '<File '+inspect.join(' ')+'>';
};

// virtual attributes
// or stuff with extra logic
Object.defineProperty(File.prototype, 'contents', {
get: function() {
return this._contents;
},
set: function(val) {
if (!isBuffer(val) && !isStream(val) && !isNull(val)) {
throw new Error("File.contents can only be a Buffer, a Stream, or null.");
}
this._contents = val;
}
});

Object.defineProperty(File.prototype, 'relative', {
get: function() {
if (!this.base) throw new Error('No base specified! Can not get relative.');
if (!this.path) throw new Error('No path specified! Can not get relative.');
return path.relative(this.base, this.path);
},
set: function() {
throw new Error('File.relative is generated from the base and path attributes. Do not modify it.');
}
});

module.exports = File;
7 changes: 7 additions & 0 deletions lib/cloneBuffer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var Buffer = require('buffer').Buffer;

module.exports = function(buf) {
var out = new Buffer(buf.length);
buf.copy(out);
return out;
};
12 changes: 12 additions & 0 deletions lib/inspectStream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var Stream = require('stream').Stream;
var isStream = require('./isStream');

module.exports = function(stream) {
if (!isStream(stream)) return;

var streamType = stream.constructor.name;
// avoid StreamStream
if (streamType === 'Stream') streamType = '';

return '<'+streamType+'Stream>';
};
7 changes: 7 additions & 0 deletions lib/isBuffer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var buf = require('buffer');
var Buffer = buf.Buffer;

// could use Buffer.isBuffer but this is the same exact thing...
module.exports = function(o) {
return typeof o === 'object' && o instanceof Buffer;
};
3 changes: 3 additions & 0 deletions lib/isNull.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function(v) {
return v === null;
};
5 changes: 5 additions & 0 deletions lib/isStream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var Stream = require('stream').Stream;

module.exports = function(o) {
return !!o && o instanceof Stream;
};
29 changes: 29 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "vinyl",
"description": "A virtual file format",
"version": "0.0.1",
"homepage": "http://github.com/wearefractal/vinyl",
"repository": "git://github.com/wearefractal/vinyl.git",
"author": "Fractal <contact@wearefractal.com> (http://wearefractal.com/)",
"main": "./index.js",
"dependencies": {
"lodash.clone": "~2.4.1"
},
"devDependencies": {
"mocha": "*",
"should": "*",
"event-stream": "~3.0.20"
},
"scripts": {
"test": "mocha --reporter spec"
},
"engines": {
"node": ">= 0.9.0"
},
"licenses": [
{
"type": "MIT",
"url": "http://github.com/wearefractal/vinyl/raw/master/LICENSE"
}
]
}
Loading

0 comments on commit f8933ca

Please sign in to comment.