UNPKG

1.34 kBJavaScriptView Raw
1/**
2 * @author: Akshay Kr Singh.
3 * @date: 20/11/15.
4 * @github: https://github.com/akshayKrSingh
5 */
6var Reader = require('../reader/index');
7var Editor = require('../editor/index');
8var _ = require('lodash');
9var util = require('util');
10var EventEmitter = require('events').EventEmitter;
11var parsePath = require('parse-filepath');
12
13var Parser = function(path) {
14 if (!(this instanceof Parser)) {
15 return new Parser(path);
16 }
17
18 EventEmitter.call(this);
19
20 var paths = (path) ? _.isArray(path) ? path : [path] : [],
21 epubs = [], queueLength = paths.length, processedCounter = 0;
22 var parser = this;
23
24 function emitWhenDone() {
25 if (processedCounter >= queueLength) {
26 parser.emit('READY', epubs);
27 }
28 }
29
30 _.each(paths, function(loc) {
31 var e = Reader({path: loc}), locInfo = parsePath(loc);
32 e.parse();
33
34 e.baseName = locInfo.basename;
35 e.absolutePath = locInfo.absolute;
36 e.dirName = locInfo.dirName || 'CWD';
37
38 epubs.push(e);
39
40 epubs[epubs.length - 1].on('error', function() {
41 processedCounter++;
42 emitWhenDone();
43 });
44
45 epubs[epubs.length - 1].on('finish', function() {
46 processedCounter++;
47 emitWhenDone();
48 });
49 });
50
51 this.queue = epubs;
52 this.Reader = Reader;
53 this.Editor = Editor;
54};
55
56util.inherits(Parser, EventEmitter);
57
58module.exports = Parser;