This repository has been archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 138
/
load-paths-handler.js
194 lines (168 loc) · 4.94 KB
/
load-paths-handler.js
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
/* global emit */
const async = require('async')
const fs = require('fs')
const os = require('os')
const path = require('path')
const {GitRepository} = require('atom')
const {Minimatch} = require('minimatch')
const childProcess = require('child_process')
const { rgPath } = require('vscode-ripgrep')
const PathsChunkSize = 100
// Use the unpacked path if the ripgrep binary is in asar archive.
const realRgPath = rgPath.replace(/\bapp\.asar\b/, 'app.asar.unpacked')
// Define the maximum number of concurrent crawling processes based on the number of CPUs
// with a maximum value of 8 and minimum of 1.
const MaxConcurrentCrawls = Math.min(Math.max(os.cpus().length - 1, 8), 1)
const emittedPaths = new Set()
class PathLoader {
constructor (rootPath, ignoreVcsIgnores, traverseSymlinkDirectories, ignoredNames, useRipGrep) {
this.rootPath = rootPath
this.traverseSymlinkDirectories = traverseSymlinkDirectories
this.ignoredNames = ignoredNames
this.useRipGrep = useRipGrep
this.paths = []
this.inodes = new Set()
this.repo = null
if (ignoreVcsIgnores && !this.useRipGrep) {
const repo = GitRepository.open(this.rootPath, {refreshOnWindowFocus: false})
if ((repo && repo.relativize(path.join(this.rootPath, 'test'))) === 'test') {
this.repo = repo
}
}
}
load (done) {
if (this.useRipGrep) {
this.loadFromRipGrep().then(done)
return
}
this.loadPath(this.rootPath, true, () => {
this.flushPaths()
if (this.repo != null) this.repo.destroy()
done()
})
}
async loadFromRipGrep () {
return new Promise((resolve) => {
const args = ['--files', '--hidden', '--sort', 'path']
if (this.ignoreVcsIgnores) {
args.push('--no-ignore')
}
if (this.traverseSymlinkDirectories) {
args.push('--follow')
}
for (let ignoredName of this.ignoredNames) {
args.push('-g', '!' + ignoredName.pattern)
}
let output = ''
const result = childProcess.spawn(realRgPath, args, {cwd: this.rootPath})
result.stdout.on('data', chunk => {
const files = (output + chunk).split('\n')
output = files.pop()
for (const file of files) {
this.pathLoaded(path.join(this.rootPath, file))
}
})
result.on('close', () => {
this.flushPaths()
resolve()
})
})
}
isIgnored (loadedPath) {
const relativePath = path.relative(this.rootPath, loadedPath)
if (this.repo && this.repo.isPathIgnored(relativePath)) {
return true
} else {
for (let ignoredName of this.ignoredNames) {
if (ignoredName.match(relativePath)) return true
}
}
}
pathLoaded (loadedPath, done) {
if (!emittedPaths.has(loadedPath)) {
this.paths.push(loadedPath)
emittedPaths.add(loadedPath)
}
if (this.paths.length === PathsChunkSize) {
this.flushPaths()
}
done && done()
}
flushPaths () {
emit('load-paths:paths-found', this.paths)
this.paths = []
}
loadPath (pathToLoad, root, done) {
if (this.isIgnored(pathToLoad) && !root) return done()
fs.lstat(pathToLoad, (error, stats) => {
if (error != null) { return done() }
if (stats.isSymbolicLink()) {
fs.stat(pathToLoad, (error, stats) => {
if (error != null) return done()
if (this.inodes.has(stats.ino)) {
return done()
} else {
this.inodes.add(stats.ino)
}
if (stats.isFile()) {
this.pathLoaded(pathToLoad, done)
} else if (stats.isDirectory()) {
if (this.traverseSymlinkDirectories) {
this.loadFolder(pathToLoad, done)
} else {
done()
}
} else {
done()
}
})
} else {
this.inodes.add(stats.ino)
if (stats.isDirectory()) {
this.loadFolder(pathToLoad, done)
} else if (stats.isFile()) {
this.pathLoaded(pathToLoad, done)
} else {
done()
}
}
})
}
loadFolder (folderPath, done) {
fs.readdir(folderPath, (_, children = []) => {
async.each(
children,
(childName, next) => {
this.loadPath(path.join(folderPath, childName), false, next)
},
done
)
})
}
}
module.exports = function (rootPaths, followSymlinks, ignoreVcsIgnores, ignores, useRipGrep) {
const ignoredNames = []
for (let ignore of ignores) {
if (ignore) {
try {
ignoredNames.push(new Minimatch(ignore, {matchBase: true, dot: true}))
} catch (error) {
console.warn(`Error parsing ignore pattern (${ignore}): ${error.message}`)
}
}
}
async.eachLimit(
rootPaths,
MaxConcurrentCrawls,
(rootPath, next) =>
new PathLoader(
rootPath,
ignoreVcsIgnores,
followSymlinks,
ignoredNames,
useRipGrep
).load(next)
,
this.async()
)
}