|
| 1 | +import { mkdir, readFile, readdir, stat, writeFile } from "node:fs/promises"; |
| 2 | +import path from "node:path"; |
| 3 | + |
| 4 | +export async function readTextFile(filePath: string): Promise<string> { |
| 5 | + if (filePath.startsWith("https://")) { |
| 6 | + const response = await fetch(filePath, { |
| 7 | + headers: { |
| 8 | + "User-Agent": "typesense-api-extractor", |
| 9 | + Accept: "text/plain", |
| 10 | + }, |
| 11 | + }); |
| 12 | + if (!response.ok) { |
| 13 | + throw new Error( |
| 14 | + `Could not read remote file ${filePath}: ${response.status} ${response.statusText}`, |
| 15 | + ); |
| 16 | + } |
| 17 | + return response.text(); |
| 18 | + } |
| 19 | + |
| 20 | + return readFile(filePath, "utf8"); |
| 21 | +} |
| 22 | + |
| 23 | +export async function writeTextFile( |
| 24 | + filePath: string, |
| 25 | + contents: string, |
| 26 | +): Promise<void> { |
| 27 | + await mkdir(path.dirname(filePath), { recursive: true }); |
| 28 | + await writeFile(filePath, contents, "utf8"); |
| 29 | +} |
| 30 | + |
| 31 | +export async function listFilesRecursive( |
| 32 | + rootPath: string, |
| 33 | +): Promise<readonly string[]> { |
| 34 | + const entries = await readdir(rootPath, { withFileTypes: true }); |
| 35 | + const nestedFiles = await Promise.all( |
| 36 | + entries.map(async (entry) => { |
| 37 | + const fullPath = path.join(rootPath, entry.name); |
| 38 | + if (entry.isDirectory()) { |
| 39 | + return listFilesRecursive(fullPath); |
| 40 | + } |
| 41 | + return entry.isFile() ? [fullPath] : []; |
| 42 | + }), |
| 43 | + ); |
| 44 | + |
| 45 | + return nestedFiles.flat(); |
| 46 | +} |
| 47 | + |
| 48 | +export async function fileExists(filePath: string): Promise<boolean> { |
| 49 | + try { |
| 50 | + const fileStat = await stat(filePath); |
| 51 | + return fileStat.isFile() ?? fileStat.isDirectory(); |
| 52 | + } catch { |
| 53 | + return false; |
| 54 | + } |
| 55 | +} |
0 commit comments