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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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-fs
  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. [Vinyl][vinyl] adapter for the file system.
  9. ## What is Vinyl?
  10. [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][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][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 now need a way to access these files. Each file source needs what we 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.
  13. ## Usage
  14. ```javascript
  15. var map = require('map-stream');
  16. var vfs = require('vinyl-fs');
  17. var log = function(file, cb) {
  18. console.log(file.path);
  19. cb(null, file);
  20. };
  21. vfs.src(['./js/**/*.js', '!./js/vendor/*.js'])
  22. .pipe(map(log))
  23. .pipe(vfs.dest('./output'));
  24. ```
  25. ## API
  26. ### `src(globs[, options])`
  27. Takes a glob string or an array of glob strings as the first argument and an options object as the second.
  28. Returns a stream of [vinyl] `File` objects.
  29. __Note: UTF-8 BOM will be removed from all UTF-8 files read with `.src` unless disabled in the options.__
  30. #### Globs
  31. Globs are executed in order, so negations should follow positive globs.
  32. For example:
  33. ```js
  34. fs.src(['!b*', '*'])
  35. ```
  36. would not exclude any files, but the following would exclude all files starting with "b":
  37. ```js
  38. fs.src(['*', '!b*'])
  39. ```
  40. #### Options
  41. - Values passed to the options must be of the expected type, otherwise they will be ignored.
  42. - All options can be passed a function instead of a value. The function will be called with the [vinyl] `File` object as its only argument and must return a value of the expected type for that option.
  43. ##### `options.buffer`
  44. Whether or not you want to buffer the file contents into memory. Setting to `false` will make `file.contents` a paused Stream.
  45. Type: `Boolean`
  46. Default: `true`
  47. ##### `options.read`
  48. Whether or not you want the file to be read at all. Useful for stuff like removing files. Setting to `false` will make `file.contents = null` and will disable writing the file to disk via `.dest()`.
  49. Type: `Boolean`
  50. Default: `true`
  51. ##### `options.since`
  52. Only streams files that have been modified since the time specified.
  53. Type: `Date` or `Number`
  54. Default: `undefined`
  55. ##### `options.removeBOM`
  56. Causes the BOM to be removed on UTF-8 encoded files. Set to `false` if you need the BOM for some reason.
  57. Type: `Boolean`
  58. Default: `true`
  59. ##### `options.sourcemaps`
  60. Enables sourcemap support on files passed through the stream. Will load inline sourcemaps and resolve sourcemap links from files.
  61. Type: `Boolean`
  62. Default: `false`
  63. ##### `options.resolveSymlinks`
  64. Whether or not to recursively resolve symlinks to their targets. Set to `false` to preserve them as symlinks and make `file.symlink` equal the original symlink's target path.
  65. Type: `Boolean`
  66. Default: `true`
  67. ##### `options.dot`
  68. Whether or not you want globs to match on dot files (e.g. `.gitignore`).
  69. __Note: This option is not resolved from a function because it is passed verbatim to node-glob.__
  70. Type: `Boolean`
  71. Default: `false`
  72. ##### other
  73. Any glob-related options are documented in [glob-stream] and [node-glob] and are forwarded verbatim.
  74. ### `dest(folder[, options])`
  75. Takes a folder path string or a function as the first argument and an options object as the second. If given a function, it will be called with each [vinyl] `File` object and must return a folder path.
  76. Returns a stream that accepts [vinyl] `File` objects, writes them to disk at the folder/cwd specified, and passes them downstream so you can keep piping these around.
  77. Once the file is written to disk, an attempt is made to determine if the `stat.mode`, `stat.mtime` and `stat.atime` of the [vinyl] `File` object differ from the file on the filesystem.
  78. If they differ and the running process owns the file, the corresponding filesystem metadata is updated.
  79. If they don't differ or the process doesn't own the file, the attempt is skipped silently.
  80. __This functionality is disabled on Windows operating systems or any other OS that doesn't support `process.getuid` or `process.geteuid` in node. This is due to Windows having very unexpected results through usage of `fs.fchmod` and `fs.futimes`.__
  81. __Note: The `fs.futimes()` method internally converts `stat.mtime` and `stat.atime` timestamps to seconds; this division by `1000` may cause some loss of precision in 32-bit Node.js.__
  82. If the file has a `symlink` attribute specifying a target path, then a symlink will be created.
  83. __Note: The file will be modified after being written to this stream.__
  84. - `cwd`, `base`, and `path` will be overwritten to match the folder.
  85. - `stat` will be updated to match the file on the filesystem.
  86. - `contents` will have it's position reset to the beginning if it is a stream.
  87. #### Options
  88. - Values passed to the options must be of the expected type, otherwise they will be ignored.
  89. - All options can be passed a function instead of a value. The function will be called with the [vinyl] `File` object as its only argument and must return a value of the expected type for that option.
  90. ##### `options.cwd`
  91. The working directory the folder is relative to.
  92. Type: `String`
  93. Default: `process.cwd()`
  94. ##### `options.mode`
  95. The mode the files should be created with. This option is only resolved if the [vinyl] `File` is not symbolic.
  96. Type: `Number`
  97. Default: The `mode` of the input file (`file.stat.mode`) if any, or the process mode if the input file has no `mode` property.
  98. ##### `options.dirMode`
  99. The mode directories should be created with.
  100. Type: `Number`
  101. Default: The process `mode`.
  102. ##### `options.overwrite`
  103. Whether or not existing files with the same path should be overwritten.
  104. Type: `Boolean`
  105. Default: `true` (always overwrite existing files)
  106. ##### `options.append`
  107. Whether or not new data should be appended after existing file contents (if any).
  108. Type: `Boolean`
  109. Default: `false` (always replace existing contents, if any)
  110. ##### `options.sourcemaps`
  111. Enables sourcemap support on files passed through the stream. Will write inline soucemaps if specified as `true`.
  112. Specifying a `String` path will write external sourcemaps at the given path.
  113. Examples:
  114. ```js
  115. // Write as inline comments
  116. vfs.dest('./', { sourcemaps: true });
  117. // Write as files in the same folder
  118. vfs.dest('./', { sourcemaps: '.' });
  119. ```
  120. Type: `Boolean` or `String`
  121. Default: `undefined` (do not write sourcemaps)
  122. ##### `options.relativeSymlinks`
  123. When creating a symlink, whether or not the created symlink should be relative. If `false`, the symlink will be absolute.
  124. __Note: This option will be ignored if a `junction` is being created, as they must be absolute.__
  125. Type: `Boolean`
  126. Default: `false`
  127. ##### `options.useJunctions`
  128. When creating a symlink, whether or not a directory symlink should be created as a `junction`.
  129. This option is only relevant on Windows and ignored elsewhere. Please refer to the [Symbolic Links on Windows][symbolic-caveats] section below.
  130. Type: `Boolean`
  131. Default: `true`
  132. ### `symlink(folder[, options])`
  133. Takes a folder path string or a function as the first argument and an options object as the second. If given a function, it will be called with each [vinyl] `File` object and must return a folder path.
  134. Returns a stream that accepts [vinyl] `File` objects, creates a symbolic link (i.e. symlink) at the folder/cwd specified, and passes them downstream so you can keep piping these around.
  135. __Note: The file will be modified after being written to this stream.__
  136. - `cwd`, `base`, and `path` will be overwritten to match the folder.
  137. - `stat` will be updated to match the symlink on the filesystem.
  138. - `contents` will be set to `null`.
  139. - `symlink` will be added or replaced to be the original path.
  140. __Note: On Windows, directory links are created using Junctions by default. Use the `useJunctions` option to disable this behavior.__
  141. #### Options
  142. - Values passed to the options must be of the expected type, otherwise they will be ignored.
  143. - All options can be passed a function instead of a value. The function will be called with the [vinyl] `File` object as its only argument and must return a value of the expected type for that option.
  144. ##### `options.cwd`
  145. The working directory the folder is relative to.
  146. Type: `String`
  147. Default: `process.cwd()`
  148. ##### `options.dirMode`
  149. The mode directories should be created with.
  150. Type: `Number`
  151. Default: The process mode.
  152. ##### `options.overwrite`
  153. Whether or not existing files with the same path should be overwritten.
  154. Type: `Boolean`
  155. Default: `true` (always overwrite existing files)
  156. ##### `options.relativeSymlinks`
  157. Whether or not the created symlinks should be relative. If `false`, the symlink will be absolute.
  158. __Note: This option will be ignored if a `junction` is being created, as they must be absolute.__
  159. Type: `Boolean`
  160. Default: `false`
  161. ##### `options.useJunctions`
  162. When creating a symlink, whether or not a directory symlink should be created as a `junction`.
  163. This option is only relevant on Windows and ignored elsewhere. Please refer to the [Symbolic Links on Windows][symbolic-caveats] section below.
  164. Type: `Boolean`
  165. Default: `true`
  166. #### Symbolic Links on Windows
  167. When creating symbolic links on Windows, we pass a `type` argument to Node's
  168. `fs` module which specifies the kind of target we link to (one of `'file'`,
  169. `'dir'` or `'junction'`). Specifically, this will be `'file'` when the target
  170. is a regular file, `'junction'` if the target is a directory, or `'dir'` if
  171. the target is a directory and the user overrides the `useJunctions` option
  172. default.
  173. However, if the user tries to make a "dangling" link (pointing to a non-existent
  174. target) we won't be able to determine automatically which type we should use.
  175. In these cases, `vinyl-fs` will behave slightly differently depending on
  176. whether the dangling link is being created via `symlink()` or via `dest()`.
  177. For dangling links created via `symlink()`, the incoming vinyl represents the
  178. target and so we will look to its stats to guess the desired type. In
  179. particular, if `isDirectory()` returns false then we'll create a `'file'` type
  180. link, otherwise we will create a `'junction'` or a `'dir'` type link depending
  181. on the value of the `useJunctions` option.
  182. For dangling links created via `dest()`, the incoming vinyl represents the link -
  183. typically read off disk via `src()` with the `resolveSymlinks` option set to
  184. false. In this case, we won't be able to make any reasonable guess as to the
  185. type of link and we default to using `'file'`, which may cause unexpected behavior
  186. if you are creating a "dangling" link to a directory. It is advised to avoid this
  187. scenario.
  188. [symbolic-caveats]: #symbolic-links-on-windows
  189. [glob-stream]: https://github.com/gulpjs/glob-stream
  190. [node-glob]: https://github.com/isaacs/node-glob
  191. [gaze]: https://github.com/shama/gaze
  192. [glob-watcher]: https://github.com/wearefractal/glob-watcher
  193. [vinyl]: https://github.com/wearefractal/vinyl
  194. [downloads-image]: http://img.shields.io/npm/dm/vinyl-fs.svg
  195. [npm-url]: https://www.npmjs.com/package/vinyl-fs
  196. [npm-image]: http://img.shields.io/npm/v/vinyl-fs.svg
  197. [travis-url]: https://travis-ci.org/gulpjs/vinyl-fs
  198. [travis-image]: http://img.shields.io/travis/gulpjs/vinyl-fs.svg?label=travis-ci
  199. [appveyor-url]: https://ci.appveyor.com/project/gulpjs/vinyl-fs
  200. [appveyor-image]: https://img.shields.io/appveyor/ci/gulpjs/vinyl-fs.svg?label=appveyor
  201. [coveralls-url]: https://coveralls.io/r/gulpjs/vinyl-fs
  202. [coveralls-image]: http://img.shields.io/coveralls/gulpjs/vinyl-fs/master.svg
  203. [gitter-url]: https://gitter.im/gulpjs/gulp
  204. [gitter-image]: https://badges.gitter.im/gulpjs/gulp.svg