Repository navigation
Expand file tree
/
Copy pathfs.ts
More file actions
56 lines (48 loc) · 1.39 KB
/
Copy pathfs.ts
File metadata and controls
56 lines (48 loc) · 1.39 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
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import { default as fs, type OpenMode } from 'node:fs';
interface IInternalDisposableFileHandle extends Disposable {
fd: number;
}
export interface IDisposableFileHandle extends IInternalDisposableFileHandle {
readonly fd: number;
}
export const DISPOSE_SYMBOL: typeof Symbol.dispose = Symbol.dispose ?? Symbol.for('Symbol.dispose');
export function getDisposableFileHandle(path: string, openMode: OpenMode): IDisposableFileHandle {
const result: IInternalDisposableFileHandle = {
fd: fs.openSync(path, openMode),
[DISPOSE_SYMBOL]: () => {
if (!isNaN(result.fd)) {
fs.closeSync(result.fd);
result.fd = NaN;
}
}
};
return result;
}
export function rmdirSync(dirPath: string): boolean {
try {
fs.rmdirSync(dirPath);
return true;
} catch (e) {
if ((e as NodeJS.ErrnoException).code === 'ENOENT' || (e as NodeJS.ErrnoException).code === 'ENOTDIR') {
// Not found, ignore
} else {
throw e;
}
}
return false;
}
export function unlinkSync(filePath: string): boolean {
try {
fs.unlinkSync(filePath);
return true;
} catch (e) {
if (e && (e as NodeJS.ErrnoException).code === 'ENOENT') {
// Not found, ignore
} else {
throw e;
}
}
return false;
}