import {program} from 'commander' import loudRejection from 'loud-rejection' import extract, {ExtractCLIOptions} from './extract' import compile, {CompileCLIOpts, Opts} from './compile' import compileFolder from './compile_folder' import {sync as globSync} from 'fast-glob' import {debug} from './console_utils' const KNOWN_COMMANDS = ['extract'] async function main(argv: string[]) { loudRejection() const version = require('../package.json').version program .version(version, '-v, --version') .usage(' [flags]') .action(command => { if (!KNOWN_COMMANDS.includes(command)) { program.help() } }) program .command('help', {isDefault: true}) .description('Show this help message.') .action(() => program.help()) // Long text wrapping to available terminal columns: https://github.com/tj/commander.js/pull/956 // NOTE: please keep the help text in sync with babel-plugin-formatjs documentation. program .command('extract [files...]') .description( `Extract string messages from React components that use react-intl. The input language is expected to be TypeScript or ES2017 with JSX.` ) .option( '--format ', `Path to a formatter file that controls the shape of JSON file from \`--out-file\`. The formatter file must export a function called \`format\` with the signature \`\`\` type FormatFn = >( msgs: Record ) => T \`\`\` This is especially useful to convert from our extracted format to a TMS-specific format. ` ) .option( '--out-file ', `The target file path where the plugin will output an aggregated \`.json\` file of all the translations from the \`files\` supplied.` ) .option( '--id-interpolation-pattern ', `If certain message descriptors don't have id, this \`pattern\` will be used to automatically generate IDs for them. Default to \`[sha512:contenthash:base64:6]\` where \`contenthash\` is the hash of \`defaultMessage\` and \`description\`. See https://github.com/webpack/loader-utils#interpolatename for sample patterns`, '[sha512:contenthash:base64:6]' ) .option( '--extract-source-location', `Whether the metadata about the location of the message in the source file should be extracted. If \`true\`, then \`file\`, \`start\`, and \`end\` fields will exist for each extracted message descriptors.`, false ) .option( '--remove-default-message', 'Remove `defaultMessage` field in generated js after extraction', false ) .option( '--additional-component-names ', `Additional component names to extract messages from, e.g: \`'FormattedFooBarMessage'\`. **NOTE**: By default we check for the fact that \`FormattedMessage\` is imported from \`moduleSourceName\` to make sure variable alias works. This option does not do that so it's less safe.`, (val: string) => val.split(',') ) .option( '--additional-function-names ', `Additional function names to extract messages from, e.g: \`'$t'\`.`, (val: string) => val.split(',') ) .option( '--ignore ', 'List of glob paths to **not** extract translations from.' ) .option( '--throws', 'Whether to throw an exception when we fail to process any file in the batch.' ) .option( '--pragma ', `parse specific additional custom pragma. This allows you to tag certain file with metadata such as \`project\`. For example with this file: \`\`\` // @intl-meta project:my-custom-project import {FormattedMessage} from 'react-intl'; ; \`\`\` and with option \`{pragma: "intl-meta"}\`, we'll parse out \`// @intl-meta project:my-custom-project\` into \`{project: 'my-custom-project'}\` in the result file.` ) .option( '--preserve-whitespace', 'Whether to preserve whitespace and newlines.' ) .option( '--flatten', `Whether to hoist selectors & flatten sentences as much as possible. E.g: "I have {count, plural, one{a dog} other{many dogs}}" becomes "{count, plural, one{I have a dog} other{I have many dogs}}". The goal is to provide as many full sentences as possible since fragmented sentences are not translator-friendly.` ) .action(async (files: readonly string[], cmdObj: ExtractCLIOptions) => { debug('File pattern:', files) debug('Options:', cmdObj) const processedFiles = [] for (const f of files) { processedFiles.push( ...globSync(f, { ignore: cmdObj.ignore, }) ) } debug('Files to extract:', files) await extract(processedFiles, { outFile: cmdObj.outFile, idInterpolationPattern: cmdObj.idInterpolationPattern || '[sha1:contenthash:base64:6]', extractSourceLocation: cmdObj.extractSourceLocation, removeDefaultMessage: cmdObj.removeDefaultMessage, additionalComponentNames: cmdObj.additionalComponentNames, additionalFunctionNames: cmdObj.additionalFunctionNames, throws: cmdObj.throws, pragma: cmdObj.pragma, format: cmdObj.format, // It is possible that the glob pattern does NOT match anything. // But so long as the glob pattern is provided, don't read from stdin. readFromStdin: files.length === 0, preserveWhitespace: cmdObj.preserveWhitespace, flatten: cmdObj.flatten, }) process.exit(0) }) program .command('compile ') .description( `Compile extracted translation file into react-intl consumable JSON We also verify that the messages are valid ICU and not malformed. can be a glob like "foo/**/en.json"` ) .option( '--format ', `Path to a formatter file that converts \`\` to \`Record\` so we can compile. The file must export a function named \`compile\` with the signature: \`\`\` type CompileFn = >( msgs: T ) => Record; \`\`\` This is especially useful to convert from a TMS-specific format back to react-intl format ` ) .option( '--out-file ', `Compiled translation output file. If this is not provided, result will be printed to stdout` ) .option( '--ast', `Whether to compile to AST. See https://formatjs.io/docs/guides/advanced-usage#pre-parsing-messages for more information` ) .option( '--skip-errors', `Whether to continue compiling messages after encountering an error. Any keys with errors will not be included in the output file.` ) .option( '--pseudo-locale ', `Whether to generate pseudo-locale files. See https://formatjs.io/docs/tooling/cli#--pseudo-locale-pseudolocale for possible values. "--ast" is required for this to work.` ) .action(async (filePattern: string, opts: CompileCLIOpts) => { debug('File pattern:', filePattern) debug('Options:', opts) const files = globSync(filePattern) if (!files.length) { throw new Error(`No input file found with pattern ${filePattern}`) } debug('Files to compile:', files) await compile(files, opts) }) program .command('compile-folder ') .description( `Batch compile all extracted translation JSON files in to containing react-intl consumable JSON. We also verify that the messages are valid ICU and not malformed.` ) .option( '--format ', `Path to a formatter file that converts JSON files in \`\` to \`Record\` so we can compile. The file must export a function named \`compile\` with the signature: \`\`\` type CompileFn = >( msgs: T ) => Record; \`\`\` This is especially useful to convert from a TMS-specific format back to react-intl format ` ) .option( '--ast', `Whether to compile to AST. See https://formatjs.io/docs/guides/advanced-usage#pre-parsing-messages for more information` ) .action(async (folder: string, outFolder: string, opts?: Opts) => { debug('Folder:', folder) debug('Options:', opts) // fast-glob expect `/` in Windows as well const files = globSync(`${folder}/*.json`) if (!files.length) { throw new Error(`No JSON file found in ${folder}`) } debug('Files to compile:', files) await compileFolder(files, outFolder, opts) }) if (argv.length < 3) { program.help() } else { program.parse(argv) } } export default main