2016-12-03 17:09:39 +03:00
|
|
|
import { ParserInterface } from './parsers/parser.interface';
|
2016-12-06 19:56:45 +03:00
|
|
|
import { PipeParser } from './parsers/pipe.parser';
|
|
|
|
import { DirectiveParser } from "./parsers/directive.parser";
|
|
|
|
import { ServiceParser } from './parsers/service.parser';
|
2016-12-03 17:09:39 +03:00
|
|
|
import { SerializerInterface } from './serializers/serializer.interface';
|
|
|
|
|
2016-12-05 00:53:59 +03:00
|
|
|
import * as lodash from 'lodash';
|
|
|
|
import * as glob from 'glob';
|
|
|
|
import * as fs from 'fs';
|
2016-12-03 17:09:39 +03:00
|
|
|
|
|
|
|
export class Extractor {
|
|
|
|
|
2016-12-06 19:56:45 +03:00
|
|
|
public parsers: ParserInterface[] = [
|
|
|
|
new PipeParser(),
|
2016-12-07 08:10:48 +03:00
|
|
|
new DirectiveParser(),
|
|
|
|
new ServiceParser()
|
2016-12-06 19:56:45 +03:00
|
|
|
];
|
2016-12-03 17:09:39 +03:00
|
|
|
|
2016-12-05 00:53:59 +03:00
|
|
|
public globPatterns: string[] = [
|
2016-12-07 08:10:48 +03:00
|
|
|
'/**/*.html',
|
|
|
|
'/**/*.ts'
|
2016-12-05 00:53:59 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
public messages: string[] = [];
|
|
|
|
|
2016-12-03 17:09:39 +03:00
|
|
|
public constructor(public serializer: SerializerInterface) { }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extracts messages from paths
|
|
|
|
*/
|
2016-12-05 00:53:59 +03:00
|
|
|
public extract(dir: string): string[] {
|
2016-12-03 17:09:39 +03:00
|
|
|
let messages = [];
|
2016-12-07 08:10:48 +03:00
|
|
|
|
|
|
|
this._getFiles(dir).forEach(filePath => {
|
|
|
|
const result = this._extractMessages(filePath);
|
|
|
|
messages = [...messages, ...result];
|
2016-12-03 17:09:39 +03:00
|
|
|
});
|
|
|
|
|
2016-12-05 00:53:59 +03:00
|
|
|
return this.messages = lodash.uniq(messages);
|
2016-12-03 17:09:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Serialize and return output
|
|
|
|
*/
|
|
|
|
public serialize(): string {
|
|
|
|
return this.serializer.serialize(this.messages);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Serialize and save to destination
|
|
|
|
*/
|
|
|
|
public save(destination: string): string {
|
|
|
|
const data = this.serialize();
|
2016-12-05 00:53:59 +03:00
|
|
|
fs.writeFileSync(destination, data);
|
2016-12-03 17:09:39 +03:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-12-07 08:10:48 +03:00
|
|
|
* Get all files in dir that matches glob patterns
|
|
|
|
*/
|
|
|
|
protected _getFiles(dir: string): string[] {
|
|
|
|
let results: string[] = [];
|
|
|
|
|
|
|
|
this.globPatterns.forEach(globPattern => {
|
|
|
|
const files = glob
|
|
|
|
.sync(dir + globPattern)
|
|
|
|
.filter(filePath => fs.statSync(filePath).isFile());
|
|
|
|
|
|
|
|
results = [...results, ...files];
|
|
|
|
});
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extract messages from file using parser
|
2016-12-03 17:09:39 +03:00
|
|
|
*/
|
|
|
|
protected _extractMessages(filePath: string): string[] {
|
2016-12-07 08:10:48 +03:00
|
|
|
let results: string[] = [];
|
2016-12-03 17:09:39 +03:00
|
|
|
|
2016-12-05 00:53:59 +03:00
|
|
|
const contents: string = fs.readFileSync(filePath, 'utf-8');
|
2016-12-06 19:56:45 +03:00
|
|
|
this.parsers.forEach((parser: ParserInterface) => {
|
2016-12-07 08:10:48 +03:00
|
|
|
results = [...results, ...parser.process(filePath, contents)];
|
2016-12-06 19:56:45 +03:00
|
|
|
});
|
2016-12-03 17:09:39 +03:00
|
|
|
|
2016-12-06 19:56:45 +03:00
|
|
|
return results;
|
2016-12-03 17:09:39 +03:00
|
|
|
}
|
|
|
|
|
2016-12-07 08:10:48 +03:00
|
|
|
}
|