UNPKG

@tsed/cli-core

Version:
40 lines (39 loc) 1.31 kB
import { extname } from "node:path"; import { inject, injectable } from "@tsed/di"; import { validate } from "../utils/validate.js"; import { CliFs } from "./CliFs.js"; import { CliYaml } from "./CliYaml.js"; export class CliLoadFile { constructor() { this.cliYaml = inject(CliYaml); this.cliFs = inject(CliFs); } /** * Load a configuration file from yaml, json */ async loadFile(path, schema) { let config; const ext = extname(path); if ([".yml", ".yaml"].includes(ext)) { config = await this.cliYaml.read(path); } else if ([".json"].includes(ext)) { config = await JSON.parse(await this.cliFs.readFile(path, "utf8")); } else if (!config) { throw new Error("Unsupported format file"); } if (schema) { const { isValid, errors, value } = validate(config, schema); if (!isValid) { const [error] = errors; throw new Error([`${error.path.replace(/\//gi, ".")} `, error.message, error.expected && `. Allowed values: ${error.expected}`] .filter(Boolean) .join("")); } return value; } return config; } } injectable(CliLoadFile);