UNPKG

openpkg-cli

Version:

OpenAPI-like specification generator for TypeScript packages

111 lines (107 loc) 3.07 kB
// src/config/openpkg-config.ts import { access } from "node:fs/promises"; import path from "node:path"; import { pathToFileURL } from "node:url"; // src/config/schema.ts import { z } from "zod"; var stringList = z.union([ z.string(), z.array(z.string()) ]); var openPkgConfigSchema = z.object({ include: stringList.optional(), exclude: stringList.optional(), plugins: z.array(z.unknown()).optional() }); var normalizeList = (value) => { if (!value) { return; } const list = Array.isArray(value) ? value : [value]; const normalized = list.map((item) => item.trim()).filter(Boolean); return normalized.length > 0 ? normalized : undefined; }; var normalizeConfig = (input) => { const include = normalizeList(input.include); const exclude = normalizeList(input.exclude); return { include, exclude, plugins: input.plugins }; }; // src/config/openpkg-config.ts var OPENPKG_CONFIG_FILENAMES = [ "openpkg.config.ts", "openpkg.config.mts", "openpkg.config.cts", "openpkg.config.js", "openpkg.config.mjs", "openpkg.config.cjs" ]; var fileExists = async (filePath) => { try { await access(filePath); return true; } catch { return false; } }; var findConfigFile = async (cwd) => { let current = path.resolve(cwd); const { root } = path.parse(current); while (true) { for (const candidate of OPENPKG_CONFIG_FILENAMES) { const candidatePath = path.join(current, candidate); if (await fileExists(candidatePath)) { return candidatePath; } } if (current === root) { return null; } current = path.dirname(current); } }; var importConfigModule = async (absolutePath) => { const fileUrl = pathToFileURL(absolutePath); fileUrl.searchParams.set("t", Date.now().toString()); const module = await import(fileUrl.href); return module?.default ?? module?.config ?? module; }; var formatIssues = (issues) => issues.map((issue) => `- ${issue}`).join(` `); var loadOpenPkgConfigInternal = async (cwd) => { const configPath = await findConfigFile(cwd); if (!configPath) { return null; } let rawConfig; try { rawConfig = await importConfigModule(configPath); } catch (error) { const message = error instanceof Error ? error.message : String(error); throw new Error(`Failed to load OpenPkg config at ${configPath}: ${message}`); } const parsed = openPkgConfigSchema.safeParse(rawConfig); if (!parsed.success) { const issues = parsed.error.issues.map((issue) => { const pathLabel = issue.path.length > 0 ? issue.path.join(".") : "(root)"; return `${pathLabel}: ${issue.message}`; }); throw new Error(`Invalid OpenPkg configuration at ${configPath}. ${formatIssues(issues)}`); } const normalized = normalizeConfig(parsed.data); return { filePath: configPath, ...normalized }; }; // src/config/index.ts var define = (config) => config; export { loadOpenPkgConfigInternal as loadOpenPkgConfig, define as defineConfig }; export { OPENPKG_CONFIG_FILENAMES, loadOpenPkgConfigInternal };