1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
|
<p align="center">
<a href="http://gulpjs.com">
<img height="257" width="114" src="https://raw.githubusercontent.com/gulpjs/artwork/master/gulp-2x.png">
</a>
</p>
# vinyl
[![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [![Build Status][travis-image]][travis-url] [![AppVeyor Build Status][appveyor-image]][appveyor-url] [![Coveralls Status][coveralls-image]][coveralls-url] [![Gitter chat][gitter-image]][gitter-url]
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` 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.
## 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 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`][vinyl-fs] provides.
## Usage
```js
var Vinyl = require('vinyl');
var jsFile = new Vinyl({
cwd: '/',
base: '/test/',
path: '/test/file.js',
contents: new Buffer('var x = 123')
});
```
## API
### `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][normalization] 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()][is-directory], [isSymbolic()][is-symbolic] and [fs.Stats][fs-stats] for more information.
Type: [`fs.Stats`][fs-stats]
Default: `undefined`
##### `options.contents`
The contents of the file. If `options.contents` is a [`Stream`][stream], it is wrapped in a [`cloneable-readable`][cloneable-readable] stream.
Type: [`Stream`][stream], [`Buffer`][buffer], or `null`
Default: `null`
##### `options.{custom}`
Any other option properties will be directly assigned to the new Vinyl object.
```js
var Vinyl = require('vinyl');
var file = new Vinyl({ 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()`
Returns `true` if the file contents are a [`Buffer`][buffer], otherwise `false`.
#### `file.isStream()`
Returns `true` if the file contents are a [`Stream`][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 object
- `file.stat.isDirectory()` returns `true`
When constructing a Vinyl object, pass in a valid [`fs.Stats`][fs-stats] object via `options.stat`. If you are mocking the [`fs.Stats`][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 object
- `file.stat.isSymbolicLink()` returns `true`
When constructing a Vinyl object, pass in a valid [`fs.Stats`][fs-stats] object via `options.stat`. If you are mocking the [`fs.Stats`][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`][buffer] and `options.contents` is `false`, the [`Buffer`][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`.
### 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 a [`Stream`][stream], it is wrapped in a [`cloneable-readable`][cloneable-readable] stream.
Throws when set to any value other than a [`Stream`][stream], a [`Buffer`][buffer] or `null`.
Type: [`Stream`][stream], [`Buffer`][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:
```js
var file = new File({
cwd: '/',
base: '/test/',
path: '/test/file.js'
});
console.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:
```js
var file = new File({
cwd: '/',
base: '/test/',
path: '/test/file.js'
});
console.log(file.dirname); // /test
file.dirname = '/specs';
console.log(file.dirname); // /specs
console.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:
```js
var file = new File({
cwd: '/',
base: '/test/',
path: '/test/file.js'
});
console.log(file.basename); // file.js
file.basename = 'file.txt';
console.log(file.basename); // file.txt
console.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:
```js
var file = new File({
cwd: '/',
base: '/test/',
path: '/test/file.js'
});
console.log(file.stem); // file
file.stem = 'foo';
console.log(file.stem); // foo
console.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:
```js
var file = new File({
cwd: '/',
base: '/test/',
path: '/test/file.js'
});
console.log(file.extname); // .js
file.extname = '.txt';
console.log(file.extname); // .txt
console.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:
```js
var Vinyl = require('vinyl');
var file = new Vinyl();
var notAFile = {};
Vinyl.isVinyl(file); // true
Vinyl.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 usefuly for inheritting from the Vinyl constructor. Read more in [Extending Vinyl][extending-vinyl].
Example:
```js
var Vinyl = require('vinyl');
Vinyl.isCustomProp('sourceMap'); // true
Vinyl.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:
```js
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.
```js
var Vinyl = require('vinyl');
var builtInProps = ['foo', '_foo'];
class SuperFile extends Vinyl {
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.
## License
MIT
[is-symbolic]: #issymbolic
[is-directory]: #isdirectory
[normalization]: #normalization-and-concatenation
[extending-vinyl]: #extending-vinyl
[stream]: https://nodejs.org/api/stream.html#stream_stream
[buffer]: https://nodejs.org/api/buffer.html#buffer_class_buffer
[fs-stats]: http://nodejs.org/api/fs.html#fs_class_fs_stats
[vinyl-fs]: https://github.com/gulpjs/vinyl-fs
[cloneable-readable]: https://github.com/mcollina/cloneable-readable
[downloads-image]: http://img.shields.io/npm/dm/vinyl.svg
[npm-url]: https://www.npmjs.com/package/vinyl
[npm-image]: http://img.shields.io/npm/v/vinyl.svg
[travis-url]: https://travis-ci.org/gulpjs/vinyl
[travis-image]: http://img.shields.io/travis/gulpjs/vinyl.svg?label=travis-ci
[appveyor-url]: https://ci.appveyor.com/project/gulpjs/vinyl
[appveyor-image]: https://img.shields.io/appveyor/ci/gulpjs/vinyl.svg?label=appveyor
[coveralls-url]: https://coveralls.io/r/gulpjs/vinyl
[coveralls-image]: http://img.shields.io/coveralls/gulpjs/vinyl/master.svg
[gitter-url]: https://gitter.im/gulpjs/gulp
[gitter-image]: https://badges.gitter.im/gulpjs/gulp.svg
|