forked from todogroup/repolinter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_system.js
257 lines (236 loc) · 7.51 KB
/
file_system.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// Copyright 2017 TODO Group. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
const isBinaryFile = require('isbinaryfile')
const path = require('path')
const glob = require('matched')
const fs = require('fs')
/**
* Utility filesystem class that scopes operations to a given set of directories and a CWD
*
* @memberof repolinter
*/
class FileSystem {
constructor(targetDir = '.', filterPaths = []) {
this.targetDir = targetDir
this.filterPaths = filterPaths
}
/**
* Asynchronously checks if a file exists using fs.access
*
* @param {string} file An absolute path to verify the existence of
* @returns {Promise<boolean>} Whether or not the path exists
*/
static fileExists(file) {
return fs.promises
.access(file, fs.constants.F_OK)
.then(() => true)
.catch(() => false)
}
/**
* Asynchronously checks if a file exists using fs.access
*
* @param {string} file A path relative to targetDir to check the existence of
* @returns {Promise<boolean>} Whether or not the path exists
*/
relativeFileExists(file) {
return FileSystem.fileExists(path.resolve(this.targetDir, file))
}
/**
* Find the first file or directory matching a list of globs. Globs are
* searched from first to last. Returns the relative path of that file
* or directory, or undefined if none was found.
*
* @param {string | Array<string>} globs The globs to search with
* @param {boolean} nocase Whether or not to ignore case-sensitivity
* @returns {Promise<undefined | string>} A path relative to this.targetDir, or undefined if no items were found
*/
async findFirst(globs, nocase) {
const allFiles = await this.findAll(globs, nocase)
if (allFiles.length > 0) {
return allFiles[0]
}
}
/**
* Find the first file matching a list of globs. Globs are
* searched from first to last. Returns the relative path of that file,
* or undefined if none was found.
*
* @param {string | Array<string>} globs The globs to search with
* @param {boolean} nocase Whether or not to ignore case-sensitivity
* @returns {Promise<undefined | string>} A path relative to this.targetDir, or undefined if no items were found
*/
async findFirstFile(globs, nocase) {
const allFiles = await this.findAllFiles(globs, nocase)
if (allFiles.length > 0) {
return allFiles[0]
}
}
/**
* Find all files matching a list of globs. Globs are
* searched from first to last. Returns the relative path of all files,
* or undefined if none was found. Automatically removes symlinks
* from results.
*
* @param {string | Array<string>} globs The globs to search with
* @param {boolean} nocase Whether or not to ignore case-sensitivity
* @returns {Promise<Array<string>>} A list of paths relative to this.targetDir
*/
async findAllFiles(globs, nocase) {
const symlinks = {}
const filePaths = await this.glob(globs, {
cwd: this.targetDir,
nocase: !!nocase,
nodir: true,
symlinks
})
// Make symlinks relative
const onlySymlinks = {}
for (const fullPath in symlinks) {
if (symlinks[fullPath]) {
const relativeToRepoPath = this.normalizePath(
path.relative(this.targetDir, fullPath)
)
onlySymlinks[relativeToRepoPath] = true
}
}
// Remove all symlinks
return filePaths.filter(
filePath => !onlySymlinks[this.normalizePath(filePath)]
)
}
async glob(globs, options) {
const fixedGlobs =
typeof globs === 'string'
? this.normalizePath(globs)
: globs.map(g => this.normalizePath(g))
return (await glob(fixedGlobs, options))
.map(p => this.normalizePath(p))
.filter(p => this.shouldInclude(p))
}
/**
* Find all files or directories matching a list of globs. Globs are
* searched from first to last. Returns the relative path of all items,
* or undefined if none was found. Automatically removes symlinks
* from results.
*
* @param {string | Array<string>} globs The globs to search with
* @param {boolean} [nocase] Whether or not to ignore case-sensitivity
* @returns {Promise<Array<string>>} A list of paths relative to this.targetDir
*/
async findAll(globs, nocase = false) {
const fixedGlobs =
typeof globs === 'string'
? this.normalizePath(globs)
: globs.map(g => this.normalizePath(g))
return this.glob(fixedGlobs, { cwd: this.targetDir, nocase: !!nocase })
}
async isBinaryFile(relativeFile) {
const file = path.resolve(this.targetDir, relativeFile)
try {
return isBinaryFile.isBinaryFile(file)
} catch (e) {
// File doesn't exist or is a directory, so it isn't a binary file
if (e.message.includes('ENOENT')) {
return false
}
throw e
}
}
shouldInclude(filePath) {
if (this.filterPaths.length === 0) {
return true
}
const resolvedPath = this.normalizePath(
path.relative(this.targetDir, path.resolve(this.targetDir, filePath))
)
return this.filterPaths
.map(p => this.normalizePath(p))
.some(p => resolvedPath.startsWith(p))
}
normalizePath(filepath) {
if (process.platform === 'win32') {
return filepath.split(path.sep).join('/')
} else {
return filepath
}
}
/**
* Get the contents of a file in utf8 given a relative path
*
* @param {string} relativeFile A path relative to this.targetDir
* @returns {Promise<string | undefined>} A string with the file contents, or undefined if the file is not found.
*/
async getFileContents(relativeFile) {
const file = path.resolve(this.targetDir, relativeFile)
try {
return await fs.promises.readFile(file, 'utf8')
} catch (e) {
return undefined
}
}
/**
* Set the contents of a file in utf8 given a relative path and contents.
*
* @param {string} relativeFile A path relative to this.targetDir
* @param {string} contents A string with the file contents
* @returns {Promise<void>}
*/
async setFileContents(relativeFile, contents) {
return fs.promises.writeFile(
path.resolve(this.targetDir, relativeFile),
contents
)
}
/**
* Remove the file at the specified path.
*
* @param {string} relativeFile
* @returns {Promise<void>}
*/
async removeFile(relativeFile) {
return fs.promises.unlink(path.resolve(this.targetDir, relativeFile))
}
async getFileLines(relativeFile, lineCount) {
const file = path.resolve(this.targetDir, relativeFile)
const fs = require('fs')
let fd
try {
fd = await fs.promises.open(path.resolve(this.targetDir, file), 'r')
} catch (e) {
if (fd) fd.close()
// File doesn't exist or is a directory
if (e.message.includes('ENOENT')) {
return undefined
}
throw e
}
var bufferSize = 1024
var buffer = Buffer.alloc(bufferSize)
var lines = ''
var lineNumber = 0
var leftOver = ''
var idxStart, idx
while (true) {
const ret = await fd.read(buffer, 0, bufferSize, null)
const read = ret.bytesRead
if (read === 0) {
break
}
leftOver += buffer.toString('utf8', 0, read)
idxStart = 0
while ((idx = leftOver.indexOf('\n', idxStart)) !== -1) {
lineNumber++
lines += leftOver.substring(idxStart, idx) + '\n'
idxStart = idx + 1
if (lineNumber >= lineCount) {
fd.close()
return lines
}
}
leftOver = leftOver.substring(idxStart)
}
fd.close()
return lines
}
}
module.exports = FileSystem