Move glob patterns to extractor

This commit is contained in:
Kim Biesbjerg 2016-12-04 22:53:59 +01:00
parent eac24905de
commit 74fd853097
3 changed files with 25 additions and 17 deletions

View File

@ -4,9 +4,9 @@ import { TypescriptParser } from './parsers/typescript.parser';
import { SerializerInterface } from './serializers/serializer.interface';
import { PotSerializer } from './serializers/pot.serializer';
import { uniq as arrayUnique } from 'lodash';
import { sync as readDir } from 'glob';
import { readFileSync as readFile, writeFileSync as writeFile } from 'fs';
import * as lodash from 'lodash';
import * as glob from 'glob';
import * as fs from 'fs';
export interface TypeParserMap {
[ext: string]: ParserInterface
@ -14,29 +14,36 @@ export interface TypeParserMap {
export class Extractor {
public messages: string[] = [];
public parsers: TypeParserMap = {
html: new HtmlParser(),
ts: new TypescriptParser()
};
public globPatterns: string[] = [
'/**/*.ts',
'/**/*.html'
];
public messages: string[] = [];
public constructor(public serializer: SerializerInterface) { }
/**
* Extracts messages from paths
*/
public extract(paths: string[]): string[] {
public extract(dir: string): string[] {
let messages = [];
paths.forEach(path => {
const filePaths = readDir(path);
filePaths.forEach(filePath => {
const result = this._extractMessages(filePath);
messages = [...messages, ...result];
});
this.globPatterns.forEach(globPattern => {
const filePaths = glob.sync(dir + globPattern);
filePaths
.filter(filePath => fs.statSync(filePath).isFile())
.forEach(filePath => {
const result = this._extractMessages(filePath);
messages = [...messages, ...result];
});
});
return this.messages = arrayUnique(messages);
return this.messages = lodash.uniq(messages);
}
/**
@ -51,7 +58,7 @@ export class Extractor {
*/
public save(destination: string): string {
const data = this.serialize();
writeFile(destination, data);
fs.writeFileSync(destination, data);
return data;
}
@ -65,7 +72,7 @@ export class Extractor {
return [];
}
const contents: string = readFile(filePath).toString();
const contents: string = fs.readFileSync(filePath, 'utf-8');
const parser: ParserInterface = this.parsers[ext];
return parser.process(contents);

View File

@ -17,7 +17,7 @@ export class TypescriptParser implements ParserInterface {
for (let patternName in this.patterns) {
const regExp = this._createRegExp(patternName, {
TRANSLATE_SERVICE: translateServiceVar
'TRANSLATE_SERVICE': translateServiceVar
});
let matches;
@ -42,7 +42,7 @@ export class TypescriptParser implements ParserInterface {
pattern = pattern.replace('{{' + key + '}}', replaceVars[key]);
});
return new RegExp(pattern, 'gi');
return new RegExp(pattern, 'g');
}
/**

View File

@ -13,6 +13,7 @@ export class PotSerializer implements SerializerInterface {
this._reset();
this._addHeader(this._headers);
this._addMessages(messages);
return this._buffer.join('\n');
}