From b5136c5d02bfa4fd5597811589ce77f5c39b34c3 Mon Sep 17 00:00:00 2001 From: Endeavorance Date: Thu, 3 Apr 2025 09:36:56 -0400 Subject: [PATCH] Clean up exports --- src/binding.ts | 44 +++++++++++++++----------------------------- src/errors.ts | 6 +++--- src/exports.ts | 1 + src/types.ts | 37 +++++++++++++++++++++---------------- 4 files changed, 40 insertions(+), 48 deletions(-) diff --git a/src/binding.ts b/src/binding.ts index 9499757..c1f9e96 100644 --- a/src/binding.ts +++ b/src/binding.ts @@ -1,6 +1,6 @@ import path, { dirname } from "node:path"; import { Glob } from "bun"; -import { FileNotFoundError, MuseError } from "./errors"; +import { MuseFileNotFoundError, MuseError } from "./errors"; import { parseMuseFile } from "./parse"; import { type Binding, @@ -33,6 +33,7 @@ async function loadProcessor( description: processorDescription, }; } + /** * Given a path, find the binding file * If the path is to a directory, check for a binding inside @@ -42,7 +43,7 @@ async function loadProcessor( * @param bindingPath - The path to the binding file * @returns The absolute resolved path to the binding file */ -export async function resolveBindingPath(bindingPath: string): Promise { +async function resolveBindingPath(bindingPath: string): Promise { // If the path does not specify a filename, use binding.yaml const inputLocation = Bun.file(bindingPath); @@ -77,13 +78,13 @@ export async function resolveBindingPath(bindingPath: string): Promise { return mdPath; } - throw new FileNotFoundError(bindingPath); + throw new MuseFileNotFoundError(bindingPath); } const exists = await inputLocation.exists(); if (!exists) { - throw new FileNotFoundError(bindingPath); + throw new MuseFileNotFoundError(bindingPath); } // If it is a file, return the path @@ -103,21 +104,8 @@ export async function loadBinding(initialPath: string): Promise { // Load and parse the Binding YAML content const parsedBinding = BindingSchema.parse(loadedBindingData[0].data); - // Prepare collections to load into - const extendsFrom: Binding[] = []; - const entries: MuseEntry[] = []; - - // Process imported bindings and bring in their entries - for (const includePath of parsedBinding.include) { - const pathFromHere = path.resolve(bindingDirname, includePath); - const extendBindingPath = await resolveBindingPath(pathFromHere); - const loadedBinding = await loadBinding(extendBindingPath); - extendsFrom.push(loadedBinding); - entries.push(...loadedBinding.entries); - } - // Aggregate file paths to import - const includedFilePaths = parsedBinding.files + const includedFilePaths = parsedBinding.sources .flatMap((thisGlob) => { return Array.from( new Glob(thisGlob).scanSync({ @@ -131,16 +119,15 @@ export async function loadBinding(initialPath: string): Promise { .filter((filePath) => filePath !== bindingPath); // Load files - const loadedEntries = await Promise.all( - includedFilePaths.map((filePath) => { - return parseMuseFile(filePath, { - contentKey: parsedBinding.contentKey, - }); - }), - ); - - // Add loaded entries to main list - entries.push(...loadedEntries.flat()); + const entries: MuseEntry[] = ( + await Promise.all( + includedFilePaths.map((filePath) => { + return parseMuseFile(filePath, { + contentKey: parsedBinding.contentKey, + }); + }), + ) + ).flat(); // Load and check processors const processors: MuseProcessor[] = await Promise.all( @@ -154,7 +141,6 @@ export async function loadBinding(initialPath: string): Promise { entries, options: parsedBinding.options, processors: processors, - imports: extendsFrom, meta: {}, }; } diff --git a/src/errors.ts b/src/errors.ts index 7c8c014..d5184d7 100644 --- a/src/errors.ts +++ b/src/errors.ts @@ -4,7 +4,7 @@ export class MuseError extends Error { } } -export class FileError extends MuseError { +export class MuseFileError extends MuseError { filePath: string; details?: string; @@ -19,7 +19,7 @@ export class FileError extends MuseError { } } -export class FileNotFoundError extends FileError { +export class MuseFileNotFoundError extends MuseFileError { constructor(filePath: string) { super("File not found", filePath); this.message = "File not found"; @@ -27,4 +27,4 @@ export class FileNotFoundError extends FileError { } } -export class CLIError extends MuseError {} +export class CLIError extends MuseError { } diff --git a/src/exports.ts b/src/exports.ts index 8a07b08..a05b13f 100644 --- a/src/exports.ts +++ b/src/exports.ts @@ -1,2 +1,3 @@ export type * from "./types"; +export * from "./errors"; export const DEFAULT_CONTENT_KEY = "content"; diff --git a/src/types.ts b/src/types.ts index f9faa6c..7aabb16 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,5 +1,7 @@ import { z } from "zod"; +export type UnknownRecord = Record; + export interface CLIFlags { help: boolean; verbose: boolean; @@ -12,32 +14,27 @@ export interface CLIArguments { } export const BindingSchema = z.object({ - include: z.array(z.string()).default([]), contentKey: z.string().default("content"), - files: z + sources: z .array(z.string()) .default(["**/*.yaml", "**/*.md", "**/*.toml", "**/*.json"]), options: z.record(z.string(), z.any()).default({}), processors: z.array(z.string()).default([]), + renderer: z.string().optional(), }); -export interface Binding { - readonly _raw: z.infer; - readonly bindingPath: string; - readonly contentKey: string; - readonly entries: MuseEntry[]; - readonly meta: Record; - readonly options: Record; - readonly processors: MuseProcessor[]; - readonly imports: Binding[]; -} - export type MuseProcessorFn = ( binding: Binding, opts: Record, flags: CLIFlags, ) => Promise; +export type MuseRenderFn = ( + binding: Binding, + opts: UnknownRecord, + flags: CLIFlags, +) => Promise; + export interface MuseProcessor { readonly filePath: string; readonly name: string; @@ -45,11 +42,19 @@ export interface MuseProcessor { readonly process: MuseProcessorFn; } -export interface MuseEntry { +export interface MuseEntry { _raw: string; filePath: string; data: Record; - meta: Record; + meta: MetaShape; } -export type UnknownRecord = Record; +export interface Binding { + readonly _raw: z.infer; + readonly bindingPath: string; + readonly contentKey: string; + readonly entries: MuseEntry[]; + readonly processors: MuseProcessor[]; + readonly meta: MetaShape; + readonly options: UnknownRecord; +}