mirror of
https://github.com/Sevichecc/Urara-Blog.git
synced 2025-05-02 23:09:30 +08:00
43 lines
1.1 KiB
Text
43 lines
1.1 KiB
Text
'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');
|