代码拉取完成,页面将自动刷新
import fs from 'fs/promises';
import normalize from '@/normalize';
/**
* 检查单个路径是否存在 (check if a single path exists)
* @param {string} path 要检查的路径 (the path to check)
* @returns {Promise<boolean>} 路径存在则返回 `true`,否则返回 `false` (returns `true` if the path exists, otherwise returns `false`)。
*/
const checkPathExists = async (path: string): Promise<boolean> => {
try {
await fs.access(path, fs.constants.F_OK);
return true;
} catch (err) {
return false;
}
};
/**
* 检查文件或目录路径是否存在 (check if a file or folder path exists)
* @param {string | string[]} paths 要检查的路径,可以是单个路径字符串或路径字符串数组 (the path to check, can be a single path string or path string array)
* @param {boolean} [anyExists=false] 是否只要有一个路径存在就返回 `true` (whether to return `true` if only one path exists)
* @returns {Promise<boolean>} 路径是否存在 (whether the path exists)
*/
const exists = async (paths: string | string[], anyExists: boolean = false): Promise<boolean> => {
const normalizedPaths = Array.isArray(paths) ? paths.map((p) => normalize(p)) : [normalize(paths)];
const results = await Promise.all(normalizedPaths.map(checkPathExists));
return anyExists ? results.some((result) => result) : results.every((result) => result);
};
export default exists;
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。