-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathfind.js
157 lines (143 loc) · 3.23 KB
/
find.js
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
'use strict';
const Funnel = require('broccoli-funnel');
const decompose = require('./utils/decompose');
const debug = require('debug')('broccoli-stew:find');
const merge = require('broccoli-merge-trees');
/**
* Allows you to find files in a tree via a glob patterns.
*
* @example
*
* given:
* root/package.json
* root/lib/bar.js
* root/lib/foo.js
* root/lib/bar.coffee
* root/lib/bar.txt
*
* let tree = 'root';
*
* find(tree) => identity;
*
* find('root/lib/*.js') => [
* 'root/lib/bar.js'
* 'root/lib/foo.js'
* ];
*
* find(tree, '*.js') => [
* 'lib/bar.js'
* 'lib/foo.js'
* ];
*
* find(tree, 'lib/*.{coffee.js}') => [
* 'lib/bar.js',
* 'lib/bar.coffee',
* 'lib/foo.js',
* ];
*
* find(tree, function(path) {
* return /*.js/.test(path);
* }) => [
* 'lib/bar.js',
* 'lib/foo.js',
* ];
*
* find(tree, { include: ['*.js'], exclude: [ 'bar.*' ]) => [
* 'lib/foo.js'
* ]);
*
* find(tree, { include: [/*.js/], exclude: [ 'bar.*' ]) => [
* 'lib/foo.js'
* ]);
*
* also given:
* other/foo/bar.jcs
* other/foo/bar.css
* other/foo/package.json
*
* other = 'other';
*
* find([
* root,
* other
*]) => [
* package.json
* lib/bar.js
* lib/foo.js
* lib/bar.coffee
* lib/bar.txt
* foo/bar.jcs
* foo/bar.css
* ]
**
* find([
* root,
* other
* ],'**//*.js') => [
* lib/bar.js
* lib/foo.js
* ]
*
* find([
* root,
* other
* ],{ overwrite: true }') => [
* lib/bar.js
* lib/foo.js
* ]
*
* @name find
* @argument tree
* @argument filter
*/
module.exports = function find(tree, a, b) {
let arity = arguments.length;
if (Array.isArray(tree)) {
let mergeOptions;
if (a&& typeof a === 'object' &&a.overwrite) {
mergeOptions = {
overwrite: true
};
}
return merge(tree.map(function(tree) {
return _find.apply(null, [tree, a, b].filter(Boolean));
}), mergeOptions);
} else {
return _find.apply(null, [tree, a, b].filter(Boolean));
}
};
function _find(tree, _options) {
let options = {};
let root = tree;
if (arguments.length === 1 && typeof root === 'string') {
let decomposition = decompose(root.replace(/[a-z]:[\\\/]/i,'/'));
root = decomposition.root;
options.destDir = root;
options.include = decomposition.include;
options.exclude = decomposition.exclude;
} else if (arguments.length > 1) {
if (typeof _options === 'object') {
options = _options;
} else if (arguments.length > 1 && typeof arguments[2] === 'object') {
options = arguments[2];
}
if (typeof _options === 'string') {
let pattern = decompose.expandSingle(_options);
let lastChar = pattern.charAt(pattern.length - 1);
if (typeof root === 'string') {
options.destDir = root;
} else if (root.inputTree && typeof root.inputTree.destDir === 'string') {
options.destDir = root.inputTree.destDir;
}
if (lastChar === '*' || lastChar !== '/') {
options.include = [pattern];
} else {
options.include = [pattern + '**/*'];
}
} else {
options = _options;
}
}
debug('%s include: %o exclude: %o destDir: %s', root, options.include, options.exclude, options.destDir);
return new Funnel(root, options);
}