-
Notifications
You must be signed in to change notification settings - Fork 30
/
index.js
142 lines (110 loc) · 3.72 KB
/
index.js
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
(function(){
'use strict';
var through2 = require('through2'),
path = require('path'),
gutil = require('gulp-util');
var File = gutil.File;
var PluginError = gutil.PluginError;
var marked = require('marked');
var Parser = require('./lib/parser.js');
var Generator = require('./lib/generator.js');
/**
* That's the plugin parser
*/
var streamParser = function (infos, name) {
name = name || 'jsdoc.json';
var firstFile = null;
var readme = null;
var wp = new Parser(infos);
var bufferFiles = function(file, enc, next){
if (file.isNull()) return; // ignore
if (file.isStream()) return this.emit('error', new PluginError('gulp-jsdoc', 'Streaming not supported'));
// Store firstFile to get a base and cwd later on
if (!firstFile)
firstFile = file;
if (/[.]js$/i.test(file.path))
wp.parse(file);
else if(/readme(?:[.]md)?$/i.test(file.path))
readme = marked(file.contents.toString('utf8'));
next();
};
var endStream = function(conclude){
// Nothing? Exit right away
if (!firstFile){
conclude();
return;
}
var data;
try{
data = JSON.stringify(wp.complete(), null, 2);
// data = parser(options, filemap));
}catch(e){
return this.emit('error', new PluginError('gulp-jsdoc',
'Oooooh! Failed parsing with jsdoc. What did you do?! ' + e));
}
// Pump-up the generated output
var vinyl = new File({
cwd: firstFile.cwd,
base: firstFile.base,
path: path.join(firstFile.base, name),
contents: new Buffer(data)
});
// Possibly stack-up the readme, if there was any in the first place
vinyl.readme = readme;
// Add that to the stream...
this.push(vinyl);
conclude();
};
// That's it for the parser
return through2.obj(bufferFiles, endStream);
};
var streamGenerator = function(destination, template, options){
var gp = new Generator(destination, template, options);
var processor = function(file, enc, next){
if (file.isNull()) return; // ignore
if (file.isStream()) return this.emit('error', new PluginError('gulp-jsdoc', 'Streaming not supported'));
try{
gp.render(file);
}catch(e){
return this.emit('error', new PluginError('gulp-jsdoc',
'Oooooh! Failed rendering with jsdoc. What did you do?! ' + e));
}
next();
};
return through2.obj(processor);
};
// // Wrap reporter helper
// var wrapReporter = function(reporter){
// return function(options){
// return through2.obj(function(file, enc, next){
// var warnings = JSON.parse(file.contents.toString('utf8')).warnings;
// if(warnings && warnings.length){
// // Don't trust the (yahoo) reporter too much
// try{
// reporter(warnings, options);
// }catch(e){
// return this.emit('error', new PluginError('gulp-jsdoc', 'Reporter crashed!' + e));
// }
// }
// this.push(file);
// next();
// });
// };
// };
var jsdoc = function(destination, template, infos, buildOptions){
return gutil.combine(
jsdoc.parser(infos),
// jsdoc.reporter(),
jsdoc.generator(destination, template, buildOptions)
)();
};
// // Yui default, provided reporter
// jsdoc.yuiReporter = wrapReporter(require('./lib/uglyreporter'));
// // Our own reporter
// jsdoc.chalkReporter = wrapReporter(require('./lib/chalkreporter'));
// // Default to chalk, nicier :)
// jsdoc.reporter = jsdoc.chalkReporter;
jsdoc.generator = streamGenerator;
jsdoc.parser = streamParser;
module.exports = jsdoc;
}());