forked from wiledal/gulp-include
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
115 lines (91 loc) · 3.64 KB
/
index.js
File metadata and controls
115 lines (91 loc) · 3.64 KB
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
var fs = require('fs'),
path = require('path'),
es = require('event-stream'),
gutil = require('gulp-util'),
glob = require('glob');
var extensions = null,
includedFiles = [];
module.exports = function (params) {
var params = params || {};
includedFiles = [];
extensions = null;
if (params.extensions) {
extensions = typeof params.extensions === 'string' ? [params.extensions] : params.extensions;
}
function esi(file, callback) {
if (file.isNull()) {
return callback(null, file);
}
if (file.isStream()) {
throw new gutil.PluginError('gulp-esi-include', 'stream not supported');
}
if (file.isBuffer()) {
var newText = processInclude(String(file.contents), file.path, params);
file.contents = new Buffer(newText);
}
callback(null, file);
}
return es.map(esi)
};
function processInclude(content, filePath, params) {
var matches = content.match(/^(\s+)?\<esi\:(include)\s+src\s?=\s?"(.+)".+$/mg);
var relativeBasePath = path.dirname(filePath);
if (!matches) return content;
for (var i = 0; i < matches.length; i++) {
var leadingWhitespaceMatch = matches[i].match(/^(\s+)/);
var leadingWhitespace = null;
if (leadingWhitespaceMatch) {
leadingWhitespace = leadingWhitespaceMatch[0];
if (leadingWhitespaceMatch[0].indexOf("\n") > -1) leadingWhitespace = leadingWhitespaceMatch[0].split("\n")[1];
leadingWhitespace = leadingWhitespace.replace("\n", "");
}
// Remove beginnings, endings and trim.
var includeCommand = matches[i]
.replace(/(\s+)/gi, " ")
.replace(/<esi:|src\s?="|"\s?\/>/g, "")
.replace(/['"]/g, "")
.trim();
var split = includeCommand.split(" ");
split[1] = params.override || split[1];
// Split the directive and the path
var includeType = split[0];
var includePath = relativeBasePath + "/" + split[1];
// Use glob for file searching
var fileMatches = glob.sync(includePath, {mark: true});
var replaceContent = null;
for (var y = 0; y < fileMatches.length; y++) {
var globbedFilePath = fileMatches[y];
// If directive is of type "require" and file already included, skip to next.
if (includeType == "require" && includedFiles.indexOf(globbedFilePath) > -1) continue;
// If not in extensions, skip this file
if (!inExtensions(globbedFilePath)) continue;
// Get file contents and apply recursive include on result
var fileContents = fs.readFileSync(globbedFilePath);
if (!replaceContent) replaceContent = "";
if (leadingWhitespace) fileContents = addLeadingWhitespace(leadingWhitespace, fileContents.toString());
replaceContent += processInclude(fileContents.toString(), globbedFilePath);
if (includedFiles.indexOf(globbedFilePath) == -1) includedFiles.push(globbedFilePath);
// If the last file did not have a line break, and it is not the last file in the matched glob,
// add a line break to the end
if (!replaceContent.trim().match(/\n$/) && y != fileMatches.length-1) replaceContent += "\n";
}
// REPLACE
if (replaceContent) {
content = content.replace(matches[i], function(){return replaceContent});
}
}
return content;
}
function addLeadingWhitespace(whitespace, string) {
return string.split("\n").map(function(line) {
return whitespace + line;
}).join("\n");
}
function inExtensions(filePath) {
if (!extensions) return true;
for (var i = 0; i < extensions.length; i++) {
var re = extensions[i] + "$";
if (filePath.match(re)) return true;
}
return false;
}