|
| 1 | +import type { Json } from '@metamask/utils'; |
| 2 | +import fs from 'fs'; |
| 3 | +import path from 'path'; |
| 4 | + |
| 5 | +import { isErrorWithCode, wrapError } from './error-utils'; |
| 6 | + |
| 7 | +/** |
| 8 | + * Reads the file at the given path, assuming its content is encoded as UTF-8. |
| 9 | + * |
| 10 | + * @param filePath - The path to the file. |
| 11 | + * @returns The content of the file. |
| 12 | + * @throws An error with a stack trace if reading fails in any way. |
| 13 | + */ |
| 14 | +export async function readFile(filePath: string): Promise<string> { |
| 15 | + try { |
| 16 | + return await fs.promises.readFile(filePath, 'utf8'); |
| 17 | + } catch (error) { |
| 18 | + throw wrapError(`Could not read file '${filePath}'`, error); |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +/** |
| 23 | + * Writes content to the file at the given path. |
| 24 | + * |
| 25 | + * @param filePath - The path to the file. |
| 26 | + * @param content - The new content of the file. |
| 27 | + * @throws An error with a stack trace if writing fails in any way. |
| 28 | + */ |
| 29 | +export async function writeFile( |
| 30 | + filePath: string, |
| 31 | + content: string, |
| 32 | +): Promise<void> { |
| 33 | + try { |
| 34 | + await fs.promises.mkdir(path.dirname(filePath), { recursive: true }); |
| 35 | + await fs.promises.writeFile(filePath, content); |
| 36 | + } catch (error) { |
| 37 | + throw wrapError(`Could not write file '${filePath}'`, error); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * Reads the assumed JSON file at the given path, attempts to parse it, and |
| 43 | + * returns the resulting object. |
| 44 | + * |
| 45 | + * @param filePath - The path segments pointing to the JSON file. Will be passed |
| 46 | + * to path.join(). |
| 47 | + * @returns The object corresponding to the parsed JSON file, typed against the |
| 48 | + * struct. |
| 49 | + * @throws An error with a stack trace if reading fails in any way, or if the |
| 50 | + * parsed value is not a plain object. |
| 51 | + */ |
| 52 | +export async function readJsonFile<Value extends Json>( |
| 53 | + filePath: string, |
| 54 | +): Promise<Value> { |
| 55 | + try { |
| 56 | + const content = await readFile(filePath); |
| 57 | + return JSON.parse(content); |
| 58 | + } catch (error) { |
| 59 | + throw wrapError(`Could not read JSON file '${filePath}'`, error); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * Attempts to write the given JSON-like value to the file at the given path. |
| 65 | + * Adds a newline to the end of the file. |
| 66 | + * |
| 67 | + * @param filePath - The path to write the JSON file to, including the file |
| 68 | + * itself. |
| 69 | + * @param jsonValue - The JSON-like value to write to the file. Make sure that |
| 70 | + * JSON.stringify can handle it. |
| 71 | + * @throws An error with a stack trace if writing fails in any way. |
| 72 | + */ |
| 73 | +export async function writeJsonFile( |
| 74 | + filePath: string, |
| 75 | + jsonValue: Json, |
| 76 | +): Promise<void> { |
| 77 | + try { |
| 78 | + await writeFile(filePath, JSON.stringify(jsonValue, null, ' ')); |
| 79 | + } catch (error) { |
| 80 | + throw wrapError(`Could not write JSON file '${filePath}'`, error); |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +/** |
| 85 | + * Determines whether the given path refers to a file. |
| 86 | + * |
| 87 | + * @param entryPath - The path. |
| 88 | + * @returns The boolean result. |
| 89 | + */ |
| 90 | +export async function isFile(entryPath: string): Promise<boolean> { |
| 91 | + try { |
| 92 | + const stats = await fs.promises.stat(entryPath); |
| 93 | + return stats.isFile(); |
| 94 | + } catch (error) { |
| 95 | + if (isErrorWithCode(error) && error.code === 'ENOENT') { |
| 96 | + return false; |
| 97 | + } |
| 98 | + throw error; |
| 99 | + } |
| 100 | +} |
0 commit comments