Add cli option to customize compiler indentation. Closes #22
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
import { ExtractTask } from './tasks/extract.task';
|
||||
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 { CompilerFactory } from '../compilers/compiler.factory';
|
||||
|
||||
import * as fs from 'fs';
|
||||
import * as yargs from 'yargs';
|
||||
@@ -48,6 +51,12 @@ export const cli = yargs
|
||||
type: 'string',
|
||||
choices: ['json', 'namespaced-json', 'pot']
|
||||
})
|
||||
.option('format-indentation', {
|
||||
alias: 'fi',
|
||||
describe: 'Output format indentation',
|
||||
default: '\t',
|
||||
type: 'string'
|
||||
})
|
||||
.option('replace', {
|
||||
alias: 'r',
|
||||
describe: 'Replace the contents of output file if it exists (Merges by default)',
|
||||
@@ -69,19 +78,22 @@ export const cli = yargs
|
||||
.exitProcess(true)
|
||||
.parse(process.argv);
|
||||
|
||||
const extractTask = new ExtractTask(cli.input, cli.output, {
|
||||
const parsers: ParserInterface[] = [
|
||||
new ServiceParser(),
|
||||
new PipeParser(),
|
||||
new DirectiveParser()
|
||||
];
|
||||
|
||||
const compiler: CompilerInterface = CompilerFactory.create(cli.format, {
|
||||
indentation: cli.formatIndentation
|
||||
});
|
||||
|
||||
new ExtractTask(cli.input, cli.output, {
|
||||
replace: cli.replace,
|
||||
sort: cli.sort,
|
||||
clean: cli.clean,
|
||||
patterns: cli.patterns
|
||||
});
|
||||
|
||||
extractTask
|
||||
.setParsers([
|
||||
new ServiceParser(),
|
||||
new PipeParser(),
|
||||
new DirectiveParser()
|
||||
])
|
||||
.setCompiler(cli.format)
|
||||
.execute();
|
||||
|
||||
})
|
||||
.setParsers(parsers)
|
||||
.setCompiler(compiler)
|
||||
.execute();
|
||||
|
||||
@@ -2,7 +2,6 @@ import { TranslationCollection } from '../../utils/translation.collection';
|
||||
import { TaskInterface } from './task.interface';
|
||||
import { ParserInterface } from '../../parsers/parser.interface';
|
||||
import { CompilerInterface } from '../../compilers/compiler.interface';
|
||||
import { CompilerFactory } from '../../compilers/compiler.factory';
|
||||
|
||||
import * as chalk from 'chalk';
|
||||
import * as glob from 'glob';
|
||||
@@ -56,13 +55,8 @@ export class ExtractTask implements TaskInterface {
|
||||
return this;
|
||||
}
|
||||
|
||||
public setCompiler(compiler: CompilerInterface | string): this {
|
||||
if (typeof compiler === 'string') {
|
||||
this._compiler = CompilerFactory.create(compiler)
|
||||
} else {
|
||||
this._compiler = compiler;
|
||||
}
|
||||
|
||||
public setCompiler(compiler: CompilerInterface): this {
|
||||
this._compiler = compiler;
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -70,10 +64,9 @@ export class ExtractTask implements TaskInterface {
|
||||
* Extract strings from input dirs using configured parsers
|
||||
*/
|
||||
protected _extract(): TranslationCollection {
|
||||
let collection: TranslationCollection = new TranslationCollection();
|
||||
|
||||
this._out(chalk.bold('Extracting strings...'));
|
||||
|
||||
let collection: TranslationCollection = new TranslationCollection();
|
||||
this._input.forEach(dir => {
|
||||
this._readDir(dir, this._options.patterns).forEach(path => {
|
||||
this._out(chalk.gray('- %s'), path);
|
||||
@@ -83,6 +76,7 @@ export class ExtractTask implements TaskInterface {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
return collection;
|
||||
}
|
||||
|
||||
|
||||
@@ -5,11 +5,11 @@ import { PoCompiler } from '../compilers/po.compiler';
|
||||
|
||||
export class CompilerFactory {
|
||||
|
||||
public static create(format: string): CompilerInterface {
|
||||
public static create(format: string, options?: {}): CompilerInterface {
|
||||
switch (format) {
|
||||
case 'pot': return new PoCompiler();
|
||||
case 'json': return new JsonCompiler();
|
||||
case 'namespaced-json': return new NamespacedJsonCompiler();
|
||||
case 'pot': return new PoCompiler(options);
|
||||
case 'json': return new JsonCompiler(options);
|
||||
case 'namespaced-json': return new NamespacedJsonCompiler(options);
|
||||
default: throw new Error(`Unknown format: ${format}`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,10 +3,18 @@ import { TranslationCollection } from '../utils/translation.collection';
|
||||
|
||||
export class JsonCompiler implements CompilerInterface {
|
||||
|
||||
public indentation: string = '\t';
|
||||
|
||||
public extension = 'json';
|
||||
|
||||
public constructor(options?: any) {
|
||||
if (options && typeof options.indentation !== 'undefined') {
|
||||
this.indentation = options.indentation;
|
||||
}
|
||||
}
|
||||
|
||||
public compile(collection: TranslationCollection): string {
|
||||
return JSON.stringify(collection.values, null, '\t');
|
||||
return JSON.stringify(collection.values, null, this.indentation);
|
||||
}
|
||||
|
||||
public parse(contents: string): TranslationCollection {
|
||||
|
||||
@@ -5,13 +5,21 @@ import * as flat from 'flat';
|
||||
|
||||
export class NamespacedJsonCompiler implements CompilerInterface {
|
||||
|
||||
public indentation: string = '\t';
|
||||
|
||||
public extension = 'json';
|
||||
|
||||
public constructor(options?: any) {
|
||||
if (options && typeof options.indentation !== 'undefined') {
|
||||
this.indentation = options.indentation;
|
||||
}
|
||||
}
|
||||
|
||||
public compile(collection: TranslationCollection): string {
|
||||
const values: {} = flat.unflatten(collection.values, {
|
||||
object: true
|
||||
});
|
||||
return JSON.stringify(values, null, '\t');
|
||||
return JSON.stringify(values, null, this.indentation);
|
||||
}
|
||||
|
||||
public parse(contents: string): TranslationCollection {
|
||||
|
||||
@@ -12,6 +12,8 @@ export class PoCompiler implements CompilerInterface {
|
||||
*/
|
||||
public domain = '';
|
||||
|
||||
public constructor(options?: any) { }
|
||||
|
||||
public compile(collection: TranslationCollection): string {
|
||||
const data = {
|
||||
charset: 'utf-8',
|
||||
|
||||
Reference in New Issue
Block a user