csval
Version:
Check CSV files against a set of validation rules.
25 lines (21 loc) • 701 B
JavaScript
import { readFile } from "node:fs/promises";
/**
* Reads a CSV file from the specified file path and returns its contents as a
* string.
*
* @param {string} filePath - The path to the CSV file
* @returns {Promise<string>} The contents of the CSV file as a string
* @throws {Error} If the file cannot be found or read, with details about the error
*/
export async function readCsv(filePath) {
try {
const data = await readFile(filePath);
return data.toString();
} catch (err) {
const error = /** @type {NodeJS.ErrnoException} */ (err);
if (error.code === "ENOENT") {
throw new Error("Cannot find the specified CSV file.", { cause: err });
}
throw err;
}
}