forked from jsdoc/jsdoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsdoc.js
318 lines (274 loc) · 9.12 KB
/
jsdoc.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*global app: true, args: true, env: true, publish: true */
/**
* @project jsdoc
* @author Michael Mathews <micmath@gmail.com>
* @license See LICENSE.md file included in this distribution.
*/
// try: $ java -classpath build-files/java/classes/js.jar org.mozilla.javascript.tools.shell.Main main.js `pwd` script/to/parse.js
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
/**
* Data representing the environment in which this app is running.
*
* @namespace
* @name env
*/
require('lib/jsdoc/util/global').env = {
/**
* Running start and finish times.
*
* @memberof env
*/
run: {
start: new Date(),
finish: null
},
/**
* The command-line arguments passed into JSDoc.
*
* @type Array
* @memberof env
*/
args: [],
/**
* The parsed JSON data from the configuration file.
*
* @type Object
* @memberof env
*/
conf: {},
/**
* The absolute path to the base directory of the JSDoc application.
*
* @private
* @deprecated Use `__dirname` instead.
* @type string
* @memberof env
*/
dirname: '.',
/**
* The command-line arguments, parsed into a key/value hash.
*
* @type Object
* @memberof env
* @example if (env.opts.help) { console.log('Helpful message.'); }
*/
opts: {},
/**
* The source files that JSDoc will parse.
* @type Array
* @memberof env
*/
sourceFiles: [],
/**
* The JSDoc version number and revision date.
*
* @type Object
* @memberof env
*/
version: {}
};
// initialize the environment for the current JavaScript VM
(function(args) {
var vm = require('jsdoc/util/vm').vm;
// TODO: may need to move this file to support Node.js
require('initialize')[vm](args);
})( Array.prototype.slice.call(arguments, 0) );
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
/**
* Data that must be shared across the entire application.
* @namespace
* @name app
*/
require('lib/jsdoc/util/global').app = {
jsdoc: {
scanner: new (require('jsdoc/src/scanner').Scanner)(),
parser: new (require('jsdoc/src/parser').Parser)(),
name: require('jsdoc/name')
}
};
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
/**
Try to recursively print out all key/values in an object.
@global
@private
@param {Object} ... Object/s to dump out to console.
*/
function dump() {
var doop = require('jsdoc/util/doop').doop;
var _dump = require('jsdoc/util/dumper').dump;
for (var i = 0, l = arguments.length; i < l; i++) {
console.log( _dump(doop(arguments[i])) );
}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
/**
* Run the jsdoc application.
* @todo Refactor function (and require statements) into smaller functions
*/
function main() {
var _ = require('underscore');
var fs = require('jsdoc/fs');
var path = require('jsdoc/path');
var taffy = require('taffydb').taffy;
var jsdoc = {
augment: require('jsdoc/augment'),
borrow: require('jsdoc/borrow'),
Config: require('jsdoc/config'),
opts: {
args: require('jsdoc/opts/args')
},
'package': require('jsdoc/package'),
plugins: require('jsdoc/plugins'),
Readme: require('jsdoc/readme'),
src: {
filter: require('jsdoc/src/filter'),
handlers: require('jsdoc/src/handlers')
},
tutorial: {
resolver: require('jsdoc/tutorial/resolver')
},
util: {
include: require('jsdoc/util/include')
}
};
var confPath;
var defaultOpts;
var docs;
var filter;
var i;
var info;
var l;
var packageDocs;
var packageJson;
var sourceFiles;
var template;
defaultOpts = {
destination: './out/',
encoding: 'utf8'
};
// get JSDoc version number
info = JSON.parse(fs.readFileSync(path.join(__dirname, 'package.json'), 'utf8'));
env.version = {
number: info.version,
revision: new Date(parseInt(info.revision, 10)).toUTCString()
};
env.opts = jsdoc.opts.args.parse(env.args);
confPath = env.opts.configure || path.join(__dirname, 'conf.json');
if ( !fs.statSync(confPath).isFile() && !env.opts.configure ) {
confPath = path.join(__dirname, 'conf.json.EXAMPLE');
}
try {
env.conf = new jsdoc.Config( fs.readFileSync(confPath, 'utf8') )
.get();
}
catch (e) {
throw new Error('Cannot parse the config file ' + confPath + ': ' + e);
}
// look for options on the command line, in the config file, and in the defaults, in that order
env.opts = _.defaults(env.opts, env.conf.opts, defaultOpts);
if (env.opts.help) {
console.log( jsdoc.opts.args.help() );
process.exit(0);
} else if (env.opts.test) {
jsdoc.util.include('test/runner.js');
process.exit(0);
} else if (env.opts.version) {
console.log('JSDoc ' + env.version.number + ' (' + env.version.revision + ')');
process.exit(0);
}
if (env.conf.plugins) {
jsdoc.plugins.installPlugins(env.conf.plugins, app.jsdoc.parser);
}
if (env.conf.source && env.conf.source.include) {
env.opts._ = (env.opts._ || []).concat(env.conf.source.include);
}
// any source file named package.json or README.md is treated special
for (i = 0, l = env.opts._.length; i < l; i++ ) {
if (/\bpackage\.json$/i.test(env.opts._[i])) {
packageJson = fs.readFileSync( env.opts._[i], 'utf8' );
env.opts._.splice(i--, 1);
}
if (/(\bREADME|\.md)$/i.test(env.opts._[i])) {
env.opts.readme = new jsdoc.Readme(env.opts._[i]).html;
env.opts._.splice(i--, 1);
}
}
if (env.conf.source && env.opts._.length > 0) { // are there any files to scan and parse?
filter = new jsdoc.src.filter.Filter(env.conf.source);
env.sourceFiles = sourceFiles = app.jsdoc.scanner.scan(env.opts._,
(env.opts.recurse? 10 : undefined), filter);
jsdoc.src.handlers.attachTo(app.jsdoc.parser);
docs = app.jsdoc.parser.parse(sourceFiles, env.opts.encoding);
//The files are ALWAYS useful for the templates to have
//If there is no package.json, just create an empty package
packageDocs = new jsdoc.package.Package(packageJson);
packageDocs.files = sourceFiles || [];
docs.push(packageDocs);
jsdoc.borrow.indexAll(docs);
jsdoc.augment.addInherited(docs);
jsdoc.borrow.resolveBorrows(docs);
app.jsdoc.parser.fireProcessingComplete(docs);
if (env.opts.explain) {
dump(docs);
process.exit(0);
}
if (env.opts.tutorials) {
jsdoc.tutorial.resolver.load(env.opts.tutorials);
jsdoc.tutorial.resolver.resolve();
}
env.opts.template = (function() {
var publish = env.opts.template || 'templates/default';
// if we don't find it, keep the user-specified value so the error message is useful
return path.getResourcePath(publish) || env.opts.template;
})();
try {
template = require(env.opts.template + '/publish');
}
catch(e) {
throw new Error('Unable to load template: ' + e.message || e);
}
// templates should include a publish.js file that exports a "publish" function
if (template.publish && typeof template.publish === 'function') {
// convert this from a URI back to a path if necessary
env.opts.template = path._uriToPath(env.opts.template);
template.publish(
taffy(docs),
env.opts,
jsdoc.tutorial.resolver.root
);
}
else {
// old templates define a global "publish" function, which is deprecated
jsdoc.util.include(env.opts.template + '/publish.js');
if (publish && typeof publish === 'function') {
console.log( env.opts.template + ' uses a global "publish" function, which is ' +
'deprecated and may not be supported in future versions. ' +
'Please update the template to use "exports.publish" instead.' );
// convert this from a URI back to a path if necessary
env.opts.template = path._uriToPath(env.opts.template);
publish(
taffy(docs),
env.opts,
jsdoc.tutorial.resolver.root
);
}
else {
throw new Error( env.opts.template + ' does not export a "publish" function.' );
}
}
}
}
try {
main();
env.run.finish = new Date();
process.exit(0);
}
catch(e) {
env.run.finish = new Date();
if (e.rhinoException != null) {
e.rhinoException.printStackTrace();
process.exit(1);
} else {
throw e;
}
}