-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjade-path-writer.js
196 lines (164 loc) · 5.89 KB
/
jade-path-writer.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
var fs = require("fs");
var path = require("path");
var _ = require("underscore");
var resolve = require("resolve");
var vinylFile = require("vinyl-file");
//Params
// (object)[optional] options -
// {
// flag: (string) default "@@", flag before include to resolve in node_modules
// extension: (string) default "jade", file type of include,
// paths: (array) array of strings to always attempt to resolve partials in
// }
//Returns
// (stream) fully resolved stream of all included/extended files
module.exports = function JadePathWriter(options) {
options = options || {};
this.options = options;
//Trigger after include
this.options.flag = options.flag || "@@";
//File extension
this.options.extension = options.extension || "jade";
//Params
// (string) fileContents - contents of file to parse
//Return
// [{
// path: (string) matched file path,
// nodeModule: (bool) should resolve in node_modules
// }, ...]
this.parsePaths = function(fileContents) {
//Matches [includes|extends] [(optional)flag][path][extension]
//ex: extends ./templates/_overlay.jade
//ex: include @@my-templates/_button.jade
var jadeRegex = new RegExp("(?:include\\s|extends\\s)(?:[\'|\"])?(" + options.flag + "|)(.*\." + options.extension + "?)(?:[\'|\"|\\s])", "gi");
var match,
paths = [];
//Flag whether each parsed path should be resolved to node_modules or not
while((match = jadeRegex.exec(fileContents)) !== null) {
paths.push({
path: match[2],
nodeModule: match[1] === options.flag
});
}
return paths;
}
//Params
// (string) filePath - current file path (probably relative)
// (bool) isModule - whether to resolve in node_modules or not
// (array) currentPrefixes - prefixes to attempt to resolve file in
//Returns
// (string) fully resolved path. undefined if path does not exist
this.resolvePath = function(filePath, isModule, currentPrefixes) {
return isModule ? this.resolveNodePath(filePath) : this.resolveLocalPath(filePath, currentPrefixes);
}
//Params
// (string) filePath - current file path (probably relative)
// (array) currentPrefixes - prefixes to attempt to resolve file in
//Returns
// (string) fully resolved path. undefined if path does note exist
this.resolveLocalPath = function(filePath, currentPrefixes){
//Root and local directory are our basis
var prefixes = [
path.sep,
"." + path.sep
];
//Check other prefixes
if(options.paths) {
prefixes = prefixes.concat(this.options.paths);
}
//Merge our relative prefixes
if(currentPrefixes) {
prefixes = prefixes.concat(currentPrefixes)
}
//Make sure we have a leading slash for our path in case our prefixes are given without a separator
if(filePath[0] !== path.sep) {
filePath = path.sep + filePath;
}
//Sort by number of separators in prefix, descending
var sortedPrefixes = _.sortBy(prefixes, function(prefix){
return prefix.split(path.sep).length;
}).reverse();
//Break on our first existing path, returning the longest (most specific) matching path
var exists = false, testPath;
for(var i = 0; (i < sortedPrefixes.length) && (!exists); i++) {
testPath = path.resolve(sortedPrefixes[i] + filePath);
try {
fs.lstatSync(testPath);
exists = true;
} catch(e) {}
}
return exists ? testPath : undefined;
}
//Params
// (string) filePath - current file path (relative to node_modules)
//Returns
// (string) fully resolved path. undefined if path does not exist
this.resolveNodePath = function(filePath){
try {
var fullPath = resolve.sync(filePath);
} catch(e) {
return undefined;
}
return fullPath;
}
//Params
// (file) file - current file stream
// (array) paths - array of paths within file to rewrite
// [{
// path: (string) path within original file to replace,
// fullPath: (string) fully resolved path to write instead
// }, ...]
//Returns
// (file) rewritten file
this.rewritePaths = function(file, paths) {
var contents = String(file.contents);
_.each(paths, function(path) {
if(!path.nodeModule) {
contents = contents.replace(path.path, path.fullPath);
} else {
contents = contents.replace(options.flag + path.path, path.fullPath);
}
});
file.contents = new Buffer(contents, 'utf8');
return file;
}
//Called recursively to process the file tree of includes and extends
//Params
// (file) file - file stream to rewrite
// (array) prefixes - prefixes to check relative paths against
//Returns
// (array) all file streams
this.processFile = function(file, prefixes) {
//Matches common Jade includes and extends
var paths = this.parsePaths(String(file.contents)), resolvedPath, childFile, contents, dirname, files = [], allFiles;
_.each(paths, function(modulePath, ind) {
//Resolve each one of our paths
paths[ind].fullPath = this.resolvePath(modulePath.path, modulePath.nodeModule, prefixes);
//If path resolved successfully
if(paths[ind].fullPath) {
//Load file and pass current directory as prefix
childFile = vinylFile.readSync(paths[ind].fullPath);
dirname = path.dirname(paths[ind].fullPath);
//Process child files
//Remove all duplicates
allFiles = files.concat(this.processFile(childFile, [dirname]));
var isUnique = true;
allFiles.forEach(function(file){
isUnique = true;
files.forEach(function(testFile){
if(testFile.path === file.path) {
isUnique = false;
}
});
if(isUnique) {
files.push(file);
}
});
}
}.bind(this));
//rewrite paths in file
file = this.rewritePaths(file, paths);
files.push(file);
return files;
}
}