stack/packages/stack-cli/src/lib/config-file-path.ts
2026-05-15 15:54:54 -07:00

29 lines
827 B
TypeScript

import { existsSync, statSync } from "fs";
import { resolve } from "path";
import { CliError } from "./errors.js";
export function resolveConfigFilePathOption(inputPath: string, options?: {
mustExist?: boolean,
optionName?: string,
}): string {
const resolved = resolve(inputPath);
const optionName = options?.optionName ?? "--config-file";
if (!existsSync(resolved)) {
if (options?.mustExist === true) {
throw new CliError(`Config file not found: ${resolved}`);
}
return resolved;
}
const stat = statSync(resolved);
if (stat.isDirectory()) {
throw new CliError(`${optionName} must point to a config file, but got a directory: ${resolved}`);
}
if (!stat.isFile()) {
throw new CliError(`${optionName} must point to a regular config file: ${resolved}`);
}
return resolved;
}