UNPKG

@ts-for-gir/lib

Version:

Typescript .d.ts generator from GIR for gjs

62 lines 1.71 kB
import { join } from 'path'; import { constants } from 'fs'; import { readFile, access } from 'fs/promises'; import { glob } from 'glob'; export { inspect } from 'util'; /** * Asynchronously checks if a file exists * @param filePath The path to the file * @returns A promise that resolves to true if the file exists, false otherwise */ export const fileExists = async (filePath) => { try { await access(filePath, constants.F_OK); return true; } catch { return false; } }; /** * Find a file in a list of directories * @param dirs The directories to search in * @param filename The filename to search for * @returns The file info */ export const findFilesInDirs = async (dirs, filename) => { const filesInfo = []; const pattern = dirs.map((dir) => join(dir, filename)); const _files = await glob(pattern); // Remove duplicates const files = [...new Set(_files)]; for (const filePath of files) { const fileInfo = { path: null, filename, exists: false, }; fileInfo.exists = await fileExists(filePath); if (fileInfo.exists) { fileInfo.path = filePath; filesInfo.push(fileInfo); } } if (filesInfo.length === 0) { filesInfo.push({ path: null, filename, exists: false, }); } return filesInfo; }; /** * Read a JSON file * @param filePath The path to the JSON file * @returns The parsed JSON */ export const readJsonFile = async (filePath) => { const fileContent = await readFile(filePath, 'utf8'); return JSON.parse(fileContent); }; //# sourceMappingURL=files.js.map