-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
147 lines (119 loc) · 3.56 KB
/
index.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
'use strict';
var async = require('async');
var globHook = require('./hooks/glob');
var excludeHook = require('./hooks/exclude');
function gloth(patterns, options, cb) {
var matches = [],
i = -1,
hooks;
// Arguments parsing
if (typeof options === 'function') {
cb = options;
options = {};
} else if (!options) {
options = {};
}
// Transform the patterns into hooks
hooks = toHooks(patterns, options, true);
// Run each of them in series
async.forEachSeries(hooks, function (hook, next) {
i++;
hook(matches, function (err, hookMatches) {
if (err) {
return next(err);
}
matches = hookMatches;
// Check if the hook passed an array
if (!Array.isArray(matches)) {
return next(new Error('Hook ' + hookName(hook, i) + ' did not passed an array.'));
}
next();
});
}, function (err) {
if (err) {
return cb(err);
}
cb(null, unique(matches));
});
}
function glothSync(patterns, options, cb) {
var matches = [],
hooks;
// Arguments parsing
if (typeof options === 'function') {
cb = options;
options = {};
} else if (!options) {
options = {};
}
// Transform the patterns into hooks
hooks = toHooks(patterns, options, false);
// Execute each hook
hooks.forEach(function (hook, i) {
matches = hook(matches);
// Check if the hook passed an array
if (!Array.isArray(matches)) {
throw new Error('Hook ' + hookName(hook, i) + ' did not returned an array.');
}
});
return unique(matches);
}
function toHooks(patterns, options, async) {
// Ensure array
patterns = !Array.isArray(patterns) ? [patterns] : patterns;
// Return an array of hooks based on each pattern
return patterns.map(function (pattern, i) {
var hook;
// Replace include/exclude patterns with the built-in hooks
if (typeof pattern === 'string') {
if (pattern.charAt(0) !== '!') {
return async ? globHook(pattern, options) : globHook.sync(pattern, options);
} else {
pattern = excludeHook(pattern.substr(1));
}
}
hook = pattern;
// Check if is a valid hook
if (typeof hook !== 'function') {
throw new Error('Hook #' + i + ' is not a function.');
}
// Check if using an async hook in sync gloth usage
if (!async) {
if (hook.length >= 2) {
throw new Error('Hook ' + hookName(hook, i) + ' is async, but was used in gloth.sync.');
}
// Transform sync hooks into async in async gloth usage
} else {
if (hook.length <= 1) {
return function (matches, next) {
try {
next(null, hook(matches));
} catch (e) {
next(e);
}
};
}
}
return hook;
});
}
function hookName(hook, index) {
return hook.name ? hook.name : '#' + index;
}
function unique(arr) {
var index = {},
newArr = [],
i,
length = arr.length,
curr;
for (i = 0; i < length; i += 1) {
curr = arr[i];
if (!index[curr]) {
newArr.push(curr);
index[curr] = true;
}
}
return newArr;
}
module.exports = gloth;
module.exports.sync = glothSync;