index.js
3.4 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
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
'use strict';
var fs = require('fs');
var MatcherCollection = require('matcher-collection');
var ensurePosix = require('ensure-posix-path');
function handleOptions(_options) {
var options = {};
if (Array.isArray(_options)) {
options.globs = _options;
} else if (_options) {
options = _options;
}
return options;
}
function handleRelativePath(_relativePath) {
if (_relativePath == null) {
return '';
} else if (_relativePath.slice(-1) !== '/') {
return _relativePath + '/';
} else {
return _relativePath;
}
}
module.exports = walkSync;
function walkSync(baseDir, _options) {
var options = handleOptions(_options);
return _walkSync(baseDir, options).map(function(entry) {
return entry.relativePath;
});
}
module.exports.entries = function entries(baseDir, _options) {
var options = handleOptions(_options);
return _walkSync(ensurePosix(baseDir), options);
};
function _walkSync(baseDir, options, _relativePath) {
// Inside this function, prefer string concatenation to the slower path.join
// https://github.com/joyent/node/pull/6929
var relativePath = handleRelativePath(_relativePath);
var globs = options.globs;
var ignorePatterns = options.ignore;
var globMatcher, ignoreMatcher;
var results = [];
if (ignorePatterns) {
ignoreMatcher = new MatcherCollection(ignorePatterns);
}
if (globs) {
globMatcher = new MatcherCollection(globs);
}
if (globMatcher && !globMatcher.mayContain(relativePath)) {
return results;
}
var names = fs.readdirSync(baseDir + '/' + relativePath);
var entries = names.map(function (name) {
var entryRelativePath = relativePath + name;
if (ignoreMatcher && ignoreMatcher.match(entryRelativePath)) {
return;
}
var fullPath = baseDir + '/' + entryRelativePath;
var stats = getStat(fullPath);
if (stats && stats.isDirectory()) {
return new Entry(entryRelativePath + '/', baseDir, stats.mode, stats.size, stats.mtime.getTime());
} else {
return new Entry(entryRelativePath, baseDir, stats && stats.mode, stats && stats.size, stats && stats.mtime.getTime());
}
}).filter(Boolean);
var sortedEntries = entries.sort(function (a, b) {
var aPath = a.relativePath;
var bPath = b.relativePath;
if (aPath === bPath) {
return 0;
} else if (aPath < bPath) {
return -1;
} else {
return 1;
}
});
for (var i=0; i<sortedEntries.length; ++i) {
var entry = sortedEntries[i];
if (entry.isDirectory()) {
if (options.directories !== false && (!globMatcher || globMatcher.match(entry.relativePath))) {
results.push(entry);
}
results = results.concat(_walkSync(baseDir, options, entry.relativePath));
} else {
if (!globMatcher || globMatcher.match(entry.relativePath)) {
results.push(entry);
}
}
}
return results;
}
function Entry(relativePath, basePath, mode, size, mtime) {
this.relativePath = relativePath;
this.basePath = basePath;
this.mode = mode;
this.size = size;
this.mtime = mtime;
}
Object.defineProperty(Entry.prototype, 'fullPath', {
get: function() {
return this.basePath + '/' + this.relativePath;
}
});
Entry.prototype.isDirectory = function () {
return (this.mode & 61440) === 16384;
};
function getStat(path) {
var stat;
try {
stat = fs.statSync(path);
} catch(error) {
if (error.code !== 'ENOENT') {
throw error;
}
}
return stat;
}