forked from jsdoc/jsdoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspec-collection.js
98 lines (80 loc) · 2.17 KB
/
spec-collection.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
'use strict';
var fs = require('jsdoc/fs');
var path = require('jsdoc/path');
var klaw = require('klaw');
var specs = [];
var finalSpecs = [];
var createSpecObj = function(_path, root) {
function relativePath() {
return _path.replace(root, '').replace(/^[/\\]/, '').replace(/\\/g, '/');
}
return {
path: function() {
return _path;
},
relativePath: relativePath,
directory: function() {
return _path.replace(/[/\\][\s\w.-]*$/, '').replace(/\\/g, '/');
},
relativeDirectory: function() {
return relativePath().replace(/[/\\][\s\w.-]*$/, '').replace(/\\/g, '/');
},
filename: function() {
return _path.replace(/^.*[\\/]/, '');
}
};
};
var clearSpecs = exports.clearSpecs = function() {
specs.splice(0, specs.length);
};
function addSpec(file, target) {
target = target || specs;
target.push( createSpecObj(file) );
}
function isValidSpec(file, matcher) {
var result;
// valid specs must...
try {
// ...be a file
result = fs.statSync(file).isFile() &&
// ...match the matcher
matcher.test( path.basename(file) );
}
catch (e) {
result = false;
}
return result;
}
function shouldLoad(file, matcher) {
var result = false;
// should this spec run at the end?
if ( /schema\.js$/.test(file) && isValidSpec(file, matcher) ) {
addSpec(file, finalSpecs);
}
else {
result = isValidSpec(file, matcher);
}
return result;
}
exports.load = function(loadpath, matcher, clear, callback) {
var wannaBeSpecs = [];
if (clear === true) {
clearSpecs();
}
klaw(loadpath)
.on('data', function(spec) {
wannaBeSpecs.push(spec.path);
})
.on('end', function() {
for (var i = 0; i < wannaBeSpecs.length; i++) {
var file = wannaBeSpecs[i];
if ( shouldLoad(file, matcher) ) {
addSpec(file);
}
}
callback();
});
};
exports.getSpecs = function() {
return specs.concat(finalSpecs);
};