1 | 'use strict';
|
2 | const fsNode = require("fs");
|
3 | const MatcherCollection = require("matcher-collection");
|
4 | const ensurePosix = require("ensure-posix-path");
|
5 | const path = require("path");
|
6 | const minimatch_1 = require("minimatch");
|
7 | function walkSync(baseDir, inputOptions) {
|
8 | const options = handleOptions(inputOptions);
|
9 | let mapFunct;
|
10 | if (options.includeBasePath) {
|
11 | mapFunct = function (entry) {
|
12 | return entry.basePath.split(path.sep).join('/').replace(/\/+$/, '') + '/' + entry.relativePath;
|
13 | };
|
14 | }
|
15 | else {
|
16 | mapFunct = function (entry) {
|
17 | return entry.relativePath;
|
18 | };
|
19 | }
|
20 | return _walkSync(baseDir, options, null, []).map(mapFunct);
|
21 | }
|
22 | function getStat(path, fs = fsNode) {
|
23 | try {
|
24 | return fs.statSync(path);
|
25 | }
|
26 | catch (error) {
|
27 | if (error !== null && typeof error === 'object' && (error.code === 'ENOENT' || error.code === 'ENOTDIR' || error.code === 'EPERM')) {
|
28 | return;
|
29 | }
|
30 | throw error;
|
31 | }
|
32 | }
|
33 | (function (walkSync) {
|
34 | function entries(baseDir, inputOptions) {
|
35 | const options = handleOptions(inputOptions);
|
36 | return _walkSync(ensurePosix(baseDir), options, null, []);
|
37 | }
|
38 | walkSync.entries = entries;
|
39 | ;
|
40 | class Entry {
|
41 | constructor(relativePath, basePath, mode, size, mtime) {
|
42 | this.relativePath = relativePath;
|
43 | this.basePath = basePath;
|
44 | this.mode = mode;
|
45 | this.size = size;
|
46 | this.mtime = mtime;
|
47 | }
|
48 | get fullPath() {
|
49 | return `${this.basePath}/${this.relativePath}`;
|
50 | }
|
51 | isDirectory() {
|
52 | return (this.mode & 61440) === 16384;
|
53 | }
|
54 | }
|
55 | walkSync.Entry = Entry;
|
56 | })(walkSync || (walkSync = {}));
|
57 | function isDefined(val) {
|
58 | return typeof val !== 'undefined';
|
59 | }
|
60 | function handleOptions(_options) {
|
61 | let options = {};
|
62 | if (Array.isArray(_options)) {
|
63 | options.globs = _options;
|
64 | }
|
65 | else if (_options) {
|
66 | options = _options;
|
67 | }
|
68 | return options;
|
69 | }
|
70 | function applyGlobOptions(globs, options) {
|
71 | return globs === null || globs === void 0 ? void 0 : globs.map(glob => {
|
72 | if (typeof glob === 'string') {
|
73 | return new minimatch_1.Minimatch(glob, options);
|
74 | }
|
75 | return glob;
|
76 | });
|
77 | }
|
78 | function handleRelativePath(_relativePath) {
|
79 | if (_relativePath == null) {
|
80 | return '';
|
81 | }
|
82 | else if (_relativePath.slice(-1) !== '/') {
|
83 | return _relativePath + '/';
|
84 | }
|
85 | else {
|
86 | return _relativePath;
|
87 | }
|
88 | }
|
89 | function lexicographically(a, b) {
|
90 | const aPath = a.relativePath;
|
91 | const bPath = b.relativePath;
|
92 | if (aPath === bPath) {
|
93 | return 0;
|
94 | }
|
95 | else if (aPath < bPath) {
|
96 | return -1;
|
97 | }
|
98 | else {
|
99 | return 1;
|
100 | }
|
101 | }
|
102 | function _walkSync(baseDir, options, _relativePath, visited) {
|
103 | var _a;
|
104 | const fs = (_a = options.fs) !== null && _a !== void 0 ? _a : fsNode;
|
105 |
|
106 |
|
107 | const relativePath = handleRelativePath(_relativePath);
|
108 | const realPath = fs.realpathSync(baseDir + '/' + relativePath);
|
109 | if (visited.indexOf(realPath) >= 0) {
|
110 | return [];
|
111 | }
|
112 | else {
|
113 | visited.push(realPath);
|
114 | }
|
115 | try {
|
116 | const globOptions = options.globOptions;
|
117 | const ignorePatterns = (isDefined(globOptions)) ? applyGlobOptions(options.ignore, globOptions) : options.ignore;
|
118 | const globs = (isDefined(globOptions)) ? applyGlobOptions(options.globs, globOptions) : options.globs;
|
119 | let globMatcher;
|
120 | let ignoreMatcher;
|
121 | let results = [];
|
122 | if (ignorePatterns) {
|
123 | ignoreMatcher = new MatcherCollection(ignorePatterns);
|
124 | }
|
125 | if (globs) {
|
126 | globMatcher = new MatcherCollection(globs);
|
127 | }
|
128 | if (globMatcher && !globMatcher.mayContain(relativePath)) {
|
129 | return results;
|
130 | }
|
131 | const names = fs.readdirSync(baseDir + '/' + relativePath);
|
132 | const entries = names.map(name => {
|
133 | let entryRelativePath = relativePath + name;
|
134 | if (ignoreMatcher && ignoreMatcher.match(entryRelativePath)) {
|
135 | return;
|
136 | }
|
137 | let fullPath = baseDir + '/' + entryRelativePath;
|
138 | let stats = getStat(fullPath, fs);
|
139 | if (stats && stats.isDirectory()) {
|
140 | return new walkSync.Entry(entryRelativePath + '/', baseDir, stats.mode, stats.size, stats.mtime.getTime());
|
141 | }
|
142 | else {
|
143 | return new walkSync.Entry(entryRelativePath, baseDir, stats && stats.mode || 0, stats && stats.size || 0, stats && stats.mtime.getTime() || 0);
|
144 | }
|
145 | }).filter(isDefined);
|
146 | const sortedEntries = entries.sort(lexicographically);
|
147 | for (let i = 0; i < sortedEntries.length; ++i) {
|
148 | let entry = sortedEntries[i];
|
149 | if (entry.isDirectory()) {
|
150 | if (options.directories !== false && (!globMatcher || globMatcher.match(entry.relativePath))) {
|
151 | results.push(entry);
|
152 | }
|
153 | results = results.concat(_walkSync(baseDir, options, entry.relativePath, visited));
|
154 | }
|
155 | else {
|
156 | if (!globMatcher || globMatcher.match(entry.relativePath)) {
|
157 | results.push(entry);
|
158 | }
|
159 | }
|
160 | }
|
161 | return results;
|
162 | }
|
163 | finally {
|
164 | visited.pop();
|
165 | }
|
166 | }
|
167 | module.exports = walkSync;
|
168 |
|
\ | No newline at end of file |