29 lines
686 B
TypeScript

import fs from 'node:fs';
export const fileIsExist = (filePath: string) => {
try {
// 检查文件或者目录是否存在
fs.accessSync(filePath, fs.constants.F_OK);
return true;
} catch (error) {
return false;
}
};
export const pathExists = (path: string, type?: 'file' | 'directory') => {
try {
// 检查路径是否存在
fs.accessSync(path, fs.constants.F_OK);
// 如果需要检查类型
if (type) {
const stats = fs.statSync(path);
if (type === 'file' && !stats.isFile()) return false;
if (type === 'directory' && !stats.isDirectory()) return false;
}
return true;
} catch (error) {
return false;
}
};