2017-03-21 17:21:39 +03:00
|
|
|
import { ExtractTask } from './tasks/extract.task';
|
2017-03-29 15:20:01 +03:00
|
|
|
import { ParserInterface } from '../parsers/parser.interface';
|
2017-03-20 17:29:41 +03:00
|
|
|
import { PipeParser } from '../parsers/pipe.parser';
|
|
|
|
import { DirectiveParser } from '../parsers/directive.parser';
|
|
|
|
import { ServiceParser } from '../parsers/service.parser';
|
2017-03-30 15:37:30 +03:00
|
|
|
import { FunctionParser } from '../parsers/function.parser';
|
2019-06-12 00:06:47 +03:00
|
|
|
import { PostProcessorInterface } from '../post-processors/post-processor.interface';
|
|
|
|
import { SortByKeyPostProcessor } from '../post-processors/sort-by-key.post-processor';
|
|
|
|
import { KeyAsDefaultValuePostProcessor } from '../post-processors/key-as-default-value.post-processor';
|
|
|
|
import { PurgeObsoleteKeysPostProcessor } from '../post-processors/purge-obsolete-keys.post-processor';
|
2017-03-29 15:20:01 +03:00
|
|
|
import { CompilerInterface } from '../compilers/compiler.interface';
|
|
|
|
import { CompilerFactory } from '../compilers/compiler.factory';
|
2017-03-20 17:29:41 +03:00
|
|
|
|
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as yargs from 'yargs';
|
|
|
|
|
2017-03-21 17:21:39 +03:00
|
|
|
export const cli = yargs
|
2017-03-20 17:29:41 +03:00
|
|
|
.usage('Extract strings from files for translation.\nUsage: $0 [options]')
|
2017-03-21 17:27:24 +03:00
|
|
|
.version(require(__dirname + '/../../package.json').version)
|
2017-03-21 17:21:39 +03:00
|
|
|
.alias('version', 'v')
|
|
|
|
.help('help')
|
|
|
|
.alias('help', 'h')
|
2017-03-20 17:51:38 +03:00
|
|
|
.option('input', {
|
2017-03-21 17:21:39 +03:00
|
|
|
alias: 'i',
|
2017-03-20 17:51:38 +03:00
|
|
|
describe: 'Paths you would like to extract strings from. You can use path expansion, glob patterns and multiple paths',
|
2017-03-20 17:29:41 +03:00
|
|
|
default: process.env.PWD,
|
2017-03-20 17:51:38 +03:00
|
|
|
type: 'array',
|
2017-03-21 17:21:39 +03:00
|
|
|
normalize: true
|
2017-03-20 17:51:38 +03:00
|
|
|
})
|
2017-03-21 17:21:39 +03:00
|
|
|
.check(options => {
|
|
|
|
options.input.forEach((dir: string) => {
|
2017-03-20 17:51:38 +03:00
|
|
|
if (!fs.existsSync(dir) || !fs.statSync(dir).isDirectory()) {
|
2017-03-29 15:20:35 +03:00
|
|
|
throw new Error(`The path you supplied was not found: '${dir}'`);
|
2017-03-20 17:51:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
return true;
|
2017-03-20 17:29:41 +03:00
|
|
|
})
|
2017-03-21 17:21:39 +03:00
|
|
|
.option('patterns', {
|
|
|
|
alias: 'p',
|
2017-03-21 17:59:03 +03:00
|
|
|
describe: 'Extract strings from the following file patterns',
|
2017-03-21 17:21:39 +03:00
|
|
|
type: 'array',
|
|
|
|
default: ['/**/*.html', '/**/*.ts']
|
|
|
|
})
|
2017-03-20 17:29:41 +03:00
|
|
|
.option('output', {
|
|
|
|
alias: 'o',
|
2017-03-20 17:51:38 +03:00
|
|
|
describe: 'Paths where you would like to save extracted strings. You can use path expansion, glob patterns and multiple paths',
|
|
|
|
type: 'array',
|
2017-03-21 17:21:39 +03:00
|
|
|
normalize: true,
|
2017-03-20 17:51:38 +03:00
|
|
|
required: true
|
2017-03-20 17:29:41 +03:00
|
|
|
})
|
2017-03-30 15:37:30 +03:00
|
|
|
.option('marker', {
|
|
|
|
alias: 'm',
|
|
|
|
describe: 'Extract strings passed to a marker function',
|
|
|
|
default: false,
|
|
|
|
type: 'string'
|
|
|
|
})
|
2017-03-20 17:29:41 +03:00
|
|
|
.option('format', {
|
|
|
|
alias: 'f',
|
|
|
|
describe: 'Output format',
|
|
|
|
default: 'json',
|
|
|
|
type: 'string',
|
|
|
|
choices: ['json', 'namespaced-json', 'pot']
|
|
|
|
})
|
2017-03-29 15:20:01 +03:00
|
|
|
.option('format-indentation', {
|
|
|
|
alias: 'fi',
|
|
|
|
describe: 'Output format indentation',
|
|
|
|
default: '\t',
|
|
|
|
type: 'string'
|
|
|
|
})
|
2017-03-20 17:29:41 +03:00
|
|
|
.option('replace', {
|
|
|
|
alias: 'r',
|
|
|
|
describe: 'Replace the contents of output file if it exists (Merges by default)',
|
|
|
|
default: false,
|
|
|
|
type: 'boolean'
|
|
|
|
})
|
|
|
|
.option('sort', {
|
|
|
|
alias: 's',
|
2017-03-21 17:59:03 +03:00
|
|
|
describe: 'Sort strings in alphabetical order when saving',
|
2017-03-20 17:29:41 +03:00
|
|
|
default: false,
|
|
|
|
type: 'boolean'
|
|
|
|
})
|
|
|
|
.option('clean', {
|
|
|
|
alias: 'c',
|
|
|
|
describe: 'Remove obsolete strings when merging',
|
|
|
|
default: false,
|
|
|
|
type: 'boolean'
|
|
|
|
})
|
2019-06-12 00:06:47 +03:00
|
|
|
.option('key-as-default-value', {
|
|
|
|
alias: 'k',
|
|
|
|
describe: 'Use key as default value for translations',
|
2019-06-11 14:00:10 +03:00
|
|
|
default: false,
|
2017-11-07 17:14:31 +03:00
|
|
|
type: 'boolean'
|
|
|
|
})
|
2017-03-21 17:21:39 +03:00
|
|
|
.exitProcess(true)
|
|
|
|
.parse(process.argv);
|
|
|
|
|
2019-06-12 00:06:47 +03:00
|
|
|
const extractTask = new ExtractTask(cli.input, cli.output, {
|
2017-03-30 15:37:30 +03:00
|
|
|
replace: cli.replace,
|
2019-06-12 00:06:47 +03:00
|
|
|
patterns: cli.patterns
|
2017-03-30 15:37:30 +03:00
|
|
|
});
|
2017-03-29 15:20:01 +03:00
|
|
|
|
2019-06-12 00:06:47 +03:00
|
|
|
// Parsers
|
2017-03-30 15:37:30 +03:00
|
|
|
const parsers: ParserInterface[] = [
|
|
|
|
new PipeParser(),
|
|
|
|
new DirectiveParser(),
|
|
|
|
new ServiceParser()
|
|
|
|
];
|
|
|
|
if (cli.marker) {
|
|
|
|
parsers.push(new FunctionParser({
|
|
|
|
identifier: cli.marker
|
|
|
|
}));
|
|
|
|
}
|
2019-06-12 00:06:47 +03:00
|
|
|
extractTask.setParsers(parsers);
|
|
|
|
|
2019-06-13 12:44:04 +03:00
|
|
|
// Post processors
|
|
|
|
const postProcessors: PostProcessorInterface[] = [];
|
2019-06-12 00:06:47 +03:00
|
|
|
if (cli.clean) {
|
2019-06-13 12:44:04 +03:00
|
|
|
postProcessors.push(new PurgeObsoleteKeysPostProcessor());
|
2019-06-12 00:06:47 +03:00
|
|
|
}
|
|
|
|
if (cli.keyAsDefaultValue) {
|
2019-06-13 12:44:04 +03:00
|
|
|
postProcessors.push(new KeyAsDefaultValuePostProcessor());
|
2019-06-12 00:06:47 +03:00
|
|
|
}
|
|
|
|
if (cli.sort) {
|
2019-06-13 12:44:04 +03:00
|
|
|
postProcessors.push(new SortByKeyPostProcessor());
|
2019-06-12 00:06:47 +03:00
|
|
|
}
|
2019-06-13 12:44:04 +03:00
|
|
|
extractTask.setPostProcessors(postProcessors);
|
2019-06-12 00:06:47 +03:00
|
|
|
|
|
|
|
// Compiler
|
|
|
|
const compiler: CompilerInterface = CompilerFactory.create(cli.format, {
|
|
|
|
indentation: cli.formatIndentation
|
|
|
|
});
|
|
|
|
extractTask.setCompiler(compiler);
|
2017-03-30 15:37:30 +03:00
|
|
|
|
2019-06-12 00:06:47 +03:00
|
|
|
extractTask.execute();
|