Gathering detailed insights and metrics for vinyl
Gathering detailed insights and metrics for vinyl
Gathering detailed insights and metrics for vinyl
Gathering detailed insights and metrics for vinyl
npm install vinyl
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
1,280 Stars
133 Commits
105 Forks
25 Watching
3 Branches
40 Contributors
Updated on 22 Nov 2024
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
1.9%
1,149,148
Compared to previous day
Last week
3.3%
6,638,446
Compared to previous week
Last month
23.7%
26,320,553
Compared to previous month
Last year
2.7%
254,250,656
Compared to previous year
Virtual file format.
Vinyl is a very simple metadata object that describes a file. When you think of a file, two attributes come to mind: path
and contents
. 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.
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 a src(globs)
and a dest(folder)
method. Each return a stream. The src
stream produces Vinyl objects, and the dest
stream consumes Vinyl objects. Vinyl adapters can expose extra methods that might be specific to their input/output medium, such as the symlink
method vinyl-fs
provides.
1var Vinyl = require('vinyl');
2
3var jsFile = new Vinyl({
4 cwd: '/',
5 base: '/test/',
6 path: '/test/file.js',
7 contents: Buffer.from('var x = 123'),
8});
new Vinyl([options])
The constructor is used to create a new instance of Vinyl
. Each instance represents a separate file, directory or symlink.
All internally managed paths (cwd
, base
, path
, history
) are normalized and have trailing separators removed. See Normalization 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 the relative
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. If options.path
and options.history
are both passed, options.path
is appended to options.history
. All options.history
paths are normalized by the file.path
setter.
Type: Array
Default: []
(or [options.path]
if options.path
is passed)
options.stat
The result of an fs.stat
call. This is how you mark the file as a directory or symbolic link. See isDirectory(), isSymbolic() and fs.Stats for more information.
Type: fs.Stats
Default: undefined
options.contents
The contents of the file. If options.contents
is a ReadableStream
, it is wrapped in a cloneable-readable
stream.
Type: ReadableStream
, Buffer
, or null
Default: null
options.{custom}
Any other option properties will be directly assigned to the new Vinyl object.
1var Vinyl = require('vinyl'); 2 3var file = new Vinyl({ foo: 'bar' }); 4file.foo === 'bar'; // true
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()
Returns true
if the file contents are a Buffer
, otherwise false
.
file.isStream()
Returns true
if the file contents are a Stream
, otherwise false
.
file.isNull()
Returns true
if the file contents are null
, otherwise false
.
file.isDirectory()
Returns true
if the file represents a directory, otherwise false
.
A file is considered a directory when:
file.isNull()
is true
file.stat
is an objectfile.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.
file.isSymbolic()
Returns true
if the file represents a symbolic link, otherwise false
.
A file is considered symbolic when:
file.isNull()
is true
file.stat
is an objectfile.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.
file.clone([options])
Returns a new Vinyl object with all attributes cloned.
By default custom attributes are cloned deeply.
If options
or options.deep
is false
, custom attributes will not be cloned deeply.
If file.contents
is a Buffer
and options.contents
is false
, the Buffer
reference will be reused instead of copied.
file.inspect()
Returns a formatted-string interpretation of the Vinyl object. Automatically called by node's console.log
.
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 a ReadableStream
, it is wrapped in a cloneable-readable
stream.
Throws when set to any value other than a ReadableStream
, a Buffer
or null
.
Type: ReadableStream
, Buffer
, or null
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).
When null
or undefined
, it simply proxies the file.cwd
property. Will always be normalized and have trailing separators removed.
Throws when set to any value other than non-empty strings or null
/undefined
.
Type: String
file.path
Gets and sets the absolute pathname string or undefined
. Setting to a different value appends the new path to file.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 of file.path
values the Vinyl object has had, from file.history[0]
(original) through file.history[file.history.length - 1]
(current). file.history
and its elements should normally be treated as read-only and only altered indirectly by setting file.path
.
Type: Array
file.relative
Gets the result of path.relative(file.base, file.path)
.
Throws when set or when file.path
is not set.
Type: String
Example:
1var file = new File({
2 cwd: '/',
3 base: '/test/',
4 path: '/test/file.js',
5});
6
7console.log(file.relative); // file.js
file.dirname
Gets and sets the dirname of file.path
. Will always be normalized and have trailing separators removed.
Throws when file.path
is not set.
Type: String
Example:
1var file = new File({ 2 cwd: '/', 3 base: '/test/', 4 path: '/test/file.js', 5}); 6 7console.log(file.dirname); // /test 8 9file.dirname = '/specs'; 10 11console.log(file.dirname); // /specs 12console.log(file.path); // /specs/file.js
file.basename
Gets and sets the basename of file.path
.
Throws when file.path
is not set.
Type: String
Example:
1var file = new File({ 2 cwd: '/', 3 base: '/test/', 4 path: '/test/file.js', 5}); 6 7console.log(file.basename); // file.js 8 9file.basename = 'file.txt'; 10 11console.log(file.basename); // file.txt 12console.log(file.path); // /test/file.txt
file.stem
Gets and sets stem (filename without suffix) of file.path
.
Throws when file.path
is not set.
Type: String
Example:
1var file = new File({ 2 cwd: '/', 3 base: '/test/', 4 path: '/test/file.js', 5}); 6 7console.log(file.stem); // file 8 9file.stem = 'foo'; 10 11console.log(file.stem); // foo 12console.log(file.path); // /test/foo.js
file.extname
Gets and sets extname of file.path
.
Throws when file.path
is not set.
Type: String
Example:
1var file = new File({ 2 cwd: '/', 3 base: '/test/', 4 path: '/test/file.js', 5}); 6 7console.log(file.extname); // .js 8 9file.extname = '.txt'; 10 11console.log(file.extname); // .txt 12console.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 of instanceof
.
Takes an object and returns true
if it is a Vinyl file, otherwise returns false
.
Note: This method uses an internal flag that some older versions of Vinyl didn't expose.
Example:
1var Vinyl = require('vinyl'); 2 3var file = new Vinyl(); 4var notAFile = {}; 5 6Vinyl.isVinyl(file); // true 7Vinyl.isVinyl(notAFile); // false
Vinyl.isCustomProp(property)
Static method used by Vinyl when setting values inside the constructor or when copying properties in file.clone()
.
Takes a string property
and returns true
if the property is not used internally, otherwise returns false
.
This method is useful for inheritting from the Vinyl constructor. Read more in Extending Vinyl.
Example:
1var Vinyl = require('vinyl');
2
3Vinyl.isCustomProp('sourceMap'); // true
4Vinyl.isCustomProp('path'); // false -> internal getter/setter
Since all properties are normalized in their setters, you can just concatenate with /
, and normalization takes care of it properly on all platforms.
Example:
1var file = new File(); 2file.path = '/' + 'test' + '/' + 'foo.bar'; 3 4console.log(file.path); 5// posix => /test/foo.bar 6// win32 => \\test\\foo.bar
But never concatenate with \
, since that is a valid filename character on posix system.
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.
1var Vinyl = require('vinyl'); 2 3var builtInProps = ['foo', '_foo']; 4 5class SuperFile extends Vinyl { 6 constructor(options) { 7 super(options); 8 this._foo = 'example internal read-only value'; 9 } 10 11 get foo() { 12 return this._foo; 13 } 14 15 static isCustomProp(name) { 16 return super.isCustomProp(name) && builtInProps.indexOf(name) === -1; 17 } 18} 19 20// `foo` won't be assigned to the object below 21new SuperFile({ foo: 'something' });
This makes properties foo
and _foo
skipped when passed in options to constructor(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
and foo
properties will still exist on the created/cloned object because you are assigning _foo
in the constructor and foo
is defined on the prototype.
Same goes for clone()
. If you have your own internal stuff that needs special handling during cloning, you should extend it to do so.
MIT
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
security policy file detected
Details
Reason
Found 6/29 approved changesets -- score normalized to 2
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More