66 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			66 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
import { parse as JSONC } from "jsr:@std/jsonc";
 | 
						|
 | 
						|
type JsonLike = { [key: string]: string | string[] | JsonLike; };
 | 
						|
 | 
						|
/** A `file://` url version of Deno.cwd() (contains trailing slash) */
 | 
						|
export const Root:string = new URL(`file://${Deno.cwd().replaceAll("\\", "/")}`).toString() + "/";
 | 
						|
export default async function HuntConfig(directory=Root):Promise<{
 | 
						|
    imports: JsonLike;
 | 
						|
    compilerOptions: JsonLike;
 | 
						|
}>
 | 
						|
{
 | 
						|
    let path:string, json:JsonLike|undefined;
 | 
						|
    console.log("searchig in directory", directory)
 | 
						|
    const loadJSON =async(inPath:string)=>{
 | 
						|
        try{
 | 
						|
            const path = new URL(inPath, directory);
 | 
						|
            console.log("looking at", path.href);
 | 
						|
            const resp = await fetch(path);
 | 
						|
            if(inPath.endsWith(".jsonc"))
 | 
						|
            {
 | 
						|
                const text = await resp.text();
 | 
						|
                json = JSONC(text) as JsonLike;
 | 
						|
            } else {
 | 
						|
                json = await resp.json() as JsonLike
 | 
						|
            }
 | 
						|
            return json;
 | 
						|
        } catch (e) {
 | 
						|
            console.error(`Error loading JSON from ${inPath}:`, e.message);
 | 
						|
            return undefined;
 | 
						|
        }
 | 
						|
    };
 | 
						|
 | 
						|
    try {
 | 
						|
        json = await loadJSON("./deno.json");
 | 
						|
    } catch (_e) {
 | 
						|
        try {
 | 
						|
            json = await loadJSON("./deno.jsonc");
 | 
						|
        } catch (_e) {
 | 
						|
            try {
 | 
						|
                json = await loadJSON("./.vscode/settings.json");
 | 
						|
                path = json?.["deno.config"] as string || "";
 | 
						|
                json = undefined;
 | 
						|
                if (path) {
 | 
						|
                    json = await loadJSON(path);
 | 
						|
                }
 | 
						|
            } catch (_e) {
 | 
						|
                console.warn("Cannot find a config in .vscode/ ");
 | 
						|
            }
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    if (!json) {
 | 
						|
        json = {};
 | 
						|
    }
 | 
						|
 | 
						|
    path = json.importMap as string || "";
 | 
						|
    if (!json.imports && path) {
 | 
						|
        json.imports = await loadJSON(path) as JsonLike;
 | 
						|
    }
 | 
						|
 | 
						|
    // Ensure the return type always includes imports and compilerOptions
 | 
						|
    return {
 | 
						|
        imports: json.imports || {},
 | 
						|
        compilerOptions: json.compilerOptions || {}
 | 
						|
    } as { imports: JsonLike, compilerOptions: JsonLike };
 | 
						|
} |