diff --git a/bin/extract.js b/bin/extract.js index 6beb00a..dd0f703 100755 --- a/bin/extract.js +++ b/bin/extract.js @@ -1,80 +1,3 @@ #! /usr/bin/env node -const Extractor = require('../dist/extractor').Extractor; -const PipeParser = require('../dist/parsers/pipe.parser').PipeParser; -const DirectiveParser = require('../dist/parsers/directive.parser').DirectiveParser; -const ServiceParser = require('../dist/parsers/service.parser').ServiceParser; -const JsonCompiler = require('../dist/compilers/json.compiler').JsonCompiler; -const PoCompiler = require('../dist/compilers/po.compiler').PoCompiler - -const fs = require('fs'); -const path = require('path'); -const cli = require('cli'); - -const options = cli.parse({ - dir: ['d', 'Directory path you would like to extract strings from', 'dir', process.env.PWD], - output: ['o', 'Directory path you would like to save extracted strings', 'dir', process.env.PWD], - format: ['f', 'Output format', ['json', 'pot'], 'json'], - replace: ['r', 'Replace the contents of output file if it exists (merging by default)', 'boolean', false], - clean: ['c', 'Remove unused keys when merging', 'boolean', false] -}); - -[options.dir, options.output].forEach(dir => { - if (!fs.existsSync(dir)) { - cli.fatal('The directory path you supplied was not found: ' + dir); - } -}); - -const filename = 'template.' + options.format; -const dest = path.join(options.output, filename); - -const parsers = [ - new PipeParser(), - new DirectiveParser(), - new ServiceParser() -]; -const patterns = [ - '/**/*.html', - '/**/*.ts', - '/**/*.js' -]; - -const extractor = new Extractor(parsers, patterns); - -try { - cli.info(`Extracting strings from '${options.dir}'`); - const extracted = extractor.process(options.dir); - cli.ok(`* Extracted ${extracted.count()} strings`); - - if (extracted.isEmpty()) { - process.exit(); - } - - let collection = extracted; - - let compiler = new JsonCompiler(); - if (options.format === 'pot') { - compiler = new PoCompiler(); - } - - if (!options.replace && fs.existsSync(dest)) { - const existing = compiler.parse(fs.readFileSync(dest, 'utf-8')); - - collection = extracted.union(existing); - cli.ok(`* Merged ${existing.count()} existing strings`); - - if (options.clean) { - const collectionCount = collection.count(); - collection = collection.intersect(extracted); - const removeCount = collectionCount - collection.count(); - if (removeCount > 0) { - cli.ok(`* Removed ${removeCount} unused strings`); - } - } - } - - fs.writeFileSync(dest, compiler.compile(collection)); - cli.ok(`* Saved to '${dest}'`); -} catch (e) { - cli.fatal(e.toString()); -} +require('../dist/cli/extract'); diff --git a/src/cli/extract.ts b/src/cli/extract.ts new file mode 100755 index 0000000..200f27c --- /dev/null +++ b/src/cli/extract.ts @@ -0,0 +1,80 @@ +import { Extractor } from '../extractor'; +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 { JsonCompiler } from '../compilers/json.compiler'; +import { PoCompiler } from '../compilers/po.compiler'; +import { TranslationCollection } from '../utils/translation.collection'; + +import * as fs from 'fs'; +import * as path from 'path'; +import * as cli from 'cli'; + +const options = cli.parse({ + dir: ['d', 'Directory path you would like to extract strings from', 'dir', process.env.PWD], + output: ['o', 'Directory path you would like to save extracted strings', 'dir', process.env.PWD], + format: ['f', 'Output format', ['json', 'pot'], 'json'], + replace: ['r', 'Replace the contents of output file if it exists (merging by default)', 'boolean', false], + clean: ['c', 'Remove unused keys when merging', 'boolean', false] +}); + +[options.dir, options.output].forEach(dir => { + if (!fs.existsSync(dir)) { + cli.fatal('The directory path you supplied was not found: ' + dir); + } +}); + +const filename: string = 'template.' + options.format; +const dest: string = path.join(options.output, filename); + +const parsers: ParserInterface[] = [ + new PipeParser(), + new DirectiveParser(), + new ServiceParser() +]; +const patterns: string[] = [ + '/**/*.html', + '/**/*.ts', + '/**/*.js' +]; + +const extractor: Extractor = new Extractor(parsers, patterns); + +try { + cli.info(`Extracting strings from '${options.dir}'`); + const extracted: TranslationCollection = extractor.process(options.dir); + cli.ok(`* Extracted ${extracted.count()} strings`); + + if (extracted.isEmpty()) { + process.exit(); + } + + let collection: TranslationCollection = extracted; + + let compiler = new JsonCompiler(); + if (options.format === 'pot') { + compiler = new PoCompiler(); + } + + if (!options.replace && fs.existsSync(dest)) { + const existing: TranslationCollection = compiler.parse(fs.readFileSync(dest, 'utf-8')); + + collection = extracted.union(existing); + cli.ok(`* Merged ${existing.count()} existing strings`); + + if (options.clean) { + const collectionCount = collection.count(); + collection = collection.intersect(extracted); + const removeCount = collectionCount - collection.count(); + if (removeCount > 0) { + cli.ok(`* Removed ${removeCount} unused strings`); + } + } + } + + fs.writeFileSync(dest, compiler.compile(collection)); + cli.ok(`* Saved to '${dest}'`); +} catch (e) { + cli.fatal(e.toString()); +} diff --git a/src/compilers/po.compiler.ts b/src/compilers/po.compiler.ts index dbadd64..0f1fcf5 100644 --- a/src/compilers/po.compiler.ts +++ b/src/compilers/po.compiler.ts @@ -1,5 +1,5 @@ import { CompilerInterface } from './compiler.interface'; -import { TranslationCollection } from '../utils/translation.collection'; +import { TranslationCollection, TranslationType } from '../utils/translation.collection'; import * as gettext from 'gettext-parser'; @@ -25,7 +25,7 @@ export class PoCompiler implements CompilerInterface { msgstr: collection.get(key) }; return translations; - }, {}) + }, {}) } }; @@ -45,7 +45,7 @@ export class PoCompiler implements CompilerInterface { .reduce((values, key) => { values[key] = po.translations[this.domain][key].msgstr.pop(); return values; - }, {}); + }, {}); return new TranslationCollection(values); } diff --git a/src/example.ts b/src/example.ts deleted file mode 100644 index 542ba7d..0000000 --- a/src/example.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { Extractor } from './extractor'; -import { JsonCompiler } from './compilers/json.compiler'; -import { TranslationCollection } from './utils/translation.collection'; - -const compiler = new JsonCompiler(); -const extractor = new Extractor(); - -const dirPath = '/your/project'; - -try { - const collection: TranslationCollection = extractor.process(dirPath); - const result: string = compiler.compile(collection); - console.log(result); -} catch (e) { - console.log(`Something went wrong: ${e.toString()}`); -} diff --git a/src/utils/translation.collection.ts b/src/utils/translation.collection.ts index 83e70e3..fd6761c 100644 --- a/src/utils/translation.collection.ts +++ b/src/utils/translation.collection.ts @@ -18,7 +18,7 @@ export class TranslationCollection { const values = keys.reduce((results, key) => { results[key] = ''; return results; - }, {}); + }, {}); return new TranslationCollection(Object.assign({}, this.values, values)); } diff --git a/tsconfig.json b/tsconfig.json index b6ce0fb..a7da6d5 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,6 +2,7 @@ "compilerOptions": { "allowSyntheticDefaultImports": true, "noUnusedLocals": true, + "noImplicitAny": true, "removeComments": true, "declaration": true, "target": "ES6",