-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReader.js
More file actions
87 lines (74 loc) · 2.07 KB
/
Copy pathReader.js
File metadata and controls
87 lines (74 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
'use strict';
/*
global module,
require
*/
const AbstractModule = require( '../AbstractModule' ),
SourceFile = require( '../document/SourceFile' );
/**
* A reader is a class that reads input origins and provides them as strings. Simple as that.
*
* @property {string|Array} origins source origins
* @abstract
* @class
* @extends AbstractModule
*/
class Reader extends AbstractModule {
/**
* creates a new Reader instance
*
* @param {string|Array} origins source origins
* @param {Document} document document instance
* @param {object} [options] reader options. these will be merged with the defaults
*
* @constructor
*/
constructor ( origins, document, options = {} ) {
super( options );
/**
* holds the source origin(s)
*
* @type {string|Array}
*/
this.origins = origins;
/**
* holds the documentation object
*
* @type {Document}
*/
this.document = document;
this.emit( 'init', { reader: this } );
}
/**
* Reads origin data sources and returns an object that maps identifiers to source code. For the
* FileSystemReader, that means filesystem path mapped to the file content.
*
* @return {Promise.<Array>}
*/
read () {
this.emit( 'before', { reader: this } );
// This is why it is important for readers to return a promise: All error handling is performed
// in the base class to prevent implementations from having to emit events or handle errors.
return this._invoke()
.then( results => {
for ( let file in results ) {
if ( results.hasOwnProperty( file ) ) {
this.addSourceFile( file, results[ file ] );
}
}
this.emit( 'after', {
reader: this,
results: results
} );
return results;
} )
.catch( error => this.emit( 'error', {
reader: this,
error: error
} ) );
}
addSourceFile ( name, content = '' ) {
this.document.sources.appendChild( new SourceFile( name, content ) );
}
}
module.exports = Reader;