import * as fs from 'fs';
import * as path from 'path';
import { parseFile, isSupported } from './parsers/index.js';
/**
* Parse a file's AST and return its public interface summary.
* For unsupported file types, returns an error message.
*/
export async function filePeek(filePath: string): Promise<string> {
const absolutePath = path.isAbsolute(filePath)
? filePath
: path.resolve(filePath);
const content = fs.readFileSync(absolutePath, 'utf8');
const ext = path.extname(absolutePath).toLowerCase();
if (!isSupported(absolutePath)) {
throw new Error(
`Unsupported file type "${ext}". Use the standard Read tool instead. ` +
`file_peek supports: .rs, .py, .ts, .tsx, .js, .jsx, .php, .cs, .gd`
);
}
const result = parseFile(absolutePath, content);
if (result && result.success) {
return result.formattedSummary;
}
const errorDetail = result && !result.success ? `: ${result.error}` : '';
throw new Error(`Failed to parse ${filePath}${errorDetail}`);
}