Compare commits

...

4 Commits

Author SHA1 Message Date
Kim Biesbjerg
cb8731ee0f Bump version to 6.0.2 2020-03-25 14:11:13 +01:00
Kim Biesbjerg
9908681243 Use proper exit codes 2020-03-25 14:10:13 +01:00
Kim Biesbjerg
a83123fb12 better handling when destination is unreadable / cannot be parsed 2020-03-25 12:57:56 +01:00
Kim Biesbjerg
6b740867d6 Update README 2020-03-25 12:42:13 +01:00
4 changed files with 34 additions and 19 deletions

View File

@@ -15,7 +15,7 @@ Add a script to your project's `package.json`:
```json
...
"scripts": {
"extract-i18n": "ngx-translate-extract --input ./src --output ./src/assets/i18n/ --clean --sort --format namespaced-json"
"extract-i18n": "ngx-translate-extract --input ./src --output ./src/assets/i18n/strings.json --key-as-default-value --clean --sort --format namespaced-json"
}
...
```
@@ -25,11 +25,11 @@ You can now run `npm run extract-i18n` and it will extract strings from your pro
**Extract from dir and save to file**
`ngx-translate-extract --input ./src --output ./src/assets/i18n/template.json`
`ngx-translate-extract --input ./src --output ./src/assets/i18n/strings.json`
**Extract from multiple dirs**
`ngx-translate-extract --input ./src-a ./src-b --output ./src/assets/i18n/template.json`
`ngx-translate-extract --input ./src-a ./src-b --output ./src/assets/i18n/strings.json`
**Extract and save to multiple files using path expansion**

View File

@@ -1,6 +1,6 @@
{
"name": "@biesbjerg/ngx-translate-extract",
"version": "6.0.1",
"version": "6.0.2",
"description": "Extract strings from projects using ngx-translate",
"main": "dist/index.js",
"typings": "dist/index.d.ts",

View File

@@ -1,4 +1,5 @@
import * as yargs from 'yargs';
import { red, green } from 'colorette';
import { ExtractTask } from './tasks/extract.task';
import { ParserInterface } from '../parsers/parser.interface';
@@ -148,6 +149,13 @@ const compiler: CompilerInterface = CompilerFactory.create(cli.format, {
});
extractTask.setCompiler(compiler);
extractTask.execute();
console.log(donateMessage);
// Run task
try {
extractTask.execute();
console.log(green('\nDone.\n'));
console.log(donateMessage);
process.exit(0);
} catch (e) {
console.log(red(`\nAn error occurred: ${e}\n`));
process.exit(1);
}

View File

@@ -4,7 +4,7 @@ import { ParserInterface } from '../../parsers/parser.interface';
import { PostProcessorInterface } from '../../post-processors/post-processor.interface';
import { CompilerInterface } from '../../compilers/compiler.interface';
import { cyan, green, bold, dim } from 'colorette';
import { cyan, green, bold, dim, red } from 'colorette';
import * as glob from 'glob';
import * as fs from 'fs';
import * as path from 'path';
@@ -56,26 +56,33 @@ export class ExtractTask implements TaskInterface {
let existing: TranslationCollection = new TranslationCollection();
if (!this.options.replace && fs.existsSync(outputPath)) {
existing = this.compiler.parse(fs.readFileSync(outputPath, 'utf-8'));
try {
existing = this.compiler.parse(fs.readFileSync(outputPath, 'utf-8'));
} catch (e) {
this.out(`%s %s`, dim(`- ${outputPath}`), red(`[ERROR]`));
throw e;
}
}
// merge extracted strings with existing
const draft = extracted.union(existing);
if (existing.isEmpty()) {
this.out(dim(`- ${outputPath}`));
} else {
this.out(dim(`- ${outputPath} (merged)`));
}
// Run collection through post processors
const final = this.process(draft, extracted, existing);
// Save to file
this.save(outputPath, final);
// Save
try {
let event = 'CREATED';
if (!fs.existsSync(outputPath)) {
this.options.replace ? event = 'REPLACED' : event = 'MERGED';
}
this.save(outputPath, final);
this.out(`%s %s`, dim(`- ${outputPath}`), green(`[${event}]`));
} catch (e) {
this.out(`%s %s`, dim(`- ${outputPath}`), red(`[ERROR]`));
throw e;
}
});
this.out(green('\nDone.\n'));
}
public setParsers(parsers: ParserInterface[]): this {