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

View File

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

View File

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

View File

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