You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. <p align="center">
  2. <a href="http://gulpjs.com">
  3. <img height="257" width="114" src="https://raw.githubusercontent.com/gulpjs/artwork/master/gulp-2x.png">
  4. </a>
  5. </p>
  6. # vinyl
  7. [![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]
  8. Virtual file format.
  9. ## What is Vinyl?
  10. 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.
  11. ## What is a Vinyl Adapter?
  12. 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.
  13. ## Usage
  14. ```js
  15. var Vinyl = require('vinyl');
  16. var jsFile = new Vinyl({
  17. cwd: '/',
  18. base: '/test/',
  19. path: '/test/file.js',
  20. contents: new Buffer('var x = 123')
  21. });
  22. ```
  23. ## API
  24. ### `new Vinyl([options])`
  25. The constructor is used to create a new instance of `Vinyl`. Each instance represents a separate file, directory or symlink.
  26. All internally managed paths (`cwd`, `base`, `path`, `history`) are normalized and have trailing separators removed. See [Normalization and concatenation][normalization] for more information.
  27. Options may be passed upon instantiation to create a file with specific properties.
  28. #### `options`
  29. Options are not mutated by the constructor.
  30. ##### `options.cwd`
  31. The current working directory of the file.
  32. Type: `String`
  33. Default: `process.cwd()`
  34. ##### `options.base`
  35. Used for calculating the `relative` property. This is typically where a glob starts.
  36. Type: `String`
  37. Default: `options.cwd`
  38. ##### `options.path`
  39. The full path to the file.
  40. Type: `String`
  41. Default: `undefined`
  42. ##### `options.history`
  43. 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.
  44. Type: `Array`
  45. Default: `[]` (or `[options.path]` if `options.path` is passed)
  46. ##### `options.stat`
  47. 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.
  48. Type: [`fs.Stats`][fs-stats]
  49. Default: `undefined`
  50. ##### `options.contents`
  51. The contents of the file. If `options.contents` is a [`ReadableStream`][readable-stream], it is wrapped in a [`cloneable-readable`][cloneable-readable] stream.
  52. Type: [`ReadableStream`][readable-stream], [`Buffer`][buffer], or `null`
  53. Default: `null`
  54. ##### `options.{custom}`
  55. Any other option properties will be directly assigned to the new Vinyl object.
  56. ```js
  57. var Vinyl = require('vinyl');
  58. var file = new Vinyl({ foo: 'bar' });
  59. file.foo === 'bar'; // true
  60. ```
  61. ### Instance methods
  62. 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.
  63. #### `file.isBuffer()`
  64. Returns `true` if the file contents are a [`Buffer`][buffer], otherwise `false`.
  65. #### `file.isStream()`
  66. Returns `true` if the file contents are a [`Stream`][stream], otherwise `false`.
  67. #### `file.isNull()`
  68. Returns `true` if the file contents are `null`, otherwise `false`.
  69. #### `file.isDirectory()`
  70. Returns `true` if the file represents a directory, otherwise `false`.
  71. A file is considered a directory when:
  72. - `file.isNull()` is `true`
  73. - `file.stat` is an object
  74. - `file.stat.isDirectory()` returns `true`
  75. 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.
  76. #### `file.isSymbolic()`
  77. Returns `true` if the file represents a symbolic link, otherwise `false`.
  78. A file is considered symbolic when:
  79. - `file.isNull()` is `true`
  80. - `file.stat` is an object
  81. - `file.stat.isSymbolicLink()` returns `true`
  82. 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.
  83. #### `file.clone([options])`
  84. Returns a new Vinyl object with all attributes cloned.
  85. __By default custom attributes are cloned deeply.__
  86. If `options` or `options.deep` is `false`, custom attributes will not be cloned deeply.
  87. If `file.contents` is a [`Buffer`][buffer] and `options.contents` is `false`, the [`Buffer`][buffer] reference will be reused instead of copied.
  88. #### `file.inspect()`
  89. Returns a formatted-string interpretation of the Vinyl object. Automatically called by node's `console.log`.
  90. ### Instance properties
  91. Each Vinyl object will have instance properties. Some may be unavailable based on what properties were set upon instantiation or modified since.
  92. #### `file.contents`
  93. Gets and sets the contents of the file. If set to a [`ReadableStream`][readable-stream], it is wrapped in a [`cloneable-readable`][cloneable-readable] stream.
  94. Throws when set to any value other than a [`ReadableStream`][readable-stream], a [`Buffer`][buffer] or `null`.
  95. Type: [`ReadableStream`][readable-stream], [`Buffer`][buffer], or `null`
  96. #### `file.cwd`
  97. Gets and sets current working directory. Will always be normalized and have trailing separators removed.
  98. Throws when set to any value other than non-empty strings.
  99. Type: `String`
  100. #### `file.base`
  101. Gets and sets base directory. Used for relative pathing (typically where a glob starts).
  102. When `null` or `undefined`, it simply proxies the `file.cwd` property. Will always be normalized and have trailing separators removed.
  103. Throws when set to any value other than non-empty strings or `null`/`undefined`.
  104. Type: `String`
  105. #### `file.path`
  106. 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.
  107. Throws when set to any value other than a string.
  108. Type: `String`
  109. #### `file.history`
  110. 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`.
  111. Type: `Array`
  112. #### `file.relative`
  113. Gets the result of `path.relative(file.base, file.path)`.
  114. Throws when set or when `file.path` is not set.
  115. Type: `String`
  116. Example:
  117. ```js
  118. var file = new File({
  119. cwd: '/',
  120. base: '/test/',
  121. path: '/test/file.js'
  122. });
  123. console.log(file.relative); // file.js
  124. ```
  125. #### `file.dirname`
  126. Gets and sets the dirname of `file.path`. Will always be normalized and have trailing separators removed.
  127. Throws when `file.path` is not set.
  128. Type: `String`
  129. Example:
  130. ```js
  131. var file = new File({
  132. cwd: '/',
  133. base: '/test/',
  134. path: '/test/file.js'
  135. });
  136. console.log(file.dirname); // /test
  137. file.dirname = '/specs';
  138. console.log(file.dirname); // /specs
  139. console.log(file.path); // /specs/file.js
  140. ```
  141. #### `file.basename`
  142. Gets and sets the basename of `file.path`.
  143. Throws when `file.path` is not set.
  144. Type: `String`
  145. Example:
  146. ```js
  147. var file = new File({
  148. cwd: '/',
  149. base: '/test/',
  150. path: '/test/file.js'
  151. });
  152. console.log(file.basename); // file.js
  153. file.basename = 'file.txt';
  154. console.log(file.basename); // file.txt
  155. console.log(file.path); // /test/file.txt
  156. ```
  157. #### `file.stem`
  158. Gets and sets stem (filename without suffix) of `file.path`.
  159. Throws when `file.path` is not set.
  160. Type: `String`
  161. Example:
  162. ```js
  163. var file = new File({
  164. cwd: '/',
  165. base: '/test/',
  166. path: '/test/file.js'
  167. });
  168. console.log(file.stem); // file
  169. file.stem = 'foo';
  170. console.log(file.stem); // foo
  171. console.log(file.path); // /test/foo.js
  172. ```
  173. #### `file.extname`
  174. Gets and sets extname of `file.path`.
  175. Throws when `file.path` is not set.
  176. Type: `String`
  177. Example:
  178. ```js
  179. var file = new File({
  180. cwd: '/',
  181. base: '/test/',
  182. path: '/test/file.js'
  183. });
  184. console.log(file.extname); // .js
  185. file.extname = '.txt';
  186. console.log(file.extname); // .txt
  187. console.log(file.path); // /test/file.txt
  188. ```
  189. #### `file.symlink`
  190. 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.
  191. Throws when set to any value other than a string.
  192. Type: `String`
  193. ### `Vinyl.isVinyl(file)`
  194. Static method used for checking if an object is a Vinyl file. Use this method instead of `instanceof`.
  195. Takes an object and returns `true` if it is a Vinyl file, otherwise returns `false`.
  196. __Note: This method uses an internal flag that some older versions of Vinyl didn't expose.__
  197. Example:
  198. ```js
  199. var Vinyl = require('vinyl');
  200. var file = new Vinyl();
  201. var notAFile = {};
  202. Vinyl.isVinyl(file); // true
  203. Vinyl.isVinyl(notAFile); // false
  204. ```
  205. ### `Vinyl.isCustomProp(property)`
  206. Static method used by Vinyl when setting values inside the constructor or when copying properties in `file.clone()`.
  207. Takes a string `property` and returns `true` if the property is not used internally, otherwise returns `false`.
  208. This method is useful for inheritting from the Vinyl constructor. Read more in [Extending Vinyl][extending-vinyl].
  209. Example:
  210. ```js
  211. var Vinyl = require('vinyl');
  212. Vinyl.isCustomProp('sourceMap'); // true
  213. Vinyl.isCustomProp('path'); // false -> internal getter/setter
  214. ```
  215. ## Normalization and concatenation
  216. Since all properties are normalized in their setters, you can just concatenate with `/`, and normalization takes care of it properly on all platforms.
  217. Example:
  218. ```js
  219. var file = new File();
  220. file.path = '/' + 'test' + '/' + 'foo.bar';
  221. console.log(file.path);
  222. // posix => /test/foo.bar
  223. // win32 => \\test\\foo.bar
  224. ```
  225. But never concatenate with `\`, since that is a valid filename character on posix system.
  226. ## Extending Vinyl
  227. When extending Vinyl into your own class with extra features, you need to think about a few things.
  228. 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.
  229. ```js
  230. var Vinyl = require('vinyl');
  231. var builtInProps = ['foo', '_foo'];
  232. class SuperFile extends Vinyl {
  233. constructor(options) {
  234. super(options);
  235. this._foo = 'example internal read-only value';
  236. }
  237. get foo() {
  238. return this._foo;
  239. }
  240. static isCustomProp(name) {
  241. return super.isCustomProp(name) && builtInProps.indexOf(name) === -1;
  242. }
  243. }
  244. ```
  245. 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.
  246. Same goes for `clone()`. If you have your own internal stuff that needs special handling during cloning, you should extend it to do so.
  247. ## License
  248. MIT
  249. [is-symbolic]: #issymbolic
  250. [is-directory]: #isdirectory
  251. [normalization]: #normalization-and-concatenation
  252. [extending-vinyl]: #extending-vinyl
  253. [stream]: https://nodejs.org/api/stream.html#stream_stream
  254. [readable-stream]: https://nodejs.org/api/stream.html#stream_readable_streams
  255. [buffer]: https://nodejs.org/api/buffer.html#buffer_class_buffer
  256. [fs-stats]: http://nodejs.org/api/fs.html#fs_class_fs_stats
  257. [vinyl-fs]: https://github.com/gulpjs/vinyl-fs
  258. [cloneable-readable]: https://github.com/mcollina/cloneable-readable
  259. [downloads-image]: http://img.shields.io/npm/dm/vinyl.svg
  260. [npm-url]: https://www.npmjs.com/package/vinyl
  261. [npm-image]: http://img.shields.io/npm/v/vinyl.svg
  262. [travis-url]: https://travis-ci.org/gulpjs/vinyl
  263. [travis-image]: http://img.shields.io/travis/gulpjs/vinyl.svg?label=travis-ci
  264. [appveyor-url]: https://ci.appveyor.com/project/gulpjs/vinyl
  265. [appveyor-image]: https://img.shields.io/appveyor/ci/gulpjs/vinyl.svg?label=appveyor
  266. [coveralls-url]: https://coveralls.io/r/gulpjs/vinyl
  267. [coveralls-image]: http://img.shields.io/coveralls/gulpjs/vinyl/master.svg
  268. [gitter-url]: https://gitter.im/gulpjs/gulp
  269. [gitter-image]: https://badges.gitter.im/gulpjs/gulp.svg