保誠-保戶業務員媒合平台
HelenHuang
2022-06-09 9bdb95c9e34cef640534e5e5a1e2225a80442000
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
'use strict';
const {promisify} = require('util');
const fs = require('fs');
 
async function isType(fsStatType, statsMethodName, filePath) {
    if (typeof filePath !== 'string') {
        throw new TypeError(`Expected a string, got ${typeof filePath}`);
    }
 
    try {
        const stats = await promisify(fs[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;
    }
}
 
exports.isFile = isType.bind(null, 'stat', 'isFile');
exports.isDirectory = isType.bind(null, 'stat', 'isDirectory');
exports.isSymlink = isType.bind(null, 'lstat', 'isSymbolicLink');
exports.isFileSync = isTypeSync.bind(null, 'statSync', 'isFile');
exports.isDirectorySync = isTypeSync.bind(null, 'statSync', 'isDirectory');
exports.isSymlinkSync = isTypeSync.bind(null, 'lstatSync', 'isSymbolicLink');