File size: 5,506 Bytes
bc20498
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
'use strict';
const fsNode = require("fs");
const MatcherCollection = require("matcher-collection");
const ensurePosix = require("ensure-posix-path");
const path = require("path");
const minimatch_1 = require("minimatch");
function walkSync(baseDir, inputOptions) {
    const options = handleOptions(inputOptions);
    let mapFunct;
    if (options.includeBasePath) {
        mapFunct = function (entry) {
            return entry.basePath.split(path.sep).join('/').replace(/\/+$/, '') + '/' + entry.relativePath;
        };
    }
    else {
        mapFunct = function (entry) {
            return entry.relativePath;
        };
    }
    return _walkSync(baseDir, options, null, []).map(mapFunct);
}
function getStat(path, fs) {
    try {
        return fs.statSync(path);
    }
    catch (error) {
        if (error !== null && typeof error === 'object' && (error.code === 'ENOENT' || error.code === 'ENOTDIR')) {
            return;
        }
        throw error;
    }
}
(function (walkSync) {
    function entries(baseDir, inputOptions) {
        const options = handleOptions(inputOptions);
        return _walkSync(ensurePosix(baseDir), options, null, []);
    }
    walkSync.entries = entries;
    ;
    class Entry {
        constructor(relativePath, basePath, mode, size, mtime) {
            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;
        }
    }
    walkSync.Entry = Entry;
})(walkSync || (walkSync = {}));
function isDefined(val) {
    return typeof val !== 'undefined';
}
function handleOptions(_options) {
    let options = {};
    if (Array.isArray(_options)) {
        options.globs = _options;
    }
    else if (_options) {
        options = _options;
    }
    if (!options.fs)
        options.fs = fsNode;
    return options;
}
function applyGlobOptions(globs, options) {
    return globs === null || globs === void 0 ? void 0 : globs.map(glob => {
        if (typeof glob === 'string') {
            return new minimatch_1.Minimatch(glob, options);
        }
        return glob;
    });
}
function handleRelativePath(_relativePath) {
    if (_relativePath == null) {
        return '';
    }
    else if (_relativePath.slice(-1) !== '/') {
        return _relativePath + '/';
    }
    else {
        return _relativePath;
    }
}
function lexicographically(a, b) {
    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, options, _relativePath, visited) {
    const { fs } = options;
    // 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;
        let results = [];
        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();
    }
}
module.exports = walkSync;