UNPKG

@lingui/cli

Version:

Lingui CLI to extract messages, compile catalogs, and manage translation workflows

41 lines (40 loc) 1.42 kB
import fs from "fs/promises"; import { createBabelExtractor } from "./babel.js"; let defaultExtractor; function createDefaultExtractor(linguiConfig) { if (!defaultExtractor) { defaultExtractor = createBabelExtractor({ parserOptions: linguiConfig.extractorParserOptions, }); } return defaultExtractor; } export function isBatchExtractor(ext) { return "extractFromFiles" in ext && typeof ext.extractFromFiles === "function"; } export function isPerFileExtractor(ext) { return "extract" in ext && typeof ext.extract === "function"; } export const getConfiguredExtractors = (linguiConfig) => { return linguiConfig.extractors ?? [createDefaultExtractor(linguiConfig)]; }; export default async function extract(filename, onMessageExtracted, linguiConfig) { const extractorsToExtract = getConfiguredExtractors(linguiConfig).filter(isPerFileExtractor); for (const ext of extractorsToExtract) { if (!ext.match(filename)) continue; try { const file = await fs.readFile(filename); await ext.extract(filename, file.toString(), onMessageExtracted, { linguiConfig, }); return true; } catch (e) { console.error(`Cannot process file ${filename} ${e.message}`); console.error(e.stack); return false; } } return true; }