Move glob patterns to extractor
This commit is contained in:
		| @@ -4,9 +4,9 @@ import { TypescriptParser } from './parsers/typescript.parser'; | |||||||
| import { SerializerInterface } from './serializers/serializer.interface'; | import { SerializerInterface } from './serializers/serializer.interface'; | ||||||
| import { PotSerializer } from './serializers/pot.serializer'; | import { PotSerializer } from './serializers/pot.serializer'; | ||||||
|  |  | ||||||
| import { uniq as arrayUnique } from 'lodash'; | import * as lodash from 'lodash'; | ||||||
| import { sync as readDir } from 'glob'; | import * as glob from 'glob'; | ||||||
| import { readFileSync as readFile, writeFileSync as writeFile } from 'fs'; | import * as fs from 'fs'; | ||||||
|  |  | ||||||
| export interface TypeParserMap { | export interface TypeParserMap { | ||||||
| 	[ext: string]: ParserInterface | 	[ext: string]: ParserInterface | ||||||
| @@ -14,29 +14,36 @@ export interface TypeParserMap { | |||||||
|  |  | ||||||
| export class Extractor { | export class Extractor { | ||||||
|  |  | ||||||
| 	public messages: string[] = []; |  | ||||||
|  |  | ||||||
| 	public parsers: TypeParserMap = { | 	public parsers: TypeParserMap = { | ||||||
| 		html: new HtmlParser(), | 		html: new HtmlParser(), | ||||||
| 		ts: new TypescriptParser() | 		ts: new TypescriptParser() | ||||||
| 	}; | 	}; | ||||||
|  |  | ||||||
|  | 	public globPatterns: string[] = [ | ||||||
|  | 		'/**/*.ts', | ||||||
|  | 		'/**/*.html' | ||||||
|  | 	]; | ||||||
|  |  | ||||||
|  | 	public messages: string[] = []; | ||||||
|  |  | ||||||
| 	public constructor(public serializer: SerializerInterface) { } | 	public constructor(public serializer: SerializerInterface) { } | ||||||
|  |  | ||||||
| 	/** | 	/** | ||||||
| 	 * Extracts messages from paths | 	 * Extracts messages from paths | ||||||
| 	 */ | 	 */ | ||||||
| 	public extract(paths: string[]): string[] { | 	public extract(dir: string): string[] { | ||||||
| 		let messages = []; | 		let messages = []; | ||||||
| 		paths.forEach(path => { | 		this.globPatterns.forEach(globPattern => { | ||||||
| 			const filePaths = readDir(path); | 			const filePaths = glob.sync(dir + globPattern); | ||||||
| 			filePaths.forEach(filePath => { | 			filePaths | ||||||
|  | 				.filter(filePath => fs.statSync(filePath).isFile()) | ||||||
|  | 				.forEach(filePath => { | ||||||
| 					const result = this._extractMessages(filePath); | 					const result = this._extractMessages(filePath); | ||||||
| 					messages = [...messages, ...result]; | 					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 { | 	public save(destination: string): string { | ||||||
| 		const data = this.serialize(); | 		const data = this.serialize(); | ||||||
| 		writeFile(destination, data); | 		fs.writeFileSync(destination, data); | ||||||
|  |  | ||||||
| 		return data; | 		return data; | ||||||
| 	} | 	} | ||||||
| @@ -65,7 +72,7 @@ export class Extractor { | |||||||
| 			return []; | 			return []; | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		const contents: string = readFile(filePath).toString(); | 		const contents: string = fs.readFileSync(filePath, 'utf-8'); | ||||||
| 		const parser: ParserInterface = this.parsers[ext]; | 		const parser: ParserInterface = this.parsers[ext]; | ||||||
|  |  | ||||||
| 		return parser.process(contents); | 		return parser.process(contents); | ||||||
|   | |||||||
| @@ -17,7 +17,7 @@ export class TypescriptParser implements ParserInterface { | |||||||
|  |  | ||||||
| 		for (let patternName in this.patterns) { | 		for (let patternName in this.patterns) { | ||||||
| 			const regExp = this._createRegExp(patternName, { | 			const regExp = this._createRegExp(patternName, { | ||||||
| 				TRANSLATE_SERVICE: translateServiceVar | 				'TRANSLATE_SERVICE': translateServiceVar | ||||||
| 			}); | 			}); | ||||||
|  |  | ||||||
| 			let matches; | 			let matches; | ||||||
| @@ -42,7 +42,7 @@ export class TypescriptParser implements ParserInterface { | |||||||
| 			pattern = pattern.replace('{{' + key + '}}', replaceVars[key]); | 			pattern = pattern.replace('{{' + key + '}}', replaceVars[key]); | ||||||
| 		}); | 		}); | ||||||
|  |  | ||||||
| 		return new RegExp(pattern, 'gi'); | 		return new RegExp(pattern, 'g'); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	/** | 	/** | ||||||
|   | |||||||
| @@ -13,6 +13,7 @@ export class PotSerializer implements SerializerInterface { | |||||||
| 		this._reset(); | 		this._reset(); | ||||||
| 		this._addHeader(this._headers); | 		this._addHeader(this._headers); | ||||||
| 		this._addMessages(messages); | 		this._addMessages(messages); | ||||||
|  |  | ||||||
| 		return this._buffer.join('\n'); | 		return this._buffer.join('\n'); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user