Move extractor to utils

This commit is contained in:
Kim Biesbjerg
2016-12-10 03:40:07 +01:00
parent 2ec4fd194f
commit 3bb74e6c9e

38
src/utils/extractor.ts Normal file
View File

@@ -0,0 +1,38 @@
import { ParserInterface } from '../parsers/parser.interface';
import { TranslationCollection } from './translation.collection';
import * as glob from 'glob';
import * as fs from 'fs';
export class Extractor {
public constructor(public parsers: ParserInterface[], public patterns: string[]) { }
/**
* Extract strings from dir
*/
public process(dir: string): TranslationCollection {
let collection: TranslationCollection = new TranslationCollection();
this._readDir(dir, this.patterns).forEach(path => {
const contents: string = fs.readFileSync(path, 'utf-8');
this.parsers.forEach((parser: ParserInterface) => {
collection = collection.union(parser.extract(contents, path));
});
});
return collection;
}
/**
* Get all files in dir matching patterns
*/
protected _readDir(dir: string, patterns: string[]): string[] {
return patterns.reduce((results, pattern) => {
return glob.sync(dir + pattern)
.filter(path => fs.statSync(path).isFile())
.concat(results);
}, []);
}
}