Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81164 views
1
exports.alphasort = alphasort
2
exports.alphasorti = alphasorti
3
exports.isAbsolute = process.platform === "win32" ? absWin : absUnix
4
exports.setopts = setopts
5
exports.ownProp = ownProp
6
exports.makeAbs = makeAbs
7
exports.finish = finish
8
exports.mark = mark
9
10
function ownProp (obj, field) {
11
return Object.prototype.hasOwnProperty.call(obj, field)
12
}
13
14
var path = require("path")
15
var minimatch = require("minimatch")
16
var Minimatch = minimatch.Minimatch
17
18
function absWin (p) {
19
if (absUnix(p)) return true
20
// pull off the device/UNC bit from a windows path.
21
// from node's lib/path.js
22
var splitDeviceRe =
23
/^([a-zA-Z]:|[\\\/]{2}[^\\\/]+[\\\/]+[^\\\/]+)?([\\\/])?([\s\S]*?)$/
24
var result = splitDeviceRe.exec(p)
25
var device = result[1] || ''
26
var isUnc = device && device.charAt(1) !== ':'
27
var isAbsolute = !!result[2] || isUnc // UNC paths are always absolute
28
29
return isAbsolute
30
}
31
32
function absUnix (p) {
33
return p.charAt(0) === "/" || p === ""
34
}
35
36
function alphasorti (a, b) {
37
return a.toLowerCase().localeCompare(b.toLowerCase())
38
}
39
40
function alphasort (a, b) {
41
return a.localeCompare(b)
42
}
43
44
45
function setopts (self, pattern, options) {
46
if (!options)
47
options = {}
48
49
// base-matching: just use globstar for that.
50
if (options.matchBase && -1 === pattern.indexOf("/")) {
51
if (options.noglobstar) {
52
throw new Error("base matching requires globstar")
53
}
54
pattern = "**/" + pattern
55
}
56
57
self.pattern = pattern
58
self.strict = options.strict !== false
59
self.dot = !!options.dot
60
self.mark = !!options.mark
61
self.nodir = !!options.nodir
62
if (self.nodir)
63
self.mark = true
64
self.sync = !!options.sync
65
self.nounique = !!options.nounique
66
self.nonull = !!options.nonull
67
self.nosort = !!options.nosort
68
self.nocase = !!options.nocase
69
self.stat = !!options.stat
70
self.noprocess = !!options.noprocess
71
72
self.maxLength = options.maxLength || Infinity
73
self.cache = options.cache || Object.create(null)
74
self.statCache = options.statCache || Object.create(null)
75
self.symlinks = options.symlinks || Object.create(null)
76
77
self.changedCwd = false
78
var cwd = process.cwd()
79
if (!ownProp(options, "cwd"))
80
self.cwd = cwd
81
else {
82
self.cwd = options.cwd
83
self.changedCwd = path.resolve(options.cwd) !== cwd
84
}
85
86
self.root = options.root || path.resolve(self.cwd, "/")
87
self.root = path.resolve(self.root)
88
if (process.platform === "win32")
89
self.root = self.root.replace(/\\/g, "/")
90
91
self.nomount = !!options.nomount
92
93
self.minimatch = new Minimatch(pattern, options)
94
self.options = self.minimatch.options
95
}
96
97
function finish (self) {
98
var nou = self.nounique
99
var all = nou ? [] : Object.create(null)
100
101
for (var i = 0, l = self.matches.length; i < l; i ++) {
102
var matches = self.matches[i]
103
if (!matches) {
104
if (self.nonull) {
105
// do like the shell, and spit out the literal glob
106
var literal = self.minimatch.globSet[i]
107
if (nou)
108
all.push(literal)
109
else
110
all[literal] = true
111
}
112
} else {
113
// had matches
114
var m = Object.keys(matches)
115
if (nou)
116
all.push.apply(all, m)
117
else
118
m.forEach(function (m) {
119
all[m] = true
120
})
121
}
122
}
123
124
if (!nou)
125
all = Object.keys(all)
126
127
if (!self.nosort)
128
all = all.sort(self.nocase ? alphasorti : alphasort)
129
130
// at *some* point we statted all of these
131
if (self.mark) {
132
for (var i = 0; i < all.length; i++) {
133
all[i] = self._mark(all[i])
134
}
135
if (self.nodir) {
136
all = all.filter(function (e) {
137
return !(/\/$/.test(e))
138
})
139
}
140
}
141
142
self.found = all
143
}
144
145
function mark (self, p) {
146
var c = self.cache[p]
147
var m = p
148
if (c) {
149
var isDir = c === 'DIR' || Array.isArray(c)
150
var slash = p.slice(-1) === '/'
151
152
if (isDir && !slash)
153
m += '/'
154
else if (!isDir && slash)
155
m = m.slice(0, -1)
156
157
if (m !== p) {
158
self.statCache[m] = self.statCache[p]
159
self.cache[m] = self.cache[p]
160
}
161
}
162
163
return m
164
}
165
166
// lotta situps...
167
function makeAbs (self, f) {
168
var abs = f
169
if (f.charAt(0) === "/") {
170
abs = path.join(self.root, f)
171
} else if (exports.isAbsolute(f)) {
172
abs = f
173
} else if (self.changedCwd) {
174
abs = path.resolve(self.cwd, f)
175
}
176
return abs
177
}
178
179