2016-12-10 06:13:18 +03:00
|
|
|
import { Extractor } from '../utils/extractor';
|
|
|
|
import { TranslationCollection } from '../utils/translation.collection';
|
2016-12-10 05:29:03 +03:00
|
|
|
import { ParserInterface } from '../parsers/parser.interface';
|
|
|
|
import { PipeParser } from '../parsers/pipe.parser';
|
|
|
|
import { DirectiveParser } from '../parsers/directive.parser';
|
|
|
|
import { ServiceParser } from '../parsers/service.parser';
|
|
|
|
import { JsonCompiler } from '../compilers/json.compiler';
|
|
|
|
import { PoCompiler } from '../compilers/po.compiler';
|
|
|
|
|
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as path from 'path';
|
|
|
|
import * as cli from 'cli';
|
|
|
|
|
|
|
|
const options = cli.parse({
|
2016-12-23 07:24:45 +03:00
|
|
|
dir: ['d', 'Path you would like to extract strings from', 'dir', process.env.PWD],
|
|
|
|
output: ['o', 'Path you would like to save extracted strings to', 'dir', process.env.PWD],
|
2016-12-10 05:29:03 +03:00
|
|
|
format: ['f', 'Output format', ['json', 'pot'], 'json'],
|
2016-12-20 18:02:56 +03:00
|
|
|
replace: ['r', 'Replace the contents of output file if it exists (Merges by default)', 'boolean', false],
|
|
|
|
clean: ['c', 'Remove obsolete strings when merging', 'boolean', false]
|
2016-12-10 05:29:03 +03:00
|
|
|
});
|
|
|
|
|
2016-12-23 07:24:45 +03:00
|
|
|
const normalizedDir: string = path.resolve(options.dir);
|
|
|
|
const normalizedOutput: string = path.resolve(options.output);
|
|
|
|
|
|
|
|
let outputDir: string = normalizedOutput;
|
|
|
|
let outputFilename: string = `template.${options.format}`;
|
|
|
|
if (!fs.existsSync(normalizedOutput) || !fs.statSync(normalizedOutput).isDirectory()) {
|
|
|
|
outputDir = path.dirname(normalizedOutput);
|
|
|
|
outputFilename = path.basename(normalizedOutput);
|
|
|
|
}
|
|
|
|
const outputPath: string = path.join(outputDir, outputFilename);
|
|
|
|
|
|
|
|
[normalizedDir, outputDir].forEach(dir => {
|
|
|
|
if (!fs.existsSync(dir) || !fs.statSync(dir).isDirectory()) {
|
|
|
|
cli.fatal(`The path you supplied was not found: '${dir}'`);
|
2016-12-10 05:29:03 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-12-23 07:54:49 +03:00
|
|
|
let compiler = new JsonCompiler();
|
|
|
|
if (options.format === 'pot') {
|
|
|
|
compiler = new PoCompiler();
|
|
|
|
}
|
2016-12-10 05:29:03 +03:00
|
|
|
const parsers: ParserInterface[] = [
|
|
|
|
new PipeParser(),
|
|
|
|
new DirectiveParser(),
|
|
|
|
new ServiceParser()
|
|
|
|
];
|
|
|
|
const patterns: string[] = [
|
|
|
|
'/**/*.html',
|
|
|
|
'/**/*.ts',
|
|
|
|
'/**/*.js'
|
|
|
|
];
|
|
|
|
|
|
|
|
try {
|
2016-12-13 17:17:03 +03:00
|
|
|
const extractor: Extractor = new Extractor(parsers, patterns);
|
2016-12-23 07:24:45 +03:00
|
|
|
cli.info(`Extracting strings from '${normalizedDir}'`);
|
2016-12-20 18:02:56 +03:00
|
|
|
|
2016-12-23 07:24:45 +03:00
|
|
|
const extracted: TranslationCollection = extractor.process(normalizedDir);
|
2016-12-10 05:29:03 +03:00
|
|
|
cli.ok(`* Extracted ${extracted.count()} strings`);
|
|
|
|
|
|
|
|
let collection: TranslationCollection = extracted;
|
|
|
|
|
2016-12-23 07:24:45 +03:00
|
|
|
if (!options.replace && fs.existsSync(outputPath)) {
|
|
|
|
const existing: TranslationCollection = compiler.parse(fs.readFileSync(outputPath, 'utf-8'));
|
2016-12-13 17:17:03 +03:00
|
|
|
if (existing.count() > 0) {
|
|
|
|
collection = extracted.union(existing);
|
2016-12-20 18:02:56 +03:00
|
|
|
cli.ok(`* Merged with ${existing.count()} existing strings`);
|
2016-12-13 17:17:03 +03:00
|
|
|
}
|
2016-12-10 05:29:03 +03:00
|
|
|
|
|
|
|
if (options.clean) {
|
|
|
|
const collectionCount = collection.count();
|
|
|
|
collection = collection.intersect(extracted);
|
|
|
|
const removeCount = collectionCount - collection.count();
|
|
|
|
if (removeCount > 0) {
|
2016-12-20 18:02:56 +03:00
|
|
|
cli.ok(`* Removed ${removeCount} obsolete strings`);
|
2016-12-10 05:29:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-23 07:24:45 +03:00
|
|
|
fs.writeFileSync(outputPath, compiler.compile(collection));
|
|
|
|
cli.ok(`* Saved to '${outputPath}'`);
|
2016-12-10 05:29:03 +03:00
|
|
|
} catch (e) {
|
|
|
|
cli.fatal(e.toString());
|
|
|
|
}
|