-
Notifications
You must be signed in to change notification settings - Fork 26
/
index.ts
201 lines (165 loc) · 5.63 KB
/
index.ts
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
197
198
199
200
201
'use strict';
import fsNode = require('fs');
import * as MatcherCollection from 'matcher-collection';
import ensurePosix = require('ensure-posix-path');
import path = require('path');
import { IMinimatch, IOptions as MinimatchOptions, Minimatch } from 'minimatch';
function walkSync(baseDir: string, inputOptions?: walkSync.Options | (string|IMinimatch)[]) {
const options = handleOptions(inputOptions);
let mapFunct: (arg: walkSync.Entry) => string;
if (options.includeBasePath) {
mapFunct = function (entry: walkSync.Entry) {
return entry.basePath.split(path.sep).join('/').replace(/\/+$/, '') + '/' + entry.relativePath;
};
} else {
mapFunct = function (entry: walkSync.Entry) {
return entry.relativePath;
};
}
return _walkSync(baseDir, options, null, []).map(mapFunct);
}
export = walkSync;
function getStat(path: string, fs: walkSync.Options['fs'] = fsNode) {
try {
return fs.statSync(path);
} catch(error) {
if (error !== null && typeof error === 'object' && (error.code === 'ENOENT' || error.code === 'ENOTDIR' || error.code === 'EPERM')) {
return;
}
throw error;
}
}
namespace walkSync {
export function entries(baseDir: string, inputOptions?: Options | (string|IMinimatch)[]) {
const options = handleOptions(inputOptions);
return _walkSync(ensurePosix(baseDir), options, null, []);
};
export interface Options {
includeBasePath?: boolean,
globs?: (string|IMinimatch)[],
ignore?: (string|IMinimatch)[],
directories?: boolean,
fs?: typeof fsNode,
globOptions?: MinimatchOptions,
}
export class Entry {
relativePath: string;
basePath: string;
mode: number;
size: number;
mtime: number;
constructor(relativePath: string, basePath: string, mode: number, size: number, mtime: number) {
this.relativePath = relativePath;
this.basePath = basePath;
this.mode = mode;
this.size = size;
this.mtime = mtime;
}
get fullPath() {
return `${this.basePath}/${this.relativePath}`;
}
isDirectory() {
return (this.mode & 61440) === 16384;
}
}
}
function isDefined<T>(val: T | undefined) : val is T {
return typeof val !== 'undefined';
}
function handleOptions(_options?: walkSync.Options | (string|IMinimatch)[]) : walkSync.Options {
let options: walkSync.Options = {};
if (Array.isArray(_options)) {
options.globs = _options;
} else if (_options) {
options = _options;
}
return options;
}
function applyGlobOptions(globs: (string|IMinimatch)[] | undefined, options: MinimatchOptions) {
return globs?.map(glob => {
if (typeof glob === 'string') {
return new Minimatch(glob, options);
}
return glob;
})
}
function handleRelativePath(_relativePath: string | null) {
if (_relativePath == null) {
return '';
} else if (_relativePath.slice(-1) !== '/') {
return _relativePath + '/';
} else {
return _relativePath;
}
}
function lexicographically(a: walkSync.Entry, b: walkSync.Entry) {
const aPath = a.relativePath;
const bPath = b.relativePath;
if (aPath === bPath) {
return 0;
} else if (aPath < bPath) {
return -1;
} else {
return 1;
}
}
function _walkSync(baseDir: string, options: walkSync.Options, _relativePath: string | null, visited: string[]) : walkSync.Entry[] {
const fs = options.fs ?? fsNode;
// Inside this function, prefer string concatenation to the slower path.join
// https://github.com/joyent/node/pull/6929
const relativePath = handleRelativePath(_relativePath);
const realPath = fs.realpathSync(baseDir + '/' + relativePath);
if (visited.indexOf(realPath) >= 0) {
return [];
} else {
visited.push(realPath);
}
try {
const globOptions = options.globOptions;
const ignorePatterns = (isDefined(globOptions)) ? applyGlobOptions(options.ignore, globOptions) : options.ignore;
const globs = (isDefined(globOptions)) ? applyGlobOptions(options.globs, globOptions) : options.globs;
let globMatcher;
let ignoreMatcher: undefined | InstanceType<typeof MatcherCollection>;
let results: walkSync.Entry[] = [];
if (ignorePatterns) {
ignoreMatcher = new MatcherCollection(ignorePatterns);
}
if (globs) {
globMatcher = new MatcherCollection(globs);
}
if (globMatcher && !globMatcher.mayContain(relativePath)) {
return results;
}
const names = fs.readdirSync(baseDir + '/' + relativePath);
const entries = names.map(name => {
let entryRelativePath = relativePath + name;
if (ignoreMatcher && ignoreMatcher.match(entryRelativePath)) {
return;
}
let fullPath = baseDir + '/' + entryRelativePath;
let stats = getStat(fullPath, fs);
if (stats && stats.isDirectory()) {
return new walkSync.Entry(entryRelativePath + '/', baseDir, stats.mode, stats.size, stats.mtime.getTime());
} else {
return new walkSync.Entry(entryRelativePath, baseDir, stats && stats.mode || 0, stats && stats.size || 0, stats && stats.mtime.getTime() || 0);
}
}).filter(isDefined);
const sortedEntries = entries.sort(lexicographically);
for (let i = 0; i<sortedEntries.length; ++i) {
let 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, visited));
} else {
if (!globMatcher || globMatcher.match(entry.relativePath)) {
results.push(entry);
}
}
}
return results;
} finally {
visited.pop();
}
}