refactor(localize): update yargs and typings for yargs (#38470)

Updating yargs and typings for the updated yargs module.

PR Close #38470
This commit is contained in:
Joey Perrott 2020-08-14 14:00:01 -07:00 committed by atscott
parent 301513311e
commit 8373b720f3
2 changed files with 36 additions and 21 deletions

View File

@ -30,12 +30,14 @@ if (require.main === module) {
alias: 'locale', alias: 'locale',
describe: 'The locale of the source being processed', describe: 'The locale of the source being processed',
default: 'en', default: 'en',
type: 'string',
}) })
.option('r', { .option('r', {
alias: 'root', alias: 'root',
default: '.', default: '.',
describe: 'The root path for other paths provided in these options.\n' + describe: 'The root path for other paths provided in these options.\n' +
'This should either be absolute or relative to the current working directory.' 'This should either be absolute or relative to the current working directory.',
type: 'string',
}) })
.option('s', { .option('s', {
alias: 'source', alias: 'source',
@ -43,40 +45,45 @@ if (require.main === module) {
describe: describe:
'A glob pattern indicating what files to search for translations, e.g. `./dist/**/*.js`.\n' + 'A glob pattern indicating what files to search for translations, e.g. `./dist/**/*.js`.\n' +
'This should be relative to the root path.', 'This should be relative to the root path.',
type: 'string',
}) })
.option('f', { .option('f', {
alias: 'format', alias: 'format',
required: true, required: true,
choices: ['xmb', 'xlf', 'xlif', 'xliff', 'xlf2', 'xlif2', 'xliff2', 'json'], choices: ['xmb', 'xlf', 'xlif', 'xliff', 'xlf2', 'xlif2', 'xliff2', 'json'],
describe: 'The format of the translation file.', describe: 'The format of the translation file.',
type: 'string',
}) })
.option('o', { .option('o', {
alias: 'outputPath', alias: 'outputPath',
required: true, required: true,
describe: describe:
'A path to where the translation file will be written. This should be relative to the root path.' 'A path to where the translation file will be written. This should be relative to the root path.',
type: 'string',
}) })
.option('loglevel', { .option('loglevel', {
describe: 'The lowest severity logging message that should be output.', describe: 'The lowest severity logging message that should be output.',
choices: ['debug', 'info', 'warn', 'error'], choices: ['debug', 'info', 'warn', 'error'],
type: 'string',
}) })
.option('useSourceMaps', { .option('useSourceMaps', {
type: 'boolean', type: 'boolean',
default: true, default: true,
describe: describe:
'Whether to generate source information in the output files by following source-map mappings found in the source files' 'Whether to generate source information in the output files by following source-map mappings found in the source files',
}) })
.option('useLegacyIds', { .option('useLegacyIds', {
type: 'boolean', type: 'boolean',
default: true, default: true,
describe: describe:
'Whether to use the legacy id format for messages that were extracted from Angular templates.' 'Whether to use the legacy id format for messages that were extracted from Angular templates.',
}) })
.option('d', { .option('d', {
alias: 'duplicateMessageHandling', alias: 'duplicateMessageHandling',
describe: 'How to handle messages with the same id but different text.', describe: 'How to handle messages with the same id but different text.',
choices: ['error', 'warning', 'ignore'], choices: ['error', 'warning', 'ignore'],
default: 'warning', default: 'warning',
type: 'string',
}) })
.strict() .strict()
.help() .help()
@ -85,22 +92,22 @@ if (require.main === module) {
const fs = new NodeJSFileSystem(); const fs = new NodeJSFileSystem();
setFileSystem(fs); setFileSystem(fs);
const rootPath = options['root']; const rootPath = options.r;
const sourceFilePaths = glob.sync(options['source'], {cwd: rootPath, nodir: true}); const sourceFilePaths = glob.sync(options.s, {cwd: rootPath, nodir: true});
const logLevel = options['loglevel'] as (keyof typeof LogLevel) | undefined; const logLevel = options.loglevel as (keyof typeof LogLevel) | undefined;
const logger = new ConsoleLogger(logLevel ? LogLevel[logLevel] : LogLevel.warn); const logger = new ConsoleLogger(logLevel ? LogLevel[logLevel] : LogLevel.warn);
const duplicateMessageHandling: DiagnosticHandlingStrategy = options['d']; const duplicateMessageHandling = options.d as DiagnosticHandlingStrategy;
extractTranslations({ extractTranslations({
rootPath, rootPath,
sourceFilePaths, sourceFilePaths,
sourceLocale: options['locale'], sourceLocale: options.l,
format: options['format'], format: options.f,
outputPath: options['outputPath'], outputPath: options.o,
logger, logger,
useSourceMaps: options['useSourceMaps'], useSourceMaps: options.useSourceMaps,
useLegacyIds: options['useLegacyIds'], useLegacyIds: options.useLegacyIds,
duplicateMessageHandling, duplicateMessageHandling,
}); });
} }

View File

@ -30,18 +30,21 @@ if (require.main === module) {
required: true, required: true,
describe: describe:
'The root path of the files to translate, either absolute or relative to the current working directory. E.g. `dist/en`.', 'The root path of the files to translate, either absolute or relative to the current working directory. E.g. `dist/en`.',
type: 'string',
}) })
.option('s', { .option('s', {
alias: 'source', alias: 'source',
required: true, required: true,
describe: describe:
'A glob pattern indicating what files to translate, relative to the `root` path. E.g. `bundles/**/*`.', 'A glob pattern indicating what files to translate, relative to the `root` path. E.g. `bundles/**/*`.',
type: 'string',
}) })
.option('l', { .option('l', {
alias: 'source-locale', alias: 'source-locale',
describe: describe:
'The source locale of the application. If this is provided then a copy of the application will be created with no translation but just the `$localize` calls stripped out.', 'The source locale of the application. If this is provided then a copy of the application will be created with no translation but just the `$localize` calls stripped out.',
type: 'string',
}) })
.option('t', { .option('t', {
@ -54,6 +57,7 @@ if (require.main === module) {
'If you want to merge multiple translation files for each locale, then provide the list of files in an array.\n' + 'If you want to merge multiple translation files for each locale, then provide the list of files in an array.\n' +
'Note that the arrays must be in double quotes if you include any whitespace within the array.\n' + 'Note that the arrays must be in double quotes if you include any whitespace within the array.\n' +
'E.g. `-t "[src/locale/messages.en.xlf, src/locale/messages-2.en.xlf]" [src/locale/messages.fr.xlf,src/locale/messages-2.fr.xlf]`', 'E.g. `-t "[src/locale/messages.en.xlf, src/locale/messages-2.en.xlf]" [src/locale/messages.fr.xlf,src/locale/messages-2.fr.xlf]`',
type: 'string',
}) })
.option('target-locales', { .option('target-locales', {
@ -61,6 +65,7 @@ if (require.main === module) {
describe: describe:
'A list of target locales for the translation files, which will override any target locale parsed from the translation file.\n' + 'A list of target locales for the translation files, which will override any target locale parsed from the translation file.\n' +
'E.g. "-t en fr de".', 'E.g. "-t en fr de".',
type: 'string',
}) })
.option('o', { .option('o', {
@ -68,7 +73,8 @@ if (require.main === module) {
required: true, required: true,
describe: 'A output path pattern to where the translated files will be written.\n' + describe: 'A output path pattern to where the translated files will be written.\n' +
'The path must be either absolute or relative to the current working directory.\n' + 'The path must be either absolute or relative to the current working directory.\n' +
'The marker `{{LOCALE}}` will be replaced with the target locale. E.g. `dist/{{LOCALE}}`.' 'The marker `{{LOCALE}}` will be replaced with the target locale. E.g. `dist/{{LOCALE}}`.',
type: 'string',
}) })
.option('m', { .option('m', {
@ -76,6 +82,7 @@ if (require.main === module) {
describe: 'How to handle missing translations.', describe: 'How to handle missing translations.',
choices: ['error', 'warning', 'ignore'], choices: ['error', 'warning', 'ignore'],
default: 'warning', default: 'warning',
type: 'string',
}) })
.option('d', { .option('d', {
@ -83,6 +90,7 @@ if (require.main === module) {
describe: 'How to handle duplicate translations.', describe: 'How to handle duplicate translations.',
choices: ['error', 'warning', 'ignore'], choices: ['error', 'warning', 'ignore'],
default: 'warning', default: 'warning',
type: 'string',
}) })
.strict() .strict()
@ -92,14 +100,14 @@ if (require.main === module) {
const fs = new NodeJSFileSystem(); const fs = new NodeJSFileSystem();
setFileSystem(fs); setFileSystem(fs);
const sourceRootPath = options['r']; const sourceRootPath = options.r;
const sourceFilePaths = glob.sync(options['s'], {cwd: sourceRootPath, nodir: true}); const sourceFilePaths = glob.sync(options.s, {cwd: sourceRootPath, nodir: true});
const translationFilePaths: (string|string[])[] = convertArraysFromArgs(options['t']); const translationFilePaths: (string|string[])[] = convertArraysFromArgs(options.t);
const outputPathFn = getOutputPathFn(fs.resolve(options['o'])); const outputPathFn = getOutputPathFn(fs.resolve(options.o));
const diagnostics = new Diagnostics(); const diagnostics = new Diagnostics();
const missingTranslation: DiagnosticHandlingStrategy = options['m']; const missingTranslation = options.m as DiagnosticHandlingStrategy;
const duplicateTranslation: DiagnosticHandlingStrategy = options['d']; const duplicateTranslation = options.d as DiagnosticHandlingStrategy;
const sourceLocale: string|undefined = options['l']; const sourceLocale: string|undefined = options.l;
const translationFileLocales: string[] = options['target-locales'] || []; const translationFileLocales: string[] = options['target-locales'] || [];
translateFiles({ translateFiles({