-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathindex.js
42 lines (35 loc) · 1.2 KB
/
index.js
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
import fs from 'node:fs';
import fsPromises from 'node:fs/promises';
async function isType(fsStatType, statsMethodName, filePath) {
if (typeof filePath !== 'string') {
throw new TypeError(`Expected a string, got ${typeof filePath}`);
}
try {
const stats = await fsPromises[fsStatType](filePath);
return stats[statsMethodName]();
} catch (error) {
if (error.code === 'ENOENT') {
return false;
}
throw error;
}
}
function isTypeSync(fsStatType, statsMethodName, filePath) {
if (typeof filePath !== 'string') {
throw new TypeError(`Expected a string, got ${typeof filePath}`);
}
try {
return fs[fsStatType](filePath)[statsMethodName]();
} catch (error) {
if (error.code === 'ENOENT') {
return false;
}
throw error;
}
}
export const isFile = isType.bind(undefined, 'stat', 'isFile');
export const isDirectory = isType.bind(undefined, 'stat', 'isDirectory');
export const isSymlink = isType.bind(undefined, 'lstat', 'isSymbolicLink');
export const isFileSync = isTypeSync.bind(undefined, 'statSync', 'isFile');
export const isDirectorySync = isTypeSync.bind(undefined, 'statSync', 'isDirectory');
export const isSymlinkSync = isTypeSync.bind(undefined, 'lstatSync', 'isSymbolicLink');