(filePath: string)
| 133 | * @returns Parsed content and metadata |
| 134 | */ |
| 135 | export async function parseFile(filePath: string): Promise<FileParseResult> { |
| 136 | try { |
| 137 | if (!filePath) { |
| 138 | throw new Error('No file path provided') |
| 139 | } |
| 140 | |
| 141 | if (!existsSync(filePath)) { |
| 142 | throw new Error(`File not found: ${filePath}`) |
| 143 | } |
| 144 | |
| 145 | const extension = path.extname(filePath).toLowerCase().substring(1) |
| 146 | logger.info('Attempting to parse file with extension:', extension) |
| 147 | |
| 148 | const parsers = getParserInstances() |
| 149 | |
| 150 | if (!Object.keys(parsers).includes(extension)) { |
| 151 | logger.info('No parser found for extension:', extension) |
| 152 | throw new Error( |
| 153 | `Unsupported file type: ${extension}. Supported types are: ${Object.keys(parsers).join(', ')}` |
| 154 | ) |
| 155 | } |
| 156 | |
| 157 | logger.info('Using parser for extension:', extension) |
| 158 | const parser = parsers[extension] |
| 159 | return await parser.parseFile(filePath) |
| 160 | } catch (error) { |
| 161 | logger.error('File parsing error:', error) |
| 162 | throw error |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Parse a buffer based on file extension |
no test coverage detected