forked from wiledal/gulp-include
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
184 lines (144 loc) · 5.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
var fs = require('fs'),
path = require('path'),
es = require('event-stream'),
gutil = require('gulp-util'),
glob = require('glob');
var DIRECTIVE_REGEX = /^[\/\s#]*?=\s*?((?:require|include)(?:_tree|_directory)?)\s+(.*$)/mg; //Note to Wiledal: Allowed asterisks, single quotes, and double quotes
// Note to wiledal: I replaced the hidden file regex just with a simple check if the first character in the file name is a period. Do you know of use cases where you would want it to be more complicated than that?
var requiredFiles = {},
extensions = [];
module.exports = function (params) {
var params = params || {};
if (params.extensions) {
extensions = typeof params.extensions === 'string' ? [params.extensions] : params.extensions;
}
function include(file, callback) {
if (file.isNull()) {
return callback(null, file);
}
if (file.isStream()) {
throw new gutil.PluginError('gulp-include', 'stream not supported');
}
if (file.isBuffer()) {
var newText = expand(String(file.contents), file.path) //Note to wiledal: Made this a function call so that it can call itself recursively
file.contents = new Buffer(newText);
}
callback(null, file);
}
return es.map(include)
};
function expand(fileContents, filePath) {
var regexMatch,
matches = [],
returnText = fileContents,
i, j;
DIRECTIVE_REGEX.lastIndex = 0;
while (regexMatch = DIRECTIVE_REGEX.exec(fileContents)) {
matches.push(regexMatch);
}
i = matches.length;
while (i--) {
var match = matches[i],
original = match[0],
directiveType = match[1],
start = match.index,
end = start + original.length,
thisMatchText = "",
files = globMatch(match, filePath);
if (directiveType.indexOf("_tree") !== -1 || directiveType.indexOf("_directory") !== -1) {
thisMatchText += original + "\n";
}
for (j = 0; j < files.length; j++) {
var fileName = files[j];
thisMatchText += expand(String(fs.readFileSync(fileName)), fileName) + "\n";
if (directiveType.indexOf('require') !== -1) requiredFiles[fileName] = true;
}
thisMatchText = thisMatchText || original;
returnText = replaceStringByIndices(returnText, start, end, thisMatchText);
}
return returnText ? returnText : fileContents;
}
function globMatch(match, filePath) {
var directiveType = match[1],
relativeFilePath = match[2],
files = [],
globs = [],
negations = [];
if (directiveType.indexOf('_tree') !== -1) {
relativeFilePath = relativeFilePath.concat('/**/*');
directiveType = directiveType.replace('_tree', '');
}
if (directiveType.indexOf('_directory') !== -1) {
relativeFilePath = relativeFilePath.concat('/*');
directiveType = directiveType.replace('_directory', '');
}
if (directiveType === 'require' || directiveType === 'include') {
if (relativeFilePath.charAt(0) === '[') {
relativeFilePath = eval(relativeFilePath);
for (var i = 0; i < relativeFilePath.length; i++) {
if (relativeFilePath[i].charAt(0) === '!') {
negations.push(relativeFilePath[i].slice(1))
} else {
globs.push(relativeFilePath[i])
}
}
} else {
globs.push(relativeFilePath);
}
for (var i = 0; i < globs.length; i++) {
var globFiles = _internalGlob(globs[i], filePath);
files = union(files, globFiles);
}
for (var i = 0; i < negations.length; i++) {
var negationFiles = _internalGlob(negations[i].substring(1), filePath);
files = difference(files, negationFiles);
}
}
return files;
}
function _internalGlob(thisGlob, filePath) {
var folderPath = path.dirname(filePath),
fullPath = path.join(folderPath, thisGlob.replace(/['"]/g, '')),
files;
files = glob.sync(fullPath, {
mark: true
});
files = files.filter(function (fileName) {
var slashSplit = fileName.split(/[\\\/]/),
thisExtension = fileName.split('.').pop();
//Ignore directories
if (slashSplit.pop() === '')
return false;
//Note to wiledal: This check is unneccessary since glob ignores hidden files by default
//Ignore hidden files
if (slashSplit.pop().slice(-1) === '.')
return false;
//Check for allowable extensions if specified, otherwise allow all extensions
if (extensions.length > 0 && extensions.indexOf(thisExtension) === -1) {
return false;
}
return true;
});
return files;
}
function replaceStringByIndices(string, start, end, replacement) {
return string.substring(0, start) + replacement + string.substring(end);
}
//We can't use lo-dash's union function because it wouldn't support this: ["*.js", "app.js"], which requires app.js to come last
function union(arr1, arr2) {
if (arr1.length == 0) return arr2;
var index;
for (var i = 0; i < arr2.length; i++) {
if ((index = arr1.indexOf(arr2[i])) !== -1) arr1.splice(index, 1)
}
return arr1.concat(arr2);
}
function difference(arr1, arr2) {
var index;
for (var i = 0; i < arr2.length; i++) {
while ((index = arr1.indexOf(arr2[i])) !== -1) {
arr1.splice(index, 1)
}
}
return arr1;
}