/vinyl

Virtual file format

Primary LanguageJavaScriptMIT LicenseMIT

vinyl NPM version Build Status Coveralls Status Dependency Status

Information

Packagevinyl
DescriptionA virtual file format
Node Version>= 0.9

What is this?

Read this for more info about how this plays into the grand scheme of things https://medium.com/@eschoff/3828e8126466

File

var File = require('vinyl');

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

isVinyl

When checking if an object is a vinyl file, you should not use instanceof. Use the isVinyl function instead.

var File = require('vinyl');

var dummy = new File({stuff});
var notAFile = {};

File.isVinyl(dummy); // true
File.isVinyl(notAFile); // false

isCustomProp

Vinyl checks if a property is not managed internally, such as sourceMap. This is than used in constructor(options) when setting, and clone() when copying properties.

var File = require('vinyl');

File.isCustomProp('sourceMap'); // true
File.isCustomProp('path'); // false -> internal getter/setter

Read more in Extending Vinyl.

constructor(options)

All internally managed paths (cwd, base, path, [history]) are normalized and remove a trailing separator.

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: undefined

options.history

Path history. Has no effect if options.path is passed.

Type: Array

Default: options.path ? [options.path] : []

options.stat

The result of an fs.stat call. This is how you mark the file as a directory. See isDirectory() and fs.Stats for more information.

Type: fs.Stats

Default: null

options.contents

File contents.

Type: Buffer, Stream, or null

Default: null

options.{custom}

Any other option properties will just be assigned to the new File object.

var File = require('vinyl');

var file = new File({foo: 'bar'});
file.foo === 'bar'; // true

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.

isDirectory()

Returns true if file is a directory. File is considered a directory when:

  • file.isNull() is true
  • file.stat is an object
  • file.stat.isDirectory() returns true

When constructing a Vinyl object, pass in a valid fs.Stats object via options.stat. If you are mocking the fs.Stats object, you may need to stub the isDirectory() method.

isSymbolic()

Returns true if file is a symbolic link. File is considered symbolic when:

  • file.isNull() is true
  • file.stat is an object
  • file.stat.isSymbolicLink() returns true

When constructing a Vinyl object, pass in a valid fs.Stats object via options.stat. If you are mocking the fs.Stats object, you may need to stub the isSymbolicLink() method.

clone([opt])

Returns a new File object with all attributes cloned.
By default custom attributes are deep-cloned.

If opt or opt.deep is false, custom attributes will not be deep-cloned.

If opt.contents is false, it will copy file.contents Buffer's reference.

pipe(stream[, opt])

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.

If opt.end is false, the destination stream will not be ended (same as node core).

Returns the stream.

inspect()

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

contents

The Stream or Buffer of the file as it was passed in via options, or as the result of modification.

For example:

if (file.isBuffer()) {
    console.log(file.contents.toString()); // logs out the string of contents
}

cwd

Gets and sets current working directory. Defaults to process.cwd. Will always be normalized and remove a trailing separator.

base

Gets and sets base directory. Used for relative pathing (typically where a glob starts). When null or undefined, it simply proxies the file.cwd property. Will always be normalized and remove a trailing separator.

path

Absolute pathname string or undefined. Setting to a different value pushes the old value to history. All new values are normalized and remove a trailing separator.

history

Array of path values the file object has had, from history[0] (original) through history[history.length - 1] (current). history and its elements should normally be treated as read-only and only altered indirectly by setting path.

relative

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

Example:

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

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

dirname

Gets and sets path.dirname for the file path. Will always be normalized and remove a trailing separator.

Example:

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

console.log(file.dirname); // /test

file.dirname = '/specs';

console.log(file.dirname); // /specs
console.log(file.path); // /specs/file.coffee

basename

Gets and sets path.basename for the file path.

Example:

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

console.log(file.basename); // file.coffee

file.basename = 'file.js';

console.log(file.basename); // file.js
console.log(file.path); // /test/file.js

stem

Gets and sets stem (filename without suffix) for the file path.

Example:

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

console.log(file.stem); // file

file.stem = 'foo';

console.log(file.stem); // foo
console.log(file.path); // /test/foo.coffee

extname

Gets and sets path.extname for the file path.

Example:

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

console.log(file.extname); // .coffee

file.extname = '.js';

console.log(file.extname); // .js
console.log(file.path); // /test/file.js

symlink

Path where the file points to in case it's a symbolic link. Will always be normalized and remove a trailing separator.

Normalization and concatenation

Since all properties are normalized in their setters, you can just concatenate with /, and normalization takes care of it properly on all platforms.

Example:

var file = new File();
file.path = '/' + 'test' + '/' + 'foo.bar';
console.log(file.path);
// posix => /test/foo.bar
// win32 => \\test\\foo.bar

But never concatenate with \, since that is a valid filename character on posix system.

Extending Vinyl

When extending Vinyl into your own class with extra features, you need to think about a few things.

When you have your own properties that are managed internally, you need to extend the static isCustomProp method to return false when one of these properties is queried.

const File = require('vinyl');

const builtInProps = ['foo', '_foo'];

class SuperFile extends File {
  constructor(options) {
    super(options);
    this._foo = 'example internal read-only value';
  }

  get foo() {
    return this._foo;
  }

  static isCustomProp(name) {
    return super.isCustomProp(name) && builtInProps.indexOf(name) === -1;
  }
}

This makes properties foo and _foo ignored when cloning, and when passed in options to constructor(options) so they don't get assigned to the new object.

Same goes for clone(). If you have your own internal stuff that needs special handling during cloning, you should extend it to do so.