UNPKG

@lingui/cli

Version:

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

62 lines (61 loc) 2.38 kB
import { styleText } from "node:util"; import { program } from "commander"; import { getConfig } from "@lingui/conf"; import { getCatalogs } from "./api/index.js"; import nodepath from "path"; import normalizePath from "normalize-path"; import { createExtractWorkerPool, } from "./api/workerPools.js"; import { resolveWorkersOptions, } from "./api/resolveWorkersOptions.js"; export default async function command(config, options) { options.verbose && console.log("Extracting messages from source files…"); const catalogs = await getCatalogs(config); const catalogStats = {}; let commandSuccess = true; let workerPool; if (options.workersOptions.poolSize) { options.verbose && console.log(`Use worker pool of size ${options.workersOptions.poolSize}`); workerPool = createExtractWorkerPool(options.workersOptions); } try { await Promise.all(catalogs.map(async (catalog) => { const result = await catalog.makeTemplate({ ...options, orderBy: config.orderBy, workerPool, }); if (result) { catalogStats[normalizePath(nodepath.relative(config.rootDir, catalog.templateFile))] = Object.keys(result).length; } commandSuccess &&= Boolean(result); })); } finally { if (workerPool) { await workerPool.destroy(); } } Object.entries(catalogStats).forEach(([key, value]) => { console.log(`Catalog statistics for ${styleText("bold", key)}: ${styleText("green", String(value))} messages`); console.log(); }); return commandSuccess; } if (import.meta.main) { program .option("--config <path>", "Path to the config file") .option("--verbose", "Verbose output") .option("--workers <n>", "Number of worker threads to use (default: CPU count - 1, capped at 8). Pass `--workers 1` to disable worker threads and run everything in a single process") .parse(process.argv); const options = program.opts(); const config = getConfig({ configPath: options.config, }); const result = command(config, { verbose: options.verbose || false, workersOptions: resolveWorkersOptions(options), }).then(() => { if (!result) process.exit(1); }); }