Refactor cli and move responsibilities out of Extractor

This commit is contained in:
Kim Biesbjerg 2016-12-10 01:57:20 +01:00
parent c9bae68a84
commit a7e0a9d909
2 changed files with 42 additions and 39 deletions

View File

@ -1,18 +1,21 @@
#! /usr/bin/env node #! /usr/bin/env node
var cli = require('cli'); const Extractor = require('../dist/extractor').Extractor;
var fs = require('fs'); const PipeParser = require('../dist/parsers/pipe.parser').PipeParser;
var path = require('path'); 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
var Extractor = require('../dist/extractor').Extractor; const fs = require('fs');
var JsonCompiler = require('../dist/compilers/json.compiler').JsonCompiler; const path = require('path');
var PoCompiler = require('../dist/compilers/po.compiler').PoCompiler const cli = require('cli');
var options = cli.parse({ const options = cli.parse({
dir: ['d', 'Directory path you would like to extract strings from', 'dir', process.env.PWD], 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], output: ['o', 'Directory path you would like to save extracted strings', 'dir', process.env.PWD],
format: ['f', 'Output format', ['json', 'pot'], 'json'], format: ['f', 'Output format', ['json', 'pot'], 'json'],
merge: ['m', 'Merge extracted strings with existing file if it exists', 'boolean', true], 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] clean: ['c', 'Remove unused keys when merging', 'boolean', false]
}); });
@ -22,21 +25,26 @@ var options = cli.parse({
} }
}); });
switch (options.format) { const filename = 'template.' + options.format;
case 'pot': const dest = path.join(options.output, filename);
var compiler = new PoCompiler();
break;
case 'json':
var compiler = new JsonCompiler();
break;
}
var filename = 'template.' + options.format; const parsers = [
var dest = path.join(options.output, filename); new PipeParser(),
new DirectiveParser(),
new ServiceParser()
];
const patterns = [
'/**/*.html',
'/**/*.ts',
'/**/*.js'
];
const extractor = new Extractor(parsers, patterns);
try { try {
var extracted = new Extractor().process(options.dir); cli.info(`Extracting strings from '${options.dir}'`);
cli.info(`* Extracted ${extracted.count()} strings`); const extracted = extractor.process(options.dir);
cli.ok(`* Extracted ${extracted.count()} strings`);
if (extracted.isEmpty()) { if (extracted.isEmpty()) {
process.exit(); process.exit();
@ -44,21 +52,29 @@ try {
let collection = extracted; let collection = extracted;
if (options.merge && fs.existsSync(dest)) { 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')); const existing = compiler.parse(fs.readFileSync(dest, 'utf-8'));
collection = extracted.union(existing); collection = extracted.union(existing);
cli.info(`* Merged with existing strings`); cli.ok(`* Merged ${existing.count()} existing strings`);
if (options.clean) { if (options.clean) {
const stringCount = collection.count(); const collectionCount = collection.count();
collection = collection.intersect(extracted); collection = collection.intersect(extracted);
cli.info(`* Removed ${stringCount - collection.count()} unused strings`); const removeCount = collectionCount - collection.count();
if (removeCount > 0) {
cli.ok(`* Removed ${removeCount} unused strings`);
}
} }
} }
fs.writeFileSync(dest, compiler.compile(collection)); fs.writeFileSync(dest, compiler.compile(collection));
cli.ok(`Saved to: '${dest}'`); cli.ok(`* Saved to '${dest}'`);
} catch (e) { } catch (e) {
cli.fatal(e.toString()); cli.fatal(e.toString());
} }

View File

@ -1,7 +1,4 @@
import { ParserInterface } from './parsers/parser.interface'; 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 { TranslationCollection } from './utils/translation.collection'; import { TranslationCollection } from './utils/translation.collection';
import * as glob from 'glob'; import * as glob from 'glob';
@ -9,17 +6,7 @@ import * as fs from 'fs';
export class Extractor { export class Extractor {
public patterns: string[] = [ public constructor(public parsers: ParserInterface[], public patterns: string[]) { }
'/**/*.html',
'/**/*.ts',
'/**/*.js'
];
public parsers: ParserInterface[] = [
new PipeParser(),
new DirectiveParser(),
new ServiceParser()
];
/** /**
* Extract strings from dir * Extract strings from dir