Add NamespacedJsonCompiler

This commit is contained in:
Kim Biesbjerg
2017-01-13 09:02:01 +01:00
parent 183592acc3
commit 7822916e28
6 changed files with 94 additions and 20 deletions

View File

@@ -4,7 +4,9 @@ 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';
@@ -14,17 +16,45 @@ 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', 'pot'], 'json'],
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]
});
const patterns: string[] = [
'/**/*.html',
'/**/*.ts',
'/**/*.js'
];
const parsers: ParserInterface[] = [
new PipeParser(),
new DirectiveParser(),
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.${options.format}`;
let outputFilename: string = `template.${ext}`;
if (!fs.existsSync(normalizedOutput) || !fs.statSync(normalizedOutput).isDirectory()) {
outputDir = path.dirname(normalizedOutput);
outputFilename = path.basename(normalizedOutput);
@@ -37,21 +67,6 @@ const outputPath: string = path.join(outputDir, outputFilename);
}
});
let compiler = new JsonCompiler();
if (options.format === 'pot') {
compiler = new PoCompiler();
}
const parsers: ParserInterface[] = [
new PipeParser(),
new DirectiveParser(),
new ServiceParser()
];
const patterns: string[] = [
'/**/*.html',
'/**/*.ts',
'/**/*.js'
];
try {
const extractor: Extractor = new Extractor(parsers, patterns);
cli.info(`Extracting strings from '${normalizedDir}'`);

View File

@@ -0,0 +1,18 @@
import { CompilerInterface } from './compiler.interface';
import { TranslationCollection } from '../utils/translation.collection';
import * as flat from 'flat';
export class NamespacedJsonCompiler implements CompilerInterface {
public compile(collection: TranslationCollection): string {
const values = flat.unflatten(collection.values);
return JSON.stringify(values, null, '\t');
}
public parse(contents: string): TranslationCollection {
const values = flat.flatten(JSON.parse(contents));
return new TranslationCollection(values);
}
}

View File

@@ -1,2 +1,3 @@
declare module 'cli';
declare module 'flat';
declare module 'gettext-parser';