import { styleText } from "node:util" import { watch } from "chokidar" import { program } from "commander" import { getConfig, LinguiConfigNormalized } from "@lingui/conf" import { helpRun } from "./api/help.js" import { getCatalogs } from "./api/index.js" import { compileLocale } from "./api/compile/compileLocale.js" import { createCompileWorkerPool } from "./api/workerPools.js" import { resolveWorkersOptions, WorkersOptions, } from "./api/resolveWorkersOptions.js" import ms from "ms" import { getPathsForCompileWatcher } from "./api/getPathsForCompileWatcher.js" import { ProgramExit } from "./api/ProgramExit.js" import { createWatchDispatcher } from "./api/createWatchDispatcher.js" export type CliCompileOptions = { verbose?: boolean allowEmpty?: boolean failOnCompileError?: boolean typescript?: boolean watch?: boolean namespace?: string workersOptions: WorkersOptions outputPrefix?: string } export async function command( config: LinguiConfigNormalized, options: CliCompileOptions, ) { const startTime = Date.now() // Check config.compile.merge if catalogs for current locale are to be merged into a single compiled file const doMerge = !!config.catalogsMergePath console.log("Compiling message catalogs…") let errored = false if (!options.workersOptions.poolSize) { // single threaded const catalogs = await getCatalogs(config) for (const locale of config.locales) { try { await compileLocale(catalogs, locale, options, config, doMerge, console) } catch (err) { if (err instanceof ProgramExit) { errored = true } else { throw err } } } } else { const resolvedConfigPath = config.resolvedConfigPath if (!resolvedConfigPath) { throw new Error( "Multithreading is only supported when lingui config loaded from file system, not passed by API", ) } options.verbose && console.log(`Use worker pool of size ${options.workersOptions.poolSize}`) const pool = createCompileWorkerPool({ poolSize: options.workersOptions.poolSize, }) try { await Promise.all( config.locales.map(async (locale) => { const { logs, error, exitProgram } = await pool.run( locale, options, doMerge, resolvedConfigPath, ) if (logs.errors) { console.error(logs.errors) } if (exitProgram) { errored = true return } if (error) { throw error } }), ) } finally { await pool.destroy() } } console.log(`Done in ${ms(Date.now() - startTime)}`) return !errored } type CliArgs = { verbose?: boolean allowEmpty?: boolean typescript?: boolean watch?: boolean namespace?: string strict?: boolean config?: string debounce?: number workers?: number outputPrefix?: string } if (import.meta.main) { program .description("Compile message catalogs to compiled bundle.") .option("--config ", "Path to the config file") .option( "--strict", "Fail if translations are missing after applying fallbackLocales, or if compilation errors occur", ) .option("--verbose", "Verbose output") .option("--typescript", "Create Typescript definition for compiled bundle") .option( "--workers ", "Number of worker threads to use (default: CPU count - 1, capped at 8; on 1-2 core machines, all cores). Pass `--workers 1` to disable worker threads and run everything in a single process", ) .option( "--namespace ", "Specify namespace for compiled bundle. Ex: cjs(default) -> module.exports, es -> export, window.test -> window.test", ) .option("--watch", "Enables Watch Mode") .option( "--debounce ", "Debounces compilation for given amount of milliseconds", ) .option( "--output-prefix ", "Add a custom string to the beginning of compiled files (header/prefix). Useful for tools like linters or coverage directives. Defaults to '/*eslint-disable*/'", ) .on("--help", function () { console.log("\n Examples:\n") console.log( " # Compile translations and use defaults or message IDs for missing translations", ) console.log(` $ ${helpRun("compile")}`) console.log("") console.log(" # Compile translations but fail when resolved output") console.log(" # still has missing translations or compilation errors") console.log(` $ ${helpRun("compile --strict")}`) }) .parse(process.argv) const options = program.opts() const config = getConfig({ configPath: options.config }) let previousRun = Promise.resolve(true) const compile = () => { previousRun = previousRun.then(() => command(config, { verbose: options.watch || options.verbose || false, allowEmpty: !options.strict, failOnCompileError: !!options.strict, workersOptions: resolveWorkersOptions(options), typescript: options.typescript || config.compileNamespace === "ts" || false, namespace: options.namespace, // we want this to be undefined if user does not specify so default can be used outputPrefix: options.outputPrefix, }), ) return previousRun } const dispatchCompile = createWatchDispatcher({ debounce: options.debounce, run: compile, }) // Check if Watch Mode is enabled if (options.watch) { console.info(styleText("bold", "Initializing Watch Mode...")) ;(async function initWatch() { const { paths } = await getPathsForCompileWatcher(config) const watcher = watch(paths, { persistent: true, }) const onReady = () => { console.info(styleText(["green", "bold"], "Watcher is ready!")) watcher .on("add", () => dispatchCompile()) .on("change", () => dispatchCompile()) } watcher.on("ready", () => onReady()) })() } else { compile().then((results) => { if (!results) { process.exit(1) } }) } }