Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 56 additions & 2 deletions apps/oxlint/src-js/js_config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
import { getErrorMessage } from "./utils/utils.ts";
import { JSONStringify } from "./utils/globals.ts";

interface JsConfigResult {
path: string;
config: unknown; // Will be validated as Oxlintrc on Rust side
}

type LoadJsConfigsResult =
| { Success: JsConfigResult[] }
| { Failures: { path: string; error: string }[] }
| { Error: string };

/**
* Load JavaScript config files in parallel.
*
Expand All @@ -7,6 +20,47 @@
* @param paths - Array of absolute paths to oxlint.config.ts files
* @returns JSON-stringified result with all configs or error
*/
export async function loadJsConfigs(_paths: string[]): Promise<string> {
throw new Error("Not implemented yet");
export async function loadJsConfigs(paths: string[]): Promise<string> {
try {
const results = await Promise.allSettled(
paths.map(async (path): Promise<JsConfigResult> => {
const fileUrl = new URL(`file://${path}`);
const module = await import(fileUrl.href);
const config = module.default;

if (config === undefined) {
throw new Error(`Configuration file has no default export.`);
}

if (typeof config !== "object") {
throw new Error(`Configuration file must have a default export that is an object.`);
}

return { path, config };
}),
);

const successes: JsConfigResult[] = [];
const errors: { path: string; error: string }[] = [];

for (let i = 0; i < results.length; i++) {
const result = results[i];
if (result.status === "fulfilled") {
successes.push(result.value);
} else {
errors.push({ path: paths[i], error: getErrorMessage(result.reason) });
}
}

// If any config failed to load, report all errors
if (errors.length > 0) {
return JSONStringify({ Failures: errors } satisfies LoadJsConfigsResult);
}

return JSONStringify({ Success: successes } satisfies LoadJsConfigsResult);
} catch (err) {
return JSONStringify({
Error: getErrorMessage(err),
} satisfies LoadJsConfigsResult);
}
}
Loading
Loading