forked from marktext/marktext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileSearcher.js
More file actions
83 lines (69 loc) · 1.9 KB
/
fileSearcher.js
File metadata and controls
83 lines (69 loc) · 1.9 KB
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
import { spawn } from 'child_process'
import RipgrepDirectorySearcher from './ripgrepSearcher'
// Use ripgrep searcher to search for files on disk only.
class FileSearcher extends RipgrepDirectorySearcher {
searchInDirectory (directoryPath, pattern, options, numPathsFound) {
const args = ['--files']
if (options.followSymlinks) {
args.push('--follow')
}
if (options.includeHidden) {
args.push('--hidden')
}
if (options.noIgnore) {
args.push('--no-ignore')
}
for (const inclusion of this.prepareGlobs(options.inclusions, directoryPath)) {
args.push('--iglob', inclusion)
}
args.push('--')
args.push(directoryPath)
let child = null
try {
child = spawn(this.rgPath, args, {
cwd: directoryPath,
stdio: ['pipe', 'pipe', 'pipe']
})
} catch (err) {
return Promise.reject(err)
}
const didMatch = options.didMatch || (() => {})
let cancelled = false
const returnedPromise = new Promise((resolve, reject) => {
let buffer = ''
let bufferError = ''
child.on('close', (code, signal) => {
// code 1 is used when no results are found.
if (code !== null && code > 1) {
reject(new Error(bufferError))
} else {
resolve()
}
})
child.on('error', err => {
reject(err)
})
child.stderr.on('data', chunk => {
bufferError += chunk
})
child.stdout.on('data', chunk => {
if (cancelled) {
return
}
buffer += chunk
const lines = buffer.split('\n')
buffer = lines.pop()
for (const line of lines) {
options.didSearchPaths(++numPathsFound.num)
didMatch(line)
}
})
})
returnedPromise.cancel = () => {
child.kill()
cancelled = true
}
return returnedPromise
}
}
export default FileSearcher