- (chore) update packages

- (refactor) use tsquery for querying AST
- (feat) autodetect usage of marker function and remove --marker cli argument
- (bugfix) extract strings when TranslateService is declared directly as a class parameter. Closes https://github.com/biesbjerg/ngx-translate-extract/issues/83
- (bugfix) handle split strings: marker('hello ' + 'world') is now extracted as a single string: 'hello world'
This commit is contained in:
Kim Biesbjerg
2019-09-16 16:40:37 +02:00
parent 41bd679fcd
commit 4fe3c43624
13 changed files with 346 additions and 325 deletions

View File

@@ -6,7 +6,7 @@ import { ParserInterface } from '../parsers/parser.interface';
import { PipeParser } from '../parsers/pipe.parser';
import { DirectiveParser } from '../parsers/directive.parser';
import { ServiceParser } from '../parsers/service.parser';
import { FunctionParser } from '../parsers/function.parser';
import { MarkerParser } from '../parsers/marker.parser';
import { PostProcessorInterface } from '../post-processors/post-processor.interface';
import { SortByKeyPostProcessor } from '../post-processors/sort-by-key.post-processor';
import { KeyAsDefaultValuePostProcessor } from '../post-processors/key-as-default-value.post-processor';
@@ -29,7 +29,7 @@ export const cli = yargs
normalize: true
})
.check(options => {
options.input.forEach((dir: string) => {
(options.input as unknown as string[]).forEach((dir: string) => {
if (!fs.existsSync(dir) || !fs.statSync(dir).isDirectory()) {
throw new Error(`The path you supplied was not found: '${dir}'`);
}
@@ -50,12 +50,6 @@ export const cli = yargs
normalize: true,
required: true
})
.option('marker', {
alias: 'm',
describe: 'Extract strings passed to a marker function',
default: false,
type: 'string'
})
.option('format', {
alias: 'f',
describe: 'Output format',
@@ -96,7 +90,7 @@ export const cli = yargs
.exitProcess(true)
.parse(process.argv);
const extractTask = new ExtractTask(cli.input, cli.output, {
const extractTask = new ExtractTask(cli.input as unknown as string[], cli.output, {
replace: cli.replace,
patterns: cli.patterns
});
@@ -105,13 +99,9 @@ const extractTask = new ExtractTask(cli.input, cli.output, {
const parsers: ParserInterface[] = [
new PipeParser(),
new DirectiveParser(),
new ServiceParser()
new ServiceParser(),
new MarkerParser()
];
if (cli.marker) {
parsers.push(new FunctionParser({
identifier: cli.marker
}));
}
extractTask.setParsers(parsers);
// Post processors

View File

@@ -100,17 +100,20 @@ export class ExtractTask implements TaskInterface {
* Extract strings from specified input dirs using configured parsers
*/
protected extract(): TranslationCollection {
let extracted: TranslationCollection = new 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.parsers.forEach(parser => {
extracted = extracted.union(parser.extract(contents, path));
const extracted = parser.extract(contents, path);
if (extracted) {
collection = collection.union(extracted);
}
});
});
});
return extracted;
return collection;
}
/**