forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathfakeVSCFileSystemAPI.ts
More file actions
109 lines (98 loc) · 3.95 KB
/
fakeVSCFileSystemAPI.ts
File metadata and controls
109 lines (98 loc) · 3.95 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import * as path from 'path';
import { FileStat, FileType, Uri } from 'vscode';
import * as fsextra from '../client/common/platform/fs-paths';
import { convertStat } from '../client/common/platform/fileSystem';
import { createDeferred } from '../client/common/utils/async';
/* eslint-disable class-methods-use-this */
// This is necessary for unit tests and functional tests, since they
// do not run under VS Code so they do not have access to the actual
// "vscode" namespace.
export class FakeVSCodeFileSystemAPI {
public async readFile(uri: Uri): Promise<Uint8Array> {
return fsextra.readFile(uri.fsPath);
}
public async writeFile(uri: Uri, content: Uint8Array): Promise<void> {
return fsextra.writeFile(uri.fsPath, Buffer.from(content));
}
public async delete(uri: Uri): Promise<void> {
return (
fsextra
// Make sure the file exists before deleting.
.stat(uri.fsPath)
.then(() => fsextra.remove(uri.fsPath))
);
}
public async stat(uri: Uri): Promise<FileStat> {
const filename = uri.fsPath;
let filetype = FileType.Unknown;
let stat = await fsextra.lstat(filename);
if (stat.isSymbolicLink()) {
filetype = FileType.SymbolicLink;
stat = await fsextra.stat(filename);
}
if (stat.isFile()) {
filetype |= FileType.File;
} else if (stat.isDirectory()) {
filetype |= FileType.Directory;
}
return convertStat(stat, filetype);
}
public async readDirectory(uri: Uri): Promise<[string, FileType][]> {
const names: string[] = await fsextra.readdir(uri.fsPath);
const promises = names.map((name) => {
const filename = path.join(uri.fsPath, name);
return (
fsextra
// Get the lstat info and deal with symlinks if necessary.
.lstat(filename)
.then(async (stat) => {
let filetype = FileType.Unknown;
if (stat.isFile()) {
filetype = FileType.File;
} else if (stat.isDirectory()) {
filetype = FileType.Directory;
} else if (stat.isSymbolicLink()) {
filetype = FileType.SymbolicLink;
stat = await fsextra.stat(filename);
if (stat.isFile()) {
filetype |= FileType.File;
} else if (stat.isDirectory()) {
filetype |= FileType.Directory;
}
}
return [name, filetype] as [string, FileType];
})
.catch(() => [name, FileType.Unknown] as [string, FileType])
);
});
return Promise.all(promises);
}
public async createDirectory(uri: Uri): Promise<void> {
return fsextra.mkdirp(uri.fsPath);
}
public async copy(src: Uri, dest: Uri): Promise<void> {
const deferred = createDeferred<void>();
const rs = fsextra
// Set an error handler on the stream.
.createReadStream(src.fsPath)
.on('error', (err) => {
deferred.reject(err);
});
const ws = fsextra
.createWriteStream(dest.fsPath)
// Set an error & close handler on the stream.
.on('error', (err) => {
deferred.reject(err);
})
.on('close', () => {
deferred.resolve();
});
rs.pipe(ws);
return deferred.promise;
}
public async rename(src: Uri, dest: Uri): Promise<void> {
return fsextra.rename(src.fsPath, dest.fsPath);
}
}