Skip to content

Commit 99f979e

Browse files
Copilotdmichon-msft
andcommitted
Complete Text refactoring, document FileSystem complexity
Co-authored-by: dmichon-msft <26827560+dmichon-msft@users.noreply.github.com>
1 parent 4cd25ab commit 99f979e

1 file changed

Lines changed: 74 additions & 0 deletions

File tree

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
2+
// See LICENSE in the project root for license information.
3+
4+
/**
5+
* Returns true if the error object indicates the file or folder already exists (`EEXIST`).
6+
* @public
7+
*/
8+
export function isExistError(error: Error): boolean {
9+
return isErrnoException(error) && error.code === 'EEXIST';
10+
}
11+
12+
/**
13+
* Returns true if the error object indicates the file or folder does not exist (`ENOENT` or `ENOTDIR`)
14+
* @public
15+
*/
16+
export function isNotExistError(error: Error): boolean {
17+
return isFileDoesNotExistError(error) || isFolderDoesNotExistError(error);
18+
}
19+
20+
/**
21+
* Returns true if the error object indicates the file does not exist (`ENOENT`).
22+
* @public
23+
*/
24+
export function isFileDoesNotExistError(error: Error): boolean {
25+
return isErrnoException(error) && error.code === 'ENOENT';
26+
}
27+
28+
/**
29+
* Returns true if the error object indicates the folder does not exist (`ENOTDIR`).
30+
* @public
31+
*/
32+
export function isFolderDoesNotExistError(error: Error): boolean {
33+
return isErrnoException(error) && error.code === 'ENOTDIR';
34+
}
35+
36+
/**
37+
* Returns true if the error object indicates the target is a directory (`EISDIR`).
38+
* @public
39+
*/
40+
export function isDirectoryError(error: Error): boolean {
41+
return isErrnoException(error) && error.code === 'EISDIR';
42+
}
43+
44+
/**
45+
* Returns true if the error object indicates the target is not a directory (`ENOTDIR`).
46+
* @public
47+
*/
48+
export function isNotDirectoryError(error: Error): boolean {
49+
return isErrnoException(error) && error.code === 'ENOTDIR';
50+
}
51+
52+
/**
53+
* Returns true if the error object indicates that the `unlink` system call failed
54+
* due to a permissions issue (`EPERM`).
55+
* @public
56+
*/
57+
export function isUnlinkNotPermittedError(error: Error): boolean {
58+
return isErrnoException(error) && error.code === 'EPERM' && error.syscall === 'unlink';
59+
}
60+
61+
/**
62+
* Detects if the provided error object is a `NodeJS.ErrnoException`
63+
* @public
64+
*/
65+
export function isErrnoException(error: Error): error is NodeJS.ErrnoException {
66+
const typedError: NodeJS.ErrnoException = error;
67+
// Don't check for `path` because the syscall may not have a path.
68+
// For example, when invoked with a file descriptor.
69+
return (
70+
typeof typedError.code === 'string' &&
71+
typeof typedError.errno === 'number' &&
72+
typeof typedError.syscall === 'string'
73+
);
74+
}

0 commit comments

Comments
 (0)