This repository was archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 129
Expand file tree
/
Copy pathload-paths-handler.js
More file actions
177 lines (153 loc) · 4.34 KB
/
load-paths-handler.js
File metadata and controls
177 lines (153 loc) · 4.34 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
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
/* global emit */
const async = require('async')
const fs = require('fs')
const path = require('path')
const {GitRepository} = require('atom')
const {Minimatch} = require('minimatch')
const childProcess = require('child_process')
const PathsChunkSize = 100
const emittedPaths = new Set()
class PathLoader {
constructor (rootPath, ignoreVcsIgnores, traverseSymlinkDirectories, ignoredNames) {
this.rootPath = rootPath
this.traverseSymlinkDirectories = traverseSymlinkDirectories
this.ignoredNames = ignoredNames
this.paths = []
this.inodes = new Set()
this.repo = null
if (ignoreVcsIgnores) {
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.repo && !this.traverseSymlinkDirectories) {
this.loadFromGit().then(done)
return
}
this.loadPath(this.rootPath, true, () => {
this.flushPaths()
if (this.repo != null) this.repo.destroy()
done()
})
}
async loadFromGit () {
return new Promise((resolve) => {
const args = ['ls-files', '--cached', '--exclude-standard', '--others', '-z']
for (let ignoredName of this.ignoredNames) {
args.push('--exclude', ignoredName.pattern)
}
let output = ''
// TODO: do this via a call to GitRepository (needs to be implemented).
const result = childProcess.spawn('git', args, {cwd: this.rootPath})
result.stdout.on('data', chunk => {
const files = (output + chunk).split('\0')
output = files.pop()
for (const file of files) {
this.pathLoaded(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 = []) {
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.each(
rootPaths,
(rootPath, next) =>
new PathLoader(
rootPath,
ignoreVcsIgnores,
followSymlinks,
ignoredNames
).load(next)
,
this.async()
)
}