- Added typings for packages where typings existed
- Remove regexp ServiceParser and make AstServiceParser the default. #23 - Replaced CLI parser to add support for multiple input/output paths (supports file expansion, glob patterns and multiple values/parameters) #7
This commit is contained in:
9
src/cli/cli-options.interface.ts
Normal file
9
src/cli/cli-options.interface.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
export interface CliOptionsInterface {
|
||||
dir: string[];
|
||||
output: string[];
|
||||
format: 'json' | 'namespaced-json' | 'pot';
|
||||
replace: boolean;
|
||||
sort: boolean;
|
||||
clean: boolean;
|
||||
help: boolean;
|
||||
}
|
||||
148
src/cli/cli.ts
Executable file
148
src/cli/cli.ts
Executable file
@@ -0,0 +1,148 @@
|
||||
import { Extractor } from '../utils/extractor';
|
||||
import { CliOptionsInterface } from './cli-options.interface';
|
||||
import { TranslationCollection } from '../utils/translation.collection';
|
||||
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 { CompilerInterface } from '../compilers/compiler.interface';
|
||||
import { JsonCompiler } from '../compilers/json.compiler';
|
||||
import { NamespacedJsonCompiler } from '../compilers/namespaced-json.compiler';
|
||||
import { PoCompiler } from '../compilers/po.compiler';
|
||||
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import * as mkdirp from 'mkdirp';
|
||||
import * as chalk from 'chalk';
|
||||
import * as yargs from 'yargs';
|
||||
|
||||
const options: CliOptionsInterface = yargs
|
||||
.usage('Extract strings from files for translation.\nUsage: $0 [options]')
|
||||
.help('help')
|
||||
.option('dir', {
|
||||
alias: 'd',
|
||||
describe: 'Paths you would like to extract strings from. Multiple paths can be specified',
|
||||
default: process.env.PWD,
|
||||
type: 'array'
|
||||
})
|
||||
.option('output', {
|
||||
alias: 'o',
|
||||
describe: 'Path you would like to save extracted strings to. Multiple paths can be specified',
|
||||
default: process.env.PWD,
|
||||
type: 'array'
|
||||
})
|
||||
.option('format', {
|
||||
alias: 'f',
|
||||
describe: 'Output format',
|
||||
default: 'json',
|
||||
type: 'string',
|
||||
choices: ['json', 'namespaced-json', 'pot']
|
||||
})
|
||||
.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',
|
||||
describe: 'Sort translations in the output file in alphabetical order',
|
||||
default: false,
|
||||
type: 'boolean'
|
||||
})
|
||||
.option('clean', {
|
||||
alias: 'c',
|
||||
describe: 'Remove obsolete strings when merging',
|
||||
default: false,
|
||||
type: 'boolean'
|
||||
})
|
||||
.argv;
|
||||
|
||||
const patterns: string[] = [
|
||||
'/**/*.html',
|
||||
'/**/*.ts'
|
||||
];
|
||||
const parsers: ParserInterface[] = [
|
||||
new ServiceParser(),
|
||||
new PipeParser(),
|
||||
new DirectiveParser()
|
||||
];
|
||||
|
||||
let compiler: CompilerInterface;
|
||||
let ext: string;
|
||||
switch (options.format) {
|
||||
case 'pot':
|
||||
compiler = new PoCompiler();
|
||||
ext = 'pot';
|
||||
break;
|
||||
case 'json':
|
||||
compiler = new JsonCompiler();
|
||||
ext = 'json';
|
||||
break;
|
||||
case 'namespaced-json':
|
||||
compiler = new NamespacedJsonCompiler();
|
||||
ext = 'json';
|
||||
break;
|
||||
}
|
||||
|
||||
const extractor: Extractor = new Extractor(parsers, patterns);
|
||||
|
||||
let extractedStrings: TranslationCollection = new TranslationCollection();
|
||||
|
||||
// Extract strings from paths
|
||||
console.log(chalk.bold('Extracting strings from...'));
|
||||
options.dir.forEach(dir => {
|
||||
const normalizedDir: string = path.resolve(dir);
|
||||
if (!fs.existsSync(dir) || !fs.statSync(dir).isDirectory()) {
|
||||
console.log(`The path you supplied was not found: '${dir}'`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
console.log(chalk.gray('- %s'), normalizedDir);
|
||||
extractedStrings = extractedStrings.union(extractor.process(normalizedDir));
|
||||
});
|
||||
console.log(chalk.green('Extracted %d strings\n'), extractedStrings.count());
|
||||
|
||||
// Save extracted strings to output paths
|
||||
options.output.forEach(output => {
|
||||
const normalizedOutput: string = path.resolve(output);
|
||||
|
||||
let outputDir: string = normalizedOutput;
|
||||
let outputFilename: string = `template.${ext}`;
|
||||
if (!fs.existsSync(normalizedOutput) || !fs.statSync(normalizedOutput).isDirectory()) {
|
||||
outputDir = path.dirname(normalizedOutput);
|
||||
outputFilename = path.basename(normalizedOutput);
|
||||
}
|
||||
const outputPath: string = path.join(outputDir, outputFilename);
|
||||
|
||||
console.log(chalk.bold('Saving to: %s'), outputPath);
|
||||
if (!fs.existsSync(outputDir)) {
|
||||
console.log(chalk.dim('- Created output dir: %s'), outputDir);
|
||||
mkdirp.sync(outputDir);
|
||||
}
|
||||
|
||||
let processedStrings: TranslationCollection = extractedStrings;
|
||||
|
||||
if (fs.existsSync(outputPath) && !options.replace) {
|
||||
const existingStrings: TranslationCollection = compiler.parse(fs.readFileSync(outputPath, 'utf-8'));
|
||||
if (existingStrings.count() > 0) {
|
||||
processedStrings = processedStrings.union(existingStrings);
|
||||
console.log(chalk.dim('- Merged with %d existing strings'), existingStrings.count());
|
||||
}
|
||||
|
||||
if (options.clean) {
|
||||
const collectionCount = processedStrings.count();
|
||||
processedStrings = processedStrings.intersect(processedStrings);
|
||||
const removeCount = collectionCount - processedStrings.count();
|
||||
console.log(chalk.dim('- Removed %d obsolete strings'), removeCount);
|
||||
}
|
||||
}
|
||||
|
||||
if (options.sort) {
|
||||
processedStrings = processedStrings.sort();
|
||||
console.log(chalk.dim('- Sorted strings'));
|
||||
}
|
||||
|
||||
fs.writeFileSync(outputPath, compiler.compile(processedStrings));
|
||||
console.log(chalk.green('OK!\n'));
|
||||
});
|
||||
@@ -1,105 +0,0 @@
|
||||
import { Extractor } from '../utils/extractor';
|
||||
import { TranslationCollection } from '../utils/translation.collection';
|
||||
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 { AstServiceParser } from '../parsers/ast-service.parser';
|
||||
import { CompilerInterface } from '../compilers/compiler.interface';
|
||||
import { JsonCompiler } from '../compilers/json.compiler';
|
||||
import { NamespacedJsonCompiler } from '../compilers/namespaced-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({
|
||||
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],
|
||||
format: ['f', 'Output format', ['json', 'namespaced-json', 'pot'], 'json'],
|
||||
replace: ['r', 'Replace the contents of output file if it exists (Merges by default)', 'boolean', false],
|
||||
sort: ['s', 'Sort translations in the output file in alphabetical order', 'boolean', false],
|
||||
clean: ['c', 'Remove obsolete strings when merging', 'boolean', false],
|
||||
experimental: ['e', 'Use experimental AST Service Parser', 'boolean', false]
|
||||
});
|
||||
|
||||
const patterns: string[] = [
|
||||
'/**/*.html',
|
||||
'/**/*.ts'
|
||||
];
|
||||
const parsers: ParserInterface[] = [
|
||||
new PipeParser(),
|
||||
new DirectiveParser(),
|
||||
options.experimental ? new AstServiceParser() : new ServiceParser()
|
||||
];
|
||||
|
||||
let compiler: CompilerInterface;
|
||||
let ext: string;
|
||||
switch (options.format) {
|
||||
case 'pot':
|
||||
compiler = new PoCompiler();
|
||||
ext = 'pot';
|
||||
break;
|
||||
case 'json':
|
||||
compiler = new JsonCompiler();
|
||||
ext = 'json';
|
||||
break;
|
||||
case 'namespaced-json':
|
||||
compiler = new NamespacedJsonCompiler();
|
||||
ext = 'json';
|
||||
break;
|
||||
}
|
||||
|
||||
const normalizedDir: string = path.resolve(options.dir);
|
||||
const normalizedOutput: string = path.resolve(options.output);
|
||||
|
||||
let outputDir: string = normalizedOutput;
|
||||
let outputFilename: string = `template.${ext}`;
|
||||
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}'`);
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
const extractor: Extractor = new Extractor(parsers, patterns);
|
||||
cli.info(`Extracting strings from '${normalizedDir}'`);
|
||||
|
||||
const extracted: TranslationCollection = extractor.process(normalizedDir);
|
||||
cli.ok(`* Extracted ${extracted.count()} strings`);
|
||||
|
||||
let collection: TranslationCollection = extracted;
|
||||
|
||||
if (!options.replace && fs.existsSync(outputPath)) {
|
||||
const existing: TranslationCollection = compiler.parse(fs.readFileSync(outputPath, 'utf-8'));
|
||||
if (existing.count() > 0) {
|
||||
collection = extracted.union(existing);
|
||||
cli.ok(`* Merged with ${existing.count()} existing strings`);
|
||||
}
|
||||
|
||||
if (options.clean) {
|
||||
const collectionCount = collection.count();
|
||||
collection = collection.intersect(extracted);
|
||||
const removeCount = collectionCount - collection.count();
|
||||
if (removeCount > 0) {
|
||||
cli.ok(`* Removed ${removeCount} obsolete strings`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (options.sort) {
|
||||
collection = collection.sort();
|
||||
}
|
||||
|
||||
fs.writeFileSync(outputPath, compiler.compile(collection));
|
||||
cli.ok(`* Saved to '${outputPath}'`);
|
||||
} catch (e) {
|
||||
cli.fatal(e.toString());
|
||||
}
|
||||
Reference in New Issue
Block a user