repomix
Version:
A tool to pack repository contents to single file for AI consumption
43 lines (42 loc) • 1.34 kB
JavaScript
import fs from 'node:fs/promises';
import { createRequire } from 'node:module';
import path from 'node:path';
import { Language } from 'web-tree-sitter';
const require = createRequire(import.meta.url);
let customWasmBasePath = null;
export function setWasmBasePath(basePath) {
customWasmBasePath = basePath;
}
function getWasmBasePath() {
return customWasmBasePath ?? process.env.REPOMIX_WASM_DIR ?? null;
}
export async function loadLanguage(langName) {
if (!langName) {
throw new Error('Invalid language name');
}
try {
const wasmPath = await getWasmPath(langName);
return await Language.load(wasmPath);
}
catch (error) {
const message = error instanceof Error ? error.message : String(error);
throw new Error(`Failed to load language ${langName}: ${message}`);
}
}
async function getWasmPath(langName) {
const wasmBasePath = getWasmBasePath();
let wasmPath;
if (wasmBasePath) {
wasmPath = path.join(wasmBasePath, `tree-sitter-${langName}.wasm`);
}
else {
wasmPath = require.resolve(`@repomix/tree-sitter-wasms/out/tree-sitter-${langName}.wasm`);
}
try {
await fs.access(wasmPath);
return wasmPath;
}
catch {
throw new Error(`WASM file not found for language ${langName}: ${wasmPath}`);
}
}