ngx-translate-extract/bin/extract.js

47 lines
1.4 KiB
JavaScript
Raw Normal View History

2016-12-05 00:54:35 +03:00
#! /usr/bin/env node
var cli = require('cli');
var fs = require('fs');
var path = require('path');
var Extractor = require('../dist/extractor').Extractor;
var JsonSerializer = require('../dist/serializers/json.serializer').JsonSerializer;
var PotSerializer = require('../dist/serializers/pot.serializer').PotSerializer;
var 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']
});
[options.dir, options.output].forEach(dir => {
if (!fs.existsSync(dir)) {
cli.fatal('The directory path you supplied was not found: ' + dir);
}
});
switch (options.format) {
case 'pot':
var serializer = new PotSerializer();
break;
case 'json':
var serializer = new JsonSerializer();
break;
}
var filename = 'template.' + options.format;
var destination = path.join(options.output, filename);
try {
var extractor = new Extractor(serializer);
var messages = extractor.extract(options.dir);
if (messages.length > 0) {
extractor.save(destination);
cli.ok(`Extracted ${messages.length} strings: '${destination}'`);
} else {
2016-12-05 01:48:11 +03:00
cli.info(`Found no extractable strings in the supplied directory path: '${options.dir}'`);
2016-12-05 00:54:35 +03:00
}
} catch (e) {
cli.fatal(e.toString());
}