forked from wiledal/gulp-include
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
241 lines (196 loc) · 7.21 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
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;
var PREPEND_REGEX = /^[\/\s#]*\s*?((?:@codekit-prepend))\s+(.*$)/mg;
var APPEND_REGEX = /^[\/\s#]*\s*?((?:@codekit-append))\s+(.*$)/mg;
var requiredFiles = {},
extensions = [];
module.exports = function (params) {
var params = params || {};
requiredFiles = {};
extensions = [];
prependCache = '';
appendCache = '';
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, DIRECTIVE_REGEX);
newText = expand(newText, file.path, PREPEND_REGEX);
newText = prependCache + newText;
newText = expand(newText, file.path, APPEND_REGEX);
newText = newText + appendCache;
file.contents = new Buffer(newText);
}
callback(null, file);
}
return es.map(include)
};
function expand(fileContents, filePath, regex) {
var regexMatch,
matches = [],
returnText = fileContents,
i, j;
regex.lastIndex = 0;
while (regexMatch = 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 = "",
newMatchText = "",
files = globMatch(match, filePath),
fileName = "",
whitespace = null;
if (directiveType.indexOf("_tree") !== -1 || directiveType.indexOf("_directory") !== -1) {
thisMatchText += original + "\n";
}
for (j = 0; j < files.length; j++) {
fileName = files[j];
newMatchText = expand(String(fs.readFileSync(fileName)), fileName, regex);
//Try to retain the same indent level from the original include line
whitespace = original.match(/^\s+/);
if (whitespace) {
//Discard newlines
whitespace = whitespace[0].replace("\n", "");
//Is there some whitespace left?
if (whitespace) {
newMatchText = addLeadingWhitespace(whitespace, newMatchText);
}
}
thisMatchText += newMatchText + "\n";
if (directiveType.indexOf('require') !== -1 || directiveType.indexOf('include') !== -1 || directiveType.indexOf('codekit') !== -1) {
requiredFiles[fileName] = true;
}
}
thisMatchText = thisMatchText || original;
if(directiveType === '@codekit-prepend'){
returnText = prependString(returnText, start, end, thisMatchText);
}else if(directiveType === '@codekit-append'){
returnText = appendString(returnText, start, end, thisMatchText);
}else{
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' || directiveType.indexOf('codekit') !== -1) {
if (relativeFilePath.charAt(0).match(/['"]/g)) {
// optional [] on multiple files
relativeFilePath = '[' + relativeFilePath + ']';
}
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], 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;
//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);
}
function prependString(string, start, end, prepend) {
// cache prepend
prependCache = prepend + prependCache;
// remove directive
return string.substring(0, start) + string.substring(end);
}
function appendString(string, start, end, append) {
// cache append
appendCache = append + appendCache;
// remove directive
return string.substring(0, start) + string.substring(end);
}
function addLeadingWhitespace(whitespace, string) {
return string.split("\n").map(function(line) {
return whitespace + line;
}).join("\n");
}
//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;
}