Pathologic was missing because of a .git folder inside.
[yaffs-website] / node_modules / glob-stream / index.js
1 /*jslint node: true */
2
3 'use strict';
4
5 var through2 = require('through2');
6 var Combine = require('ordered-read-streams');
7 var unique = require('unique-stream');
8
9 var glob = require('glob');
10 var minimatch = require('minimatch');
11 var glob2base = require('glob2base');
12 var path = require('path');
13
14 var gs = {
15   // creates a stream for a single glob or filter
16   createStream: function(ourGlob, negatives, opt) {
17     if (!negatives) negatives = [];
18     if (!opt) opt = {};
19     if (typeof opt.cwd !== 'string') opt.cwd = process.cwd();
20     if (typeof opt.dot !== 'boolean') opt.dot = false;
21     if (typeof opt.silent !== 'boolean') opt.silent = true;
22     if (typeof opt.nonull !== 'boolean') opt.nonull = false;
23     if (typeof opt.cwdbase !== 'boolean') opt.cwdbase = false;
24     if (opt.cwdbase) opt.base = opt.cwd;
25
26     // remove path relativity to make globs make sense
27     ourGlob = unrelative(opt.cwd, ourGlob);
28     negatives = negatives.map(unrelative.bind(null, opt.cwd));
29
30     // create globbing stuff
31     var globber = new glob.Glob(ourGlob, opt);
32
33     // extract base path from glob
34     var basePath = opt.base ? opt.base : glob2base(globber);
35
36     // create stream and map events from globber to it
37     var stream = through2.obj(negatives.length ? filterNegatives : undefined);
38
39     globber.on('error', stream.emit.bind(stream, 'error'));
40     globber.on('end', function(/* some args here so can't use bind directly */){
41       stream.end();
42     });
43     globber.on('match', function(filename) {
44       stream.write({
45         cwd: opt.cwd,
46         base: basePath,
47         path: path.resolve(opt.cwd, filename)
48       });
49     });
50
51     return stream;
52
53     function filterNegatives(filename, enc, cb) {
54       var matcha = isMatch.bind(null, filename, opt);
55       if (negatives.every(matcha)) {
56         cb(null, filename); // pass
57       } else {
58         cb(); // ignore
59       }
60     }
61   },
62
63   // creates a stream for multiple globs or filters
64   create: function(globs, opt) {
65     if (!opt) opt = {};
66
67     // only one glob no need to aggregate
68     if (!Array.isArray(globs)) return gs.createStream(globs, null, opt);
69
70     var positives = globs.filter(isPositive);
71     var negatives = globs.filter(isNegative);
72
73     if (positives.length === 0) throw new Error("Missing positive glob");
74
75     // only one positive glob no need to aggregate
76     if (positives.length === 1) return gs.createStream(positives[0], negatives, opt);
77
78     // create all individual streams
79     var streams = positives.map(function(glob){
80       return gs.createStream(glob, negatives, opt);
81     });
82
83     // then just pipe them to a single unique stream and return it
84     var aggregate = new Combine(streams);
85     var uniqueStream = unique('path');
86
87     return aggregate.pipe(uniqueStream);
88   }
89 };
90
91 function isMatch(file, opt, pattern) {
92   if (typeof pattern === 'string') return minimatch(file.path, pattern, opt);
93   if (pattern instanceof RegExp) return pattern.test(file.path);
94   return true; // unknown glob type?
95 }
96
97 function isNegative(pattern) {
98   if (typeof pattern !== 'string') return true;
99   if (pattern[0] === '!') return true;
100   return false;
101 }
102
103 function isPositive(pattern) {
104   return !isNegative(pattern);
105 }
106
107 function unrelative(cwd, glob) {
108   var mod = '';
109   if (glob[0] === '!') {
110     mod = glob[0];
111     glob = glob.slice(1);
112   }
113   return mod+path.resolve(cwd, glob);
114 }
115
116
117 module.exports = gs;