Repository navigation
Expand file tree
/
Copy pathgithub1sfs.ts
More file actions
204 lines (178 loc) · 5.58 KB
/
Copy pathgithub1sfs.ts
File metadata and controls
204 lines (178 loc) · 5.58 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
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
/**
* @file VSCode GitHub1sFs Provider
* @author netcon
*/
import {
workspace,
Disposable,
FileSystemProvider,
FileSystemError,
Event,
EventEmitter,
FileChangeEvent,
FileStat,
FileType,
Uri,
} from 'vscode';
import { noop, dirname, reuseable } from './util';
import { readGitHubDirectory, readGitHubFile } from './api';
import { toUint8Array as decodeBase64 } from 'js-base64';
export class File implements FileStat {
type: FileType;
ctime: number;
mtime: number;
size: number;
name: string;
sha: string;
data?: Uint8Array;
constructor(public uri: Uri, name: string, options?: any) {
this.type = FileType.File;
this.ctime = Date.now();
this.mtime = Date.now();
this.name = name;
this.sha = (options && ('sha' in options)) ? options.sha : '';
this.size = (options && ('size' in options)) ? options.size : 0;
this.data = (options && ('data' in options)) ? options.data : null;
}
}
export class Directory implements FileStat {
type: FileType;
ctime: number;
mtime: number;
size: number;
sha: string;
name: string;
entries: Map<string, File | Directory>;
constructor(public uri: Uri, name: string, options?: any) {
this.type = FileType.Directory;
this.ctime = Date.now();
this.mtime = Date.now();
this.size = 0;
this.name = name;
this.entries = null;
this.sha = (options && ('sha' in options)) ? options.sha : '';
this.size = (options && ('size' in options)) ? options.size : 0;
}
}
export type Entry = File | Directory;
export class GitHub1sFS implements FileSystemProvider, Disposable {
static scheme = 'github1s';
private readonly disposable: Disposable;
private _emitter = new EventEmitter<FileChangeEvent[]>();
private root: Directory = null;
onDidChangeFile: Event<FileChangeEvent[]> = this._emitter.event;
constructor() {
this.disposable = Disposable.from(
workspace.registerFileSystemProvider(GitHub1sFS.scheme, this, { isCaseSensitive: true, isReadonly: true }),
);
}
dispose() {
this.disposable?.dispose();
}
// --- lookup
private async _lookup(uri: Uri, silent: false): Promise<Entry>;
private async _lookup(uri: Uri, silent: boolean): Promise<Entry | undefined>;
private async _lookup(uri: Uri, silent: boolean): Promise<Entry | undefined> {
let parts = uri.path.split('/').filter(Boolean);
let entry: Entry = this.root || (this.root = new Directory(uri.with({ path: '/' }), ''));
for (const part of parts) {
let child: Entry | undefined;
if (entry instanceof Directory) {
if (entry.entries === null) {
await this.readDirectory(Uri.joinPath(entry.uri, entry.name));
}
child = entry.entries.get(part);
}
if (!child) {
if (!silent) {
throw FileSystemError.FileNotFound(uri);
} else {
return undefined;
}
}
entry = child;
}
return entry;
}
private async _lookupAsDirectory(uri: Uri, silent: boolean): Promise<Directory> {
const entry = await this._lookup(uri, silent);
if (entry instanceof Directory) {
return entry;
}
if (!silent) {
throw FileSystemError.FileNotADirectory(uri);
}
}
private async _lookupAsFile(uri: Uri, silent: boolean): Promise<File> {
const entry = await this._lookup(uri, silent);
if (entry instanceof File) {
return entry;
}
if (!silent) {
throw FileSystemError.FileIsADirectory(uri);
}
}
private _lookupParentDirectory(uri: Uri): Promise<Directory> {
const _dirname = uri.with({ path: dirname(uri.path) });
return this._lookupAsDirectory(_dirname, false);
}
watch(uri: Uri, options: { recursive: boolean; excludes: string[]; }): Disposable {
return new Disposable(noop);
}
stat(uri: Uri): FileStat | Thenable<FileStat> {
return this._lookup(uri, false);
}
readDirectory = reuseable((uri: Uri): [string, FileType][] | Thenable<[string, FileType][]> => {
if (!uri.authority) {
throw FileSystemError.FileNotFound(uri);
}
return this._lookupAsDirectory(uri, false).then(parent => {
if (parent.entries !== null) {
const res = Array.from(parent.entries.values())
.map((item: any) => [item.name, item instanceof Directory ? FileType.Directory : FileType.File]);
return Promise.resolve(res);
}
return readGitHubDirectory(uri).then(data => {
parent.entries = new Map<string, Entry>();
return data.tree.map((item: any) => {
const fileType: FileType = item.type === 'tree' ? FileType.Directory : FileType.File;
parent.entries.set(
item.path, fileType === FileType.Directory
? new Directory(uri, item.path, { sha: item.sha })
: new File(uri, item.path, { sha: item.sha, size: item.size })
);
return [item.path, fileType];
});
});
});
}, (uri: Uri) => uri.toString());
readFile = reuseable((uri: Uri): Uint8Array | Thenable<Uint8Array> => {
if (!uri.authority) {
throw FileSystemError.FileNotFound(uri);
}
return this._lookupAsFile(uri, false).then(file => {
if (file.data !== null) {
return file.data;
}
return readGitHubFile(uri, file.sha).then(blob => {
file.data = decodeBase64(blob.content);
return file.data;
});
});
}, (uri: Uri) => uri.toString());
createDirectory(uri: Uri): void | Thenable<void> {
return Promise.resolve();
}
writeFile(uri: Uri, content: Uint8Array, options: { create: boolean; overwrite: boolean; }): void | Thenable<void> {
return Promise.resolve();
}
delete(uri: Uri, options: { recursive: boolean; }): void | Thenable<void> {
return Promise.resolve();
}
rename(oldUri: Uri, newUri: Uri, options: { overwrite: boolean; }): void | Thenable<void> {
return Promise.resolve();
}
copy?(source: Uri, destination: Uri, options: { overwrite: boolean; }): void | Thenable<void> {
return Promise.resolve();
}
}