UNPKG

4.05 kBJavaScriptView Raw
1#!/usr/bin/env node
2/*global arguments, require: true */
3/**
4 * @project jsdoc
5 * @author Michael Mathews <micmath@gmail.com>
6 * @license See LICENSE.md file included in this distribution.
7 */
8
9// initialize the environment for the current JavaScript VM
10(function(args) {
11 'use strict';
12
13 var path;
14
15 if (args[0] && typeof args[0] === 'object') {
16 // we should be on Node.js
17 args = [__dirname, process.cwd()];
18 path = require('path');
19
20 // Create a custom require method that adds `lib/jsdoc` and `node_modules` to the module
21 // lookup path. This makes it possible to `require('jsdoc/foo')` from external templates and
22 // plugins, and within JSDoc itself. It also allows external templates and plugins to
23 // require JSDoc's module dependencies without installing them locally.
24 require = require('requizzle')({
25 requirePaths: {
26 before: [path.join(__dirname, 'lib')],
27 after: [path.join(__dirname, 'node_modules')]
28 },
29 infect: true
30 });
31 }
32
33 require('./lib/jsdoc/util/runtime').initialize(args);
34})( Array.prototype.slice.call(arguments, 0) );
35
36/**
37 * Data about the environment in which JSDoc is running, including the configuration settings that
38 * were used to run JSDoc.
39 *
40 * @deprecated As of JSDoc 3.4.0. Use `require('jsdoc/env')` to access the `env` object. The global
41 * `env` object will be removed in a future release.
42 * @namespace
43 * @name env
44 */
45global.env = (function() {
46 'use strict';
47
48 // This bit of joy is here because Rhino treats `./lib/jsdoc/env` and `jsdoc/env` as separate
49 // modules. In contrast, Node.js errors out on `jsdoc/env` because we don't patch `require()`
50 // until after this file is loaded.
51 if (require('./lib/jsdoc/util/runtime').isRhino()) {
52 return require('jsdoc/env');
53 }
54 else {
55 return require('./lib/jsdoc/env');
56 }
57})();
58
59/**
60 * Data that must be shared across the entire application.
61 *
62 * @deprecated As of JSDoc 3.4.0. Avoid using the `app` object. The global `app` object and the
63 * `jsdoc/app` module will be removed in a future release.
64 * @namespace
65 * @name app
66 */
67global.app = (function() {
68 'use strict';
69
70 // See comment in `global.env` to find out why we jump through this hoop.
71 if (require('./lib/jsdoc/util/runtime').isRhino()) {
72 return require('jsdoc/app');
73 }
74 else {
75 return require('./lib/jsdoc/app');
76 }
77})();
78
79(function() {
80 'use strict';
81
82 var env = global.env;
83 var logger = require('./lib/jsdoc/util/logger');
84 var runtime = require('./lib/jsdoc/util/runtime');
85 var cli = require('./cli');
86
87 function cb(errorCode) {
88 cli.logFinish();
89 cli.exit(errorCode || 0);
90 }
91
92 cli.setVersionInfo()
93 .loadConfig();
94
95 if (!env.opts.test) {
96 cli.configureLogger();
97 }
98
99 cli.logStart();
100
101 if (env.opts.debug) {
102 /**
103 * Recursively print an object's properties to stdout. This method is safe to use with
104 * objects that contain circular references. In addition, on Mozilla Rhino, this method is
105 * safe to use with native Java objects.
106 *
107 * This method is available only when JSDoc is run with the `--debug` option.
108 *
109 * @global
110 * @name dump
111 * @private
112 * @param {...*} obj - Object(s) to print to stdout.
113 */
114 global.dump = function() {
115 console.log(require('./lib/jsdoc/util/dumper').dump(arguments));
116 };
117 }
118
119 // On Rhino, we use a try/catch block so we can log the Java exception (if available)
120 if ( runtime.isRhino() ) {
121 try {
122 cli.runCommand(cb);
123 }
124 catch(e) {
125 if (e.rhinoException) {
126 logger.fatal( e.rhinoException.printStackTrace() );
127 } else {
128 console.trace(e);
129 cli.exit(1);
130 }
131 }
132 }
133 else {
134 cli.runCommand(cb);
135 }
136})();