This commit is contained in:
Kim Biesbjerg 2019-09-18 13:47:57 +02:00
parent 8c8fe8d131
commit 3827789346
4 changed files with 51 additions and 43 deletions

View File

@ -16,7 +16,6 @@ export interface ExtractTaskOptionsInterface {
}
export class ExtractTask implements TaskInterface {
protected options: ExtractTaskOptionsInterface = {
replace: false,
patterns: []
@ -102,11 +101,11 @@ export class ExtractTask implements TaskInterface {
protected extract(): TranslationCollection {
let collection: TranslationCollection = new TranslationCollection();
this.inputs.forEach(dir => {
this.readDir(dir, this.options.patterns).forEach(path => {
this.out(dim('- %s'), path);
const contents: string = fs.readFileSync(path, 'utf-8');
this.readDir(dir, this.options.patterns).forEach(filePath => {
this.out(dim('- %s'), filePath);
const contents: string = fs.readFileSync(filePath, 'utf-8');
this.parsers.forEach(parser => {
const extracted = parser.extract(contents, path);
const extracted = parser.extract(contents, filePath);
if (extracted instanceof TranslationCollection) {
collection = collection.union(extracted);
}
@ -119,7 +118,11 @@ export class ExtractTask implements TaskInterface {
/**
* Run strings through configured post processors
*/
protected process(draft: TranslationCollection, extracted: TranslationCollection, existing: TranslationCollection): TranslationCollection {
protected process(
draft: TranslationCollection,
extracted: TranslationCollection,
existing: TranslationCollection
): TranslationCollection {
this.postProcessors.forEach(postProcessor => {
draft = postProcessor.process(draft, extracted, existing);
});
@ -143,8 +146,9 @@ export class ExtractTask implements TaskInterface {
*/
protected readDir(dir: string, patterns: string[]): string[] {
return patterns.reduce((results, pattern) => {
return glob.sync(dir + pattern)
.filter(path => fs.statSync(path).isFile())
return glob
.sync(dir + pattern)
.filter(filePath => fs.statSync(filePath).isFile())
.concat(results);
}, []);
}
@ -178,5 +182,4 @@ export class ExtractTask implements TaskInterface {
this.out(cyan(dim(`- ${this.compiler.constructor.name}`)));
this.out();
}
}

View File

@ -4,7 +4,6 @@ import { TranslationCollection, TranslationType } from '../utils/translation.col
import * as gettext from 'gettext-parser';
export class PoCompiler implements CompilerInterface {
public extension: string = 'po';
/**
@ -23,13 +22,16 @@ export class PoCompiler implements CompilerInterface {
'content-transfer-encoding': '8bit'
},
translations: {
[this.domain]: Object.keys(collection.values).reduce((translations, key) => {
translations[key] = {
msgid: key,
msgstr: collection.get(key)
};
return translations;
}, {} as any)
[this.domain]: Object.keys(collection.values).reduce(
(translations, key) => {
translations[key] = {
msgid: key,
msgstr: collection.get(key)
};
return translations;
},
{} as any
)
}
};
@ -46,12 +48,14 @@ export class PoCompiler implements CompilerInterface {
const values = Object.keys(po.translations[this.domain])
.filter(key => key.length > 0)
.reduce((values, key) => {
values[key] = po.translations[this.domain][key].msgstr.pop();
return values;
}, {} as TranslationType);
.reduce(
(result, key) => {
result[key] = po.translations[this.domain][key].msgstr.pop();
return result;
},
{} as TranslationType
);
return new TranslationCollection(values);
}
}

View File

@ -77,7 +77,7 @@ export function findMethodCallExpressions(node: Node, prop: string, fnName: stri
fnName = fnName.join('|');
}
const query = `CallExpression > PropertyAccessExpression:has(Identifier[name=/^(${fnName})$/]):has(PropertyAccessExpression:has(Identifier[name="${prop}"]):has(ThisKeyword))`;
let nodes = tsquery<PropertyAccessExpression>(node, query).map(node => node.parent as CallExpression);
const nodes = tsquery<PropertyAccessExpression>(node, query).map(n => n.parent as CallExpression);
return nodes;
}
@ -89,10 +89,7 @@ export function getStringsFromExpression(expression: Expression): string[] {
if (isArrayLiteralExpression(expression)) {
return expression.elements.reduce((result: string[], element: Expression) => {
const strings = getStringsFromExpression(element);
return [
...result,
...strings
];
return [...result, ...strings];
}, []);
}

View File

@ -3,7 +3,6 @@ export interface TranslationType {
}
export class TranslationCollection {
public values: TranslationType = {};
public constructor(values: TranslationType = {}) {
@ -15,10 +14,13 @@ export class TranslationCollection {
}
public addKeys(keys: string[]): TranslationCollection {
const values = keys.reduce((results, key) => {
results[key] = '';
return results;
}, {} as TranslationType);
const values = keys.reduce(
(results, key) => {
results[key] = '';
return results;
},
{} as TranslationType
);
return new TranslationCollection({ ...this.values, ...values });
}
@ -32,7 +34,7 @@ export class TranslationCollection {
}
public filter(callback: (key?: string, val?: string) => boolean): TranslationCollection {
let values: TranslationType = {};
const values: TranslationType = {};
this.forEach((key, val) => {
if (callback.call(this, key, val)) {
values[key] = val;
@ -42,7 +44,7 @@ export class TranslationCollection {
}
public map(callback: (key?: string, val?: string) => string): TranslationCollection {
let values: TranslationType = {};
const values: TranslationType = {};
this.forEach((key, val) => {
values[key] = callback.call(this, key, val);
});
@ -54,11 +56,11 @@ export class TranslationCollection {
}
public intersect(collection: TranslationCollection): TranslationCollection {
let values: TranslationType = {};
this.filter(key => collection.has(key))
this.forEach((key, val) => {
values[key] = val;
});
const values: TranslationType = {};
this.filter(key => collection.has(key));
this.forEach((key, val) => {
values[key] = val;
});
return new TranslationCollection(values);
}
@ -84,10 +86,12 @@ export class TranslationCollection {
}
public sort(compareFn?: (a: string, b: string) => number): TranslationCollection {
let values: TranslationType = {};
this.keys().sort(compareFn).forEach((key) => {
values[key] = this.get(key);
});
const values: TranslationType = {};
this.keys()
.sort(compareFn)
.forEach(key => {
values[key] = this.get(key);
});
return new TranslationCollection(values);
}