cray
Version:
Epub parser
80 lines (66 loc) • 2.41 kB
JavaScript
/**
* @author: Akshay Singh
* @date: 11/21/2015
* @company: Matter Inc
*/
var cray = require('commander');
var _ = require('lodash');
var Parser = require('../index');
var exec = require('child_process').exec;
var Table = require('cli-table');
var chalk = require('chalk');
var path = require('path');
var appPath = require('app-root-path');
function files(val) {
return val.split(',');
}
cray
.version('0.0.1')
.option('-v, --verbose', "Validate using IDPF epubcheck tool.")
.option('-e, --epubs [<path(s)>]', "Specify epub files to parse.", files, '')
.parse(process.argv);
if (!_.isEmpty(cray.epubs)) {
if (!cray.verbose) {
var epubs = Parser(cray.epubs);
var table = new Table({
head: ['Epub Name', 'Directory', 'Status'],
style: {head: ['blue']}
});
epubs.on('READY', function() {
_.each(epubs.queue, function(epub) {
table.push([epub.baseName, epub.dirName, (epub.error) ? chalk.red(epub.error.message) : chalk.green('Valid')]);
});
console.log(table.toString());
});
} else {
_.each(cray.epubs, function(epub) {
var isWin = /^win/.test(process.platform),
jarPath = isWin ? appPath.resolve('node_modules/cray/epubcheck/epubcheck.jar') :
appPath.resolve('/epubcheck/epubcheck.jar'),
command = 'java -jar ' + jarPath + ' ';
command += epub + ' -q';
exec(command, function(error, stdout, stderr) {
console.log('\n' + chalk.blue(epub));
if (error) {
if (/(ERROR|FATAL|WARNING)/.test(stderr)) {
_.each(stderr.split('\n'), function(info) {
var message = info.split(':'), type = message[0], text = message[2];
var color = '';
if (/(ERROR)/.test(info)) {color = chalk.white.bgYellow.bold(type)}
if (/(FATAL)/.test(info)) {color = chalk.white.bgRed.bold(type)}
if (/(WARNING)/.test(info)) {color = chalk.white.bgCyan.bold(type)}
if (text) {console.log('> ' + color + ' ' + text);}
});
return null;
} else if (stderr) {
return console.log(' ' + stderr);
}
}
console.log(' ' + (_.isEmpty(stdout)) ? chalk.green(' Valid EPUB!') : stdout);
});
});
}
} else {
cray.help();
}