-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
65 lines (56 loc) · 1.67 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
var fs = require('fs');
var path = require('path');
var through = require('through');
var flatten = require('whisk/flatten');
var reLineBreak = require('reu/newline');
var reHttp = /^http/;
var reScript = /\<script(.*?)\><\/script\>/i;
var rePreprocessedUrl = /\<c\:url(.*)\/\>/i;
var pluck = require('whisk/pluck');
var toAttributes = require('parse-attributes');
function extractJspUrl(result) {
return rePreprocessedUrl.exec(result[1]) || result;
}
function isIncluded(includedPaths) {
return function(path) {
return !includedPaths || includedPaths.some(function(prefix) {
return path.slice(0, prefix.length) === prefix;
});
};
}
function isLocal(path) {
return !reHttp.test(path);
}
module.exports = function(opts) {
var remain = ((opts || {}).argv || {}).remain || [];
function addBasePaths(file) {
// if the file is absolute, then make relative
file = (file[0] === '/') ? file.slice(1) : file;
return (opts.path || []).map(function(basePath) {
return path.resolve(basePath, file);
});
}
function write(data) {
var lines = data.toString().split(reLineBreak).map(function(line) {
return reScript.exec(line);
}).filter(Boolean);
// extract c:url elements if they exist
lines
.map(extractJspUrl)
.map(pluck(1))
.map(toAttributes)
.map(function(data) {
return data.value || data.src;
})
.filter(isLocal)
.filter(isIncluded(opts.include))
.map(addBasePaths)
.reduce(flatten, [])
.filter(fs.existsSync)
.map(function(filename) {
return filename + '\n';
})
.forEach(this.queue.bind(this));
}
return through(write, function() {});
};