/
fs.ts
129 lines (118 loc) · 2.74 KB
/
fs.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
import * as vscode from 'vscode';
import { promisify } from 'util';
import * as util from 'util';
import * as fs from 'fs';
export const constants = {
UV_FS_SYMLINK_DIR: 1,
UV_FS_SYMLINK_JUNCTION: 2,
O_RDONLY: 0,
O_WRONLY: 1,
O_RDWR: 2,
UV_DIRENT_UNKNOWN: 0,
UV_DIRENT_FILE: 1,
UV_DIRENT_DIR: 2,
UV_DIRENT_LINK: 3,
UV_DIRENT_FIFO: 4,
UV_DIRENT_SOCKET: 5,
UV_DIRENT_CHAR: 6,
UV_DIRENT_BLOCK: 7,
S_IFMT: 61440,
S_IFREG: 32768,
S_IFDIR: 16384,
S_IFCHR: 8192,
S_IFBLK: 24576,
S_IFIFO: 4096,
S_IFLNK: 40960,
S_IFSOCK: 49152,
O_CREAT: 512,
O_EXCL: 2048,
UV_FS_O_FILEMAP: 0,
O_NOCTTY: 131072,
O_TRUNC: 1024,
O_APPEND: 8,
O_DIRECTORY: 1048576,
O_NOFOLLOW: 256,
O_SYNC: 128,
O_DSYNC: 4194304,
O_SYMLINK: 2097152,
O_NONBLOCK: 4,
S_IRWXU: 448,
S_IRUSR: 256,
S_IWUSR: 128,
S_IXUSR: 64,
S_IRWXG: 56,
S_IRGRP: 32,
S_IWGRP: 16,
S_IXGRP: 8,
S_IRWXO: 7,
S_IROTH: 4,
S_IWOTH: 2,
S_IXOTH: 1,
F_OK: 0,
R_OK: 4,
W_OK: 2,
X_OK: 1,
UV_FS_COPYFILE_EXCL: 1,
COPYFILE_EXCL: 1,
UV_FS_COPYFILE_FICLONE: 2,
COPYFILE_FICLONE: 2,
UV_FS_COPYFILE_FICLONE_FORCE: 4,
COPYFILE_FICLONE_FORCE: 4,
};
export async function doesFileExist(fileUri: vscode.Uri) {
const activeTextEditor = vscode.window.activeTextEditor;
if (activeTextEditor) {
try {
await vscode.workspace.fs.stat(fileUri);
return true;
} catch {
return false;
}
} else {
// fallback to local fs
const fsExists = util.promisify(fs.exists);
return fsExists(fileUri.fsPath);
}
}
export async function existsAsync(path: string): Promise<boolean> {
try {
await vscode.workspace.fs.stat(vscode.Uri.file(path));
return true;
} catch (_e) {
return false;
}
}
export async function unlink(path: string): Promise<void> {
fs.unlinkSync(path);
}
export async function readFileAsync(path: string, encoding: BufferEncoding): Promise<string> {
return promisify(fs.readFile)(path, encoding);
}
export async function mkdirAsync(path: string, options: any): Promise<void> {
await promisify(fs.mkdir)(path, options);
}
export async function writeFileAsync(
path: string,
content: string,
encoding: BufferEncoding
): Promise<void> {
await promisify(fs.writeFile)(path, content, encoding);
}
export async function accessAsync(path: string, mode: number) {
return promisify(fs.access)(path, mode);
}
export async function chmodAsync(path: string, mode: string | number) {
return promisify(fs.chmod)(path, mode);
}
export async function getMode(path: string): Promise<number> {
const pathMode = await fs.promises
.stat(path)
.then((stat) => stat.mode)
.catch((e) => {
throw e;
});
return pathMode;
}
export function unlinkSync(path: string) {
fs.unlinkSync(path);
}