Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Virtual file format.

License

NotificationsYou must be signed in to change notification settings

gulpjs/vinyl

Repository files navigation

vinyl

NPM versionDownloadsBuild StatusCoveralls Status

Virtual file format.

What is Vinyl?

Vinyl is a very simple metadata object that describes a file. When you think of a file, two attributes come to mind:path andcontents. These are the main attributes on a Vinyl object. A file does not necessarily represent something on your computer’s file system. You have files on S3, FTP, Dropbox, Box, CloudThingly.io and other services. Vinyl can be used to describe files from all of these sources.

What is a Vinyl Adapter?

While Vinyl provides a clean way to describe a file, we also need a way to access these files. Each file source needs what I call a "Vinyl adapter". A Vinyl adapter simply exposes asrc(globs) and adest(folder) method. Each return a stream. Thesrc stream produces Vinyl objects, and thedest stream consumes Vinyl objects. Vinyl adapters can expose extra methods that might be specific to their input/output medium, such as thesymlink methodvinyl-fs provides.

Usage

varVinyl=require('vinyl');varjsFile=newVinyl({cwd:'/',base:'/test/',path:'/test/file.js',contents:Buffer.from('var x = 123'),});

API

new Vinyl([options])

The constructor is used to create a new instance ofVinyl. Each instance represents a separate file, directory or symlink.

All internally managed paths (cwd,base,path,history) are normalized and have trailing separators removed. SeeNormalization and concatenation for more information.

Options may be passed upon instantiation to create a file with specific properties.

options

Options are not mutated by the constructor.

options.cwd

The current working directory of the file.

Type:String

Default:process.cwd()

options.base

Used for calculating therelative property. This is typically where a glob starts.

Type:String

Default:options.cwd

options.path

The full path to the file.

Type:String

Default:undefined

options.history

Stores the path history. Ifoptions.path andoptions.history are both passed,options.path is appended tooptions.history. Alloptions.history paths are normalized by thefile.path setter.

Type:Array

Default:[] (or[options.path] ifoptions.path is passed)

options.stat

The result of anfs.stat call. This is how you mark the file as a directory or symbolic link. SeeisDirectory(),isSymbolic() andfs.Stats for more information.

Type:fs.Stats

Default:undefined

options.contents

The contents of the file. Ifoptions.contents is aReadableStream, it is wrapped in acloneable-readable stream.

Type:ReadableStream,Buffer, ornull

Default:null

options.{custom}

Any other option properties will be directly assigned to the new Vinyl object.

varVinyl=require('vinyl');varfile=newVinyl({foo:'bar'});file.foo==='bar';// true

Instance methods

Each Vinyl object will have instance methods. Every method will be available but may return differently based on what properties were set upon instantiation or modified since.

file.isBuffer()

Returnstrue if the file contents are aBuffer, otherwisefalse.

file.isStream()

Returnstrue if the file contents are aStream, otherwisefalse.

file.isNull()

Returnstrue if the file contents arenull, otherwisefalse.

file.isDirectory()

Returnstrue if the file represents a directory, otherwisefalse.

A file is considered a directory when:

  • file.isNull() istrue
  • file.stat is an object
  • file.stat.isDirectory() returnstrue

When constructing a Vinyl object, pass in a validfs.Stats object viaoptions.stat. If you are mocking thefs.Stats object, you may need to stub theisDirectory() method.

file.isSymbolic()

Returnstrue if the file represents a symbolic link, otherwisefalse.

A file is considered symbolic when:

  • file.isNull() istrue
  • file.stat is an object
  • file.stat.isSymbolicLink() returnstrue

When constructing a Vinyl object, pass in a validfs.Stats object viaoptions.stat. If you are mocking thefs.Stats object, you may need to stub theisSymbolicLink() method.

file.clone([options])

Returns a new Vinyl object with all attributes cloned.

By default custom attributes are cloned deeply.

Ifoptions oroptions.deep isfalse, custom attributes will not be cloned deeply.

Iffile.contents is aBuffer andoptions.contents isfalse, theBuffer reference will be reused instead of copied.

file.inspect()

Returns a formatted-string interpretation of the Vinyl object. Automatically called by node'sconsole.log.

Instance properties

Each Vinyl object will have instance properties. Some may be unavailable based on what properties were set upon instantiation or modified since.

file.contents

Gets and sets the contents of the file. If set to aReadableStream, it is wrapped in acloneable-readable stream.

Throws when set to any value other than aReadableStream, aBuffer ornull.

Type:ReadableStream,Buffer, ornull

file.cwd

Gets and sets current working directory. Will always be normalized and have trailing separators removed.

Throws when set to any value other than non-empty strings.

Type:String

file.base

Gets and sets base directory. Used for relative pathing (typically where a glob starts).Whennull orundefined, it simply proxies thefile.cwd property. Will always be normalized and have trailing separators removed.

Throws when set to any value other than non-empty strings ornull/undefined.

Type:String

file.path

Gets and sets the absolute pathname string orundefined. Setting to a different value appends the new path tofile.history. If set to the same value as the current path, it is ignored. All new values are normalized and have trailing separators removed.

Throws when set to any value other than a string.

Type:String

file.history

Array offile.path values the Vinyl object has had, fromfile.history[0] (original) throughfile.history[file.history.length - 1] (current).file.history and its elements should normally be treated as read-only and only altered indirectly by settingfile.path.

Type:Array

file.relative

Gets the result ofpath.relative(file.base, file.path).

Throws when set or whenfile.path is not set.

Type:String

Example:

varfile=newFile({cwd:'/',base:'/test/',path:'/test/file.js',});console.log(file.relative);// file.js

file.dirname

Gets and sets the dirname offile.path. Will always be normalized and have trailing separators removed.

Throws whenfile.path is not set.

Type:String

Example:

varfile=newFile({cwd:'/',base:'/test/',path:'/test/file.js',});console.log(file.dirname);// /testfile.dirname='/specs';console.log(file.dirname);// /specsconsole.log(file.path);// /specs/file.js

file.basename

Gets and sets the basename offile.path.

Throws whenfile.path is not set.

Type:String

Example:

varfile=newFile({cwd:'/',base:'/test/',path:'/test/file.js',});console.log(file.basename);// file.jsfile.basename='file.txt';console.log(file.basename);// file.txtconsole.log(file.path);// /test/file.txt

file.stem

Gets and sets stem (filename without suffix) offile.path.

Throws whenfile.path is not set.

Type:String

Example:

varfile=newFile({cwd:'/',base:'/test/',path:'/test/file.js',});console.log(file.stem);// filefile.stem='foo';console.log(file.stem);// fooconsole.log(file.path);// /test/foo.js

file.extname

Gets and sets extname offile.path.

Throws whenfile.path is not set.

Type:String

Example:

varfile=newFile({cwd:'/',base:'/test/',path:'/test/file.js',});console.log(file.extname);// .jsfile.extname='.txt';console.log(file.extname);// .txtconsole.log(file.path);// /test/file.txt

file.symlink

Gets and sets the path where the file points to if it's a symbolic link. Will always be normalized and have trailing separators removed.

Throws when set to any value other than a string.

Type:String

Vinyl.isVinyl(file)

Static method used for checking if an object is a Vinyl file. Use this method instead ofinstanceof.

Takes an object and returnstrue if it is a Vinyl file, otherwise returnsfalse.

Note: This method uses an internal flag that some older versions of Vinyl didn't expose.

Example:

varVinyl=require('vinyl');varfile=newVinyl();varnotAFile={};Vinyl.isVinyl(file);// trueVinyl.isVinyl(notAFile);// false

Vinyl.isCustomProp(property)

Static method used by Vinyl when setting values inside the constructor or when copying properties infile.clone().

Takes a stringproperty and returnstrue if the property is not used internally, otherwise returnsfalse.

This method is useful for inheritting from the Vinyl constructor. Read more inExtending Vinyl.

Example:

varVinyl=require('vinyl');Vinyl.isCustomProp('sourceMap');// trueVinyl.isCustomProp('path');// false -> internal getter/setter

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:

varfile=newFile();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 staticisCustomProp method to returnfalse when one of these properties is queried.

varVinyl=require('vinyl');varbuiltInProps=['foo','_foo'];classSuperFileextendsVinyl{constructor(options){super(options);this._foo='example internal read-only value';}getfoo(){returnthis._foo;}staticisCustomProp(name){returnsuper.isCustomProp(name)&&builtInProps.indexOf(name)===-1;}}// `foo` won't be assigned to the object belownewSuperFile({foo:'something'});

This makes propertiesfoo and_foo skipped when passed in options toconstructor(options) so they don't get assigned to the new object and override your custom implementation. They also won't be copied when cloning.Note: The_foo andfoo properties will still exist on the created/cloned object because you are assigning_foo in the constructor andfoo is defined on the prototype.

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

License

MIT


[8]ページ先頭

©2009-2025 Movatter.jp