forked from sourcegraph/javascript-typescript-langserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfs.ts
More file actions
86 lines (69 loc) · 2.15 KB
/
fs.ts
File metadata and controls
86 lines (69 loc) · 2.15 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
import {
RequestType, IConnection
} from 'vscode-languageserver';
import * as fs from 'fs';
import * as path_ from 'path';
export interface FileInfo {
name: string
size: number
dir: boolean
}
export interface FileSystem {
readDir(path: string, callback: (err: Error, result?: FileInfo[]) => void): void
readFile(path: string, callback: (err: Error, result?: string) => void): void
}
export namespace ReadDirRequest {
export const type: RequestType<string, FileInfo[], any> = { get method() { return 'fs/readDir'; } };
}
export namespace ReadFileRequest {
export const type: RequestType<string, string, any> = { get method() { return 'fs/readFile'; } };
}
export class RemoteFileSystem implements FileSystem {
private connection: IConnection;
constructor(connection: IConnection) {
this.connection = connection
}
readDir(path: string, callback: (err: Error | null, result?: FileInfo[]) => void) {
this.connection.sendRequest(ReadDirRequest.type, path).then((f: FileInfo[]) => {
return callback(null, f)
}, callback)
}
readFile(path: string, callback: (err: Error | null, result?: string) => void) {
this.connection.sendRequest(ReadFileRequest.type, path).then((content: string) => {
return callback(null, Buffer.from(content, 'base64').toString())
}, callback)
}
}
export class LocalFileSystem implements FileSystem {
private root: string;
constructor(root: string) {
this.root = root
}
readDir(path: string, callback: (err: Error | null, result?: FileInfo[]) => void): void {
path = path_.resolve(this.root, path);
fs.readdir(path, (err: Error, files: string[]) => {
if (err) {
return callback(err)
}
let ret: FileInfo[] = [];
files.forEach((f) => {
const stats: fs.Stats = fs.statSync(path_.resolve(path, f));
ret.push({
name: f,
size: stats.size,
dir: stats.isDirectory()
})
});
return callback(null, ret)
});
}
readFile(path: string, callback: (err: Error | null, result?: string) => void): void {
path = path_.resolve(this.root, path);
fs.readFile(path, (err: Error, buf: Buffer) => {
if (err) {
return callback(err)
}
return callback(null, buf.toString())
});
}
}