-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
fileSystem.ts
306 lines (260 loc) · 10.1 KB
/
fileSystem.ts
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
* fileSystem.ts
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT license.
*
* A "file system provider" abstraction that allows us to swap out a
* real file system implementation for a virtual (mocked) implementation
* for testing.
*/
/* eslint-disable no-dupe-class-members */
// * NOTE * except tests, this should be only file that import "fs"
import * as chokidar from 'chokidar';
import * as fs from 'fs';
import * as tmp from 'tmp';
// Automatically remove files created by tmp at process exit.
tmp.setGracefulCleanup();
import { ConsoleInterface, NullConsole } from './console';
import { createDeferred } from './deferred';
export type FileWatcherEventType = 'add' | 'addDir' | 'change' | 'unlink' | 'unlinkDir';
export type FileWatcherEventHandler = (eventName: FileWatcherEventType, path: string, stats?: Stats) => void;
export interface FileWatcher {
close(): void;
}
export interface Stats {
size: number;
isFile(): boolean;
isDirectory(): boolean;
isBlockDevice(): boolean;
isCharacterDevice(): boolean;
isSymbolicLink(): boolean;
isFIFO(): boolean;
isSocket(): boolean;
}
export interface MkDirOptions {
recursive: boolean;
// Not supported on Windows so commented out.
// mode: string | number;
}
export interface TmpfileOptions {
postfix?: string;
prefix?: string;
}
export interface FileSystem {
existsSync(path: string): boolean;
mkdirSync(path: string, options?: MkDirOptions | number): void;
chdir(path: string): void;
readdirEntriesSync(path: string): fs.Dirent[];
readdirSync(path: string): string[];
readFileSync(path: string, encoding?: null): Buffer;
readFileSync(path: string, encoding: BufferEncoding): string;
readFileSync(path: string, encoding?: BufferEncoding | null): string | Buffer;
writeFileSync(path: string, data: string | Buffer, encoding: BufferEncoding | null): void;
statSync(path: string): Stats;
unlinkSync(path: string): void;
realpathSync(path: string): string;
getModulePath(): string;
createFileSystemWatcher(paths: string[], listener: FileWatcherEventHandler): FileWatcher;
createReadStream(path: string): fs.ReadStream;
createWriteStream(path: string): fs.WriteStream;
copyFileSync(src: string, dst: string): void;
// Async I/O
readFile(path: string): Promise<Buffer>;
readFileText(path: string, encoding?: BufferEncoding): Promise<string>;
// The directory returned by tmpdir must exist and be the same each time tmpdir is called.
tmpdir(): string;
tmpfile(options?: TmpfileOptions): string;
}
export interface FileWatcherProvider {
createFileWatcher(paths: string[], listener: FileWatcherEventHandler): FileWatcher;
}
// Callers can specify a different file watcher provider if desired.
// By default, we'll use the file watcher based on chokidar.
export function createFromRealFileSystem(
console?: ConsoleInterface,
fileWatcherProvider?: FileWatcherProvider
): FileSystem {
return new RealFileSystem(fileWatcherProvider ?? new ChokidarFileWatcherProvider(console ?? new NullConsole()));
}
// File watchers can give "changed" event even for a file open. but for those cases,
// it will give relative path rather than absolute path. To get rid of such cases,
// we will drop any event with relative paths. this trick is copied from VS Code
// (https://github.com/microsoft/vscode/blob/master/src/vs/platform/files/node/watcher/unix/chokidarWatcherService.ts)
export function ignoredWatchEventFunction(paths: string[]) {
const normalizedPaths = paths.map((p) => p.toLowerCase());
return (path: string): boolean => {
if (!path || path.indexOf('__pycache__') >= 0) {
return true;
}
const normalizedPath = path.toLowerCase();
return normalizedPaths.every((p) => normalizedPath.indexOf(p) < 0);
};
}
const _isMacintosh = process.platform === 'darwin';
const _isLinux = process.platform === 'linux';
class RealFileSystem implements FileSystem {
private _fileWatcherProvider: FileWatcherProvider;
private _tmpdir?: string;
constructor(fileWatcherProvider: FileWatcherProvider) {
this._fileWatcherProvider = fileWatcherProvider;
}
existsSync(path: string) {
return fs.existsSync(path);
}
mkdirSync(path: string, options?: MkDirOptions | number) {
fs.mkdirSync(path, options);
}
chdir(path: string) {
process.chdir(path);
}
readdirSync(path: string): string[] {
return fs.readdirSync(path);
}
readdirEntriesSync(path: string): fs.Dirent[] {
return fs.readdirSync(path, { withFileTypes: true });
}
readFileSync(path: string, encoding?: null): Buffer;
readFileSync(path: string, encoding: BufferEncoding): string;
readFileSync(path: string, encoding?: BufferEncoding | null): Buffer | string;
readFileSync(path: string, encoding: BufferEncoding | null = null) {
return fs.readFileSync(path, { encoding });
}
writeFileSync(path: string, data: string | Buffer, encoding: BufferEncoding | null) {
fs.writeFileSync(path, data, { encoding });
}
statSync(path: string) {
return fs.statSync(path);
}
unlinkSync(path: string) {
fs.unlinkSync(path);
}
realpathSync(path: string) {
return fs.realpathSync(path);
}
getModulePath(): string {
// The entry point to the tool should have set the __rootDirectory
// global variable to point to the directory that contains the
// typeshed-fallback directory.
return (global as any).__rootDirectory;
}
createFileSystemWatcher(paths: string[], listener: FileWatcherEventHandler): FileWatcher {
return this._fileWatcherProvider.createFileWatcher(paths, listener);
}
createReadStream(path: string): fs.ReadStream {
return fs.createReadStream(path);
}
createWriteStream(path: string): fs.WriteStream {
return fs.createWriteStream(path);
}
copyFileSync(src: string, dst: string): void {
fs.copyFileSync(src, dst);
}
readFile(path: string): Promise<Buffer> {
const d = createDeferred<Buffer>();
fs.readFile(path, (e, b) => {
if (e) {
d.reject(e);
} else {
d.resolve(b);
}
});
return d.promise;
}
readFileText(path: string, encoding: BufferEncoding): Promise<string> {
const d = createDeferred<string>();
fs.readFile(path, { encoding }, (e, s) => {
if (e) {
d.reject(e);
} else {
d.resolve(s);
}
});
return d.promise;
}
tmpdir() {
if (!this._tmpdir) {
const dir = tmp.dirSync({ prefix: 'pyright' });
this._tmpdir = dir.name;
}
return this._tmpdir;
}
tmpfile(options?: TmpfileOptions): string {
const f = tmp.fileSync({ dir: this.tmpdir(), discardDescriptor: true, ...options });
return f.name;
}
}
class ChokidarFileWatcherProvider implements FileWatcherProvider {
constructor(private _console: ConsoleInterface) {}
createFileWatcher(paths: string[], listener: FileWatcherEventHandler): FileWatcher {
return this._createFileSystemWatcher(paths).on('all', listener);
}
createReadStream(path: string): fs.ReadStream {
return fs.createReadStream(path);
}
createWriteStream(path: string): fs.WriteStream {
return fs.createWriteStream(path);
}
private _createFileSystemWatcher(paths: string[]): chokidar.FSWatcher {
// The following options are copied from VS Code source base. It also
// uses chokidar for its file watching.
const watcherOptions: chokidar.WatchOptions = {
ignoreInitial: true,
ignorePermissionErrors: true,
followSymlinks: true, // this is the default of chokidar and supports file events through symlinks
interval: 1000, // while not used in normal cases, if any error causes chokidar to fallback to polling, increase its intervals
binaryInterval: 1000,
disableGlobbing: true, // fix https://github.com/Microsoft/vscode/issues/4586
awaitWriteFinish: {
// this will make sure we re-scan files once file changes are written to disk
stabilityThreshold: 1000,
pollInterval: 1000,
},
};
if (_isMacintosh) {
// Explicitly disable on MacOS because it uses up large amounts of memory
// and CPU for large file hierarchies, resulting in instability and crashes.
watcherOptions.usePolling = false;
}
const excludes: string[] = ['**/__pycache__/**'];
if (_isMacintosh || _isLinux) {
if (paths.some((path) => path === '' || path === '/')) {
excludes.push('/dev/**');
if (_isLinux) {
excludes.push('/proc/**', '/sys/**');
}
}
}
watcherOptions.ignored = excludes;
const watcher = chokidar.watch(paths, watcherOptions);
watcher.on('error', (_) => {
this._console.error('Error returned from file system watcher.');
});
// Detect if for some reason the native watcher library fails to load
if (_isMacintosh && !watcher.options.useFsEvents) {
this._console.info('Watcher could not use native fsevents library. File system watcher disabled.');
}
return watcher;
}
readFile(path: string): Promise<Buffer> {
const d = createDeferred<Buffer>();
fs.readFile(path, (e, b) => {
if (e) {
d.reject(e);
} else {
d.resolve(b);
}
});
return d.promise;
}
readFileText(path: string, encoding: BufferEncoding): Promise<string> {
const d = createDeferred<string>();
fs.readFile(path, { encoding }, (e, s) => {
if (e) {
d.reject(e);
} else {
d.resolve(s);
}
});
return d.promise;
}
}