Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/html-language-features/client/src/node/nodeFs.ts
3323 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import * as fs from 'fs';
7
import { Uri } from 'vscode';
8
import { FileSystemProvider, FileType } from '../requests';
9
10
export function getNodeFileFS(): FileSystemProvider {
11
function ensureFileUri(location: string) {
12
if (!location.startsWith('file:')) {
13
throw new Error('fileRequestService can only handle file URLs');
14
}
15
}
16
return {
17
stat(location: string) {
18
ensureFileUri(location);
19
return new Promise((c, e) => {
20
const uri = Uri.parse(location);
21
fs.stat(uri.fsPath, (err, stats) => {
22
if (err) {
23
if (err.code === 'ENOENT') {
24
return c({ type: FileType.Unknown, ctime: -1, mtime: -1, size: -1 });
25
} else {
26
return e(err);
27
}
28
}
29
30
let type = FileType.Unknown;
31
if (stats.isFile()) {
32
type = FileType.File;
33
} else if (stats.isDirectory()) {
34
type = FileType.Directory;
35
} else if (stats.isSymbolicLink()) {
36
type = FileType.SymbolicLink;
37
}
38
39
c({
40
type,
41
ctime: stats.ctime.getTime(),
42
mtime: stats.mtime.getTime(),
43
size: stats.size
44
});
45
});
46
});
47
},
48
readDirectory(location: string) {
49
ensureFileUri(location);
50
return new Promise((c, e) => {
51
const path = Uri.parse(location).fsPath;
52
53
fs.readdir(path, { withFileTypes: true }, (err, children) => {
54
if (err) {
55
return e(err);
56
}
57
c(children.map(stat => {
58
if (stat.isSymbolicLink()) {
59
return [stat.name, FileType.SymbolicLink];
60
} else if (stat.isDirectory()) {
61
return [stat.name, FileType.Directory];
62
} else if (stat.isFile()) {
63
return [stat.name, FileType.File];
64
} else {
65
return [stat.name, FileType.Unknown];
66
}
67
}));
68
});
69
});
70
}
71
};
72
}
73
74