UNPKG

@lingui/cli

Version:

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

55 lines (54 loc) 1.51 kB
/** * Detect where is the command lingui extract or lingui compile * and how is being run (npm, yarn, pnpm) and construct help * for follow-up commands based on that. * * Example: * $ yarn extract * ... * (use "yarn compile" to compile catalogs for production) * * $ yarn lingui extract * ... * (use "yarn lingui compile" to compile catalogs for production) * * $ pnpm run extract * ... * (use "pnpm run compile" to compile catalogs for production) * * $ npm run extract * ... * (use "npm run compile" to compile catalogs for production) */ import { resolve, join } from "path"; import { readFileSync } from "node:fs"; export function helpRun(command) { let findRootPkgJson; try { findRootPkgJson = JSON.parse(readFileSync(resolve(join(process.cwd(), "package.json")), "utf8")); } catch (error) { // noting } if (findRootPkgJson?.scripts) { const res = Object.entries(findRootPkgJson.scripts).find(([_, value]) => value.includes(`lingui ${command}`)); if (res) { command = res[0]; } } const runCommand = runCommandFrom(process.env.npm_config_user_agent); return `${runCommand} ${command}`; } function runCommandFrom(userAgent) { const defaultRunCommand = "npm run"; if (!userAgent) { return defaultRunCommand; } if (userAgent.includes("yarn")) { return "yarn"; } if (userAgent.includes("pnpm")) { return "pnpm run"; } return defaultRunCommand; }