Refactor and clean code
This commit is contained in:
parent
73801a9cc5
commit
59ef277c64
@ -10,8 +10,8 @@ const dest = '/your/project/template.json';
|
||||
|
||||
try {
|
||||
const collection: StringCollection = extractor.process(src);
|
||||
const output: string = extractor.save(dest);
|
||||
console.log({ strings: collection.keys(), output: output });
|
||||
const serialized: string = extractor.save(dest);
|
||||
console.log({ strings: collection.keys(), serialized });
|
||||
} catch (e) {
|
||||
console.log(`Something went wrong: ${e.toString()}`);
|
||||
}
|
||||
|
@ -10,18 +10,18 @@ import * as fs from 'fs';
|
||||
|
||||
export class Extractor {
|
||||
|
||||
public patterns: string[] = [
|
||||
'/**/*.html',
|
||||
'/**/*.ts',
|
||||
'/**/*.js'
|
||||
];
|
||||
|
||||
public parsers: ParserInterface[] = [
|
||||
new PipeParser(),
|
||||
new DirectiveParser(),
|
||||
new ServiceParser()
|
||||
];
|
||||
|
||||
public find: string[] = [
|
||||
'/**/*.html',
|
||||
'/**/*.ts',
|
||||
'/**/*.js'
|
||||
];
|
||||
|
||||
public collection: StringCollection = new StringCollection();
|
||||
|
||||
public constructor(public serializer: SerializerInterface) { }
|
||||
@ -30,7 +30,7 @@ export class Extractor {
|
||||
* Process dir
|
||||
*/
|
||||
public process(dir: string): StringCollection {
|
||||
this._getFiles(dir).forEach(path => {
|
||||
this._readDir(dir, this.patterns).forEach(path => {
|
||||
const contents: string = fs.readFileSync(path, 'utf-8');
|
||||
this.parsers.forEach((parser: ParserInterface) => {
|
||||
this.collection.merge(parser.extract(contents, path));
|
||||
@ -57,20 +57,14 @@ export class Extractor {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all files in dir that matches glob patterns
|
||||
* Get all files in dir matching find patterns
|
||||
*/
|
||||
protected _getFiles(dir: string): string[] {
|
||||
let results: string[] = [];
|
||||
|
||||
this.find.forEach(pattern => {
|
||||
const files = glob
|
||||
.sync(dir + pattern)
|
||||
.filter(path => fs.statSync(path).isFile());
|
||||
|
||||
results = [...results, ...files];
|
||||
});
|
||||
|
||||
return results;
|
||||
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);
|
||||
}, []);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -5,14 +5,14 @@ export abstract class AbstractTemplateParser {
|
||||
* makes the assumption that it is an Angular Component
|
||||
*/
|
||||
protected _isAngularComponent(path: string): boolean {
|
||||
return new RegExp(/\.(ts|js)$/, 'i').test(path);
|
||||
return new RegExp(/\.ts|js$/, 'i').test(path);
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts inline template from components
|
||||
*/
|
||||
protected _extractInlineTemplate(contents: string): string {
|
||||
const match = new RegExp(/template\s?:\s?("|\'|`)((.|[\r\n])+?[^\\])\1/).exec(contents);
|
||||
const match = new RegExp(/template\s*:\s*(["'`])((.|[\r\n])+?[^\\])\1/).exec(contents);
|
||||
if (match !== null) {
|
||||
return match[2];
|
||||
}
|
||||
|
@ -19,7 +19,7 @@ export class DirectiveParser extends AbstractTemplateParser implements ParserInt
|
||||
|
||||
template = this._normalizeTemplateAttributes(template);
|
||||
$(template)
|
||||
.find('[translate],[ng2-translate]')
|
||||
.find('[translate], [ng2-translate]')
|
||||
.addBack()
|
||||
.each((i: number, element: CheerioElement) => {
|
||||
const $element = $(element);
|
||||
@ -31,8 +31,8 @@ export class DirectiveParser extends AbstractTemplateParser implements ParserInt
|
||||
$element
|
||||
.contents()
|
||||
.toArray()
|
||||
.filter(textNode => textNode.type === 'text')
|
||||
.map(textNode => textNode.nodeValue.trim())
|
||||
.filter(node => node.type === 'text')
|
||||
.map(node => node.nodeValue.trim())
|
||||
.filter(text => text.length > 0)
|
||||
.forEach(text => collection.add(text));
|
||||
}
|
||||
|
@ -15,7 +15,7 @@ export class PipeParser extends AbstractTemplateParser implements ParserInterfac
|
||||
protected _parseTemplate(template: string): StringCollection {
|
||||
const collection = new StringCollection();
|
||||
|
||||
const regExp = new RegExp(/([\'"`])([^\1\r\n]*)\1\s+\|\s*translate(:.*?)?/, 'g');
|
||||
const regExp = new RegExp(/(['"`])([^\1\r\n]*)\1\s*\|\s*translate(:.*?)?/, 'g');
|
||||
|
||||
let matches;
|
||||
while (matches = regExp.exec(template)) {
|
||||
|
@ -11,13 +11,14 @@ export class ServiceParser implements ParserInterface {
|
||||
return collection;
|
||||
}
|
||||
|
||||
const methodRegExp: RegExp = new RegExp(/(?:get|instant)\s*\(\s*(\[?([\'"`])([^\1\r\n]+)\2\]?)/);
|
||||
const methodRegExp: RegExp = new RegExp(/(?:get|instant)\s*\(\s*(\[?(['"`])([^\1\r\n]+)\2\]?)/);
|
||||
const regExp: RegExp = new RegExp(`${translateServiceVar}\.${methodRegExp.source}`, 'g');
|
||||
|
||||
let matches;
|
||||
while (matches = regExp.exec(contents)) {
|
||||
if (this._stringContainsArray(matches[1])) {
|
||||
collection.add(this._stringToArray(matches[1]));
|
||||
const matchCollection = StringCollection.fromArray(this._stringToArray(matches[1]));
|
||||
collection.merge(matchCollection);
|
||||
} else {
|
||||
collection.add(matches[3]);
|
||||
}
|
||||
@ -26,6 +27,18 @@ export class ServiceParser implements ParserInterface {
|
||||
return collection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts name of TranslateService variable for use in patterns
|
||||
*/
|
||||
protected _extractTranslateServiceVar(contents: string): string {
|
||||
const matches = contents.match(/([a-z0-9_]+)\s*:\s*TranslateService/i);
|
||||
if (matches === null) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return matches[1];
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if string contains an array
|
||||
*/
|
||||
@ -44,16 +57,4 @@ export class ServiceParser implements ParserInterface {
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts name of TranslateService variable for use in patterns
|
||||
*/
|
||||
protected _extractTranslateServiceVar(contents: string): string {
|
||||
const matches = contents.match(/([a-z0-9_]+)\s*:\s*TranslateService/i);
|
||||
if (matches === null) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return matches[1];
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user