Initial commit
[yaffs-website] / node_modules / gulp / node_modules / vinyl-fs / lib / src / index.js
1 'use strict';
2
3 var defaults = require('defaults');
4 var through = require('through2');
5 var gs = require('glob-stream');
6 var File = require('vinyl');
7
8 var getContents = require('./getContents');
9 var getStats = require('./getStats');
10
11 function createFile (globFile, enc, cb) {
12   cb(null, new File(globFile));
13 }
14
15 function src(glob, opt) {
16   opt = opt || {};
17   var pass = through.obj();
18
19   if (!isValidGlob(glob)) {
20     throw new Error('Invalid glob argument: ' + glob);
21   }
22   // return dead stream if empty array
23   if (Array.isArray(glob) && glob.length === 0) {
24     process.nextTick(pass.end.bind(pass));
25     return pass;
26   }
27
28   var options = defaults(opt, {
29     read: true,
30     buffer: true
31   });
32
33   var globStream = gs.create(glob, options);
34
35   // when people write to use just pass it through
36   var outputStream = globStream
37     .pipe(through.obj(createFile))
38     .pipe(getStats(options));
39
40   if (options.read !== false) {
41     outputStream = outputStream
42       .pipe(getContents(options));
43   }
44
45   return outputStream.pipe(pass);
46 }
47
48 function isValidGlob(glob) {
49   if (typeof glob === 'string') {
50     return true;
51   }
52   if (Array.isArray(glob) && glob.length !== 0) {
53     return glob.every(isValidGlob);
54   }
55   if (Array.isArray(glob) && glob.length === 0) {
56     return true;
57   }
58   return false;
59 }
60
61 module.exports = src;