UNPKG

findstate-cli

Version:

A CLI for querying data optimized for Real Estate

30 lines (29 loc) 1.04 kB
import path from "path"; import fs from "fs"; import { getClosestMatch } from "./distance-helpers.js"; export function validateRequiredArray(array, errorMessage) { if (!Array.isArray(array) || array.length === 0) { throw new Error(errorMessage); } } export function validateOptionValues(option, validValues, errorMessage) { if (!validValues.includes(option)) { const closestMatch = getClosestMatch(option, validValues); throw new Error(`${errorMessage} Did you mean '${closestMatch}'? Allowed values: ${validValues.join(", ")}.`); } } export function validateFileName(fileName) { if (!fileName.endsWith(".json")) { throw new Error("The output file must have a .json extension."); } const dirName = path.dirname(fileName); if (!fs.existsSync(dirName)) { throw new Error(`Directory '${dirName}' does not exist.`); } try { fs.accessSync(dirName, fs.constants.W_OK); } catch { throw new Error(`Directory '${dirName}' is not writable.`); } }