Code coverage report for master/lib/jsdoc/util/runtime.js

Statements: 69.77% (30 / 43)      Branches: 53.33% (8 / 15)      Functions: 75% (6 / 8)      Lines: 69.77% (30 / 43)      Ignored: none     

All files » master/lib/jsdoc/util/ » runtime.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                  1 1       1   1                   1 1     1 1                     1 168             1 1     1                       1 1 1   1 1     1       1 1 1     1 1         1 1                       1                     1 1   1    
/*global java */
/**
 * Helper functions to enable JSDoc to run on multiple JavaScript runtimes.
 *
 * @module jsdoc/util/runtime
 * @private
 */
'use strict';
 
var env = require('jsdoc/env');
var os = require('os');
 
// These strings represent directory names; do not modify them!
/** @private */
var RHINO = exports.RHINO = 'rhino';
/** @private */
var NODE = exports.NODE = 'node';
 
/**
 * The JavaScript runtime that is executing JSDoc:
 *
 * + `module:jsdoc/util/runtime~RHINO`: Mozilla Rhino.
 * + `module:jsdoc/util/runtime~NODE`: Node.js.
 *
 * @private
 */
var runtime = (function() {
    Iif (global.Packages && typeof global.Packages === 'object' &&
        Object.prototype.toString.call(global.Packages) === '[object JavaPackage]') {
        return RHINO;
    } else Eif (require && require.main && module) {
        return NODE;
    } else {
        // unknown runtime
        throw new Error('Unable to identify the current JavaScript runtime.');
    }
})();
 
/**
 * Check whether Mozilla Rhino is running JSDoc.
 * @return {boolean} Set to `true` if the current runtime is Mozilla Rhino.
 */
exports.isRhino = function() {
    return runtime === RHINO;
};
 
/**
 * Check whether Node.js is running JSDoc.
 * @return {boolean} Set to `true` if the current runtime is Node.js.
 */
exports.isNode = function() {
    return runtime === NODE;
};
 
function initializeRhino(args) {
    // the JSDoc dirname is the main module URI, minus the filename, converted to a path
    var uriParts = require.main.uri.split('/');
    uriParts.pop();
 
    env.dirname = String( new java.io.File(new java.net.URI(uriParts.join('/'))) );
    env.pwd = String( java.lang.System.getenv().get('PWD') );
    env.args = args;
 
    require(env.dirname + '/rhino/rhino-shim.js');
}
 
function initializeNode(args) {
    var fs = require('fs');
    var path = require('path');
 
    var jsdocPath = args[0];
    var pwd = args[1];
 
    // resolve the path if it's a symlink
    Iif ( fs.statSync(jsdocPath).isSymbolicLink() ) {
        jsdocPath = path.resolve( path.dirname(jsdocPath), fs.readlinkSync(jsdocPath) );
    }
 
    env.dirname = jsdocPath;
    env.pwd = pwd;
    env.args = process.argv.slice(2);
}
 
exports.initialize = function(args) {
    switch (runtime) {
        case RHINO:
            initializeRhino(args);
            break;
        case NODE:
            initializeNode(args);
            break;
        default:
            throw new Error('Cannot initialize the unknown JavaScript runtime "' + runtime + '"!');
    }
};
 
/**
 * Retrieve the identifier for the current JavaScript runtime.
 *
 * @private
 * @return {string} The runtime identifier.
 */
exports.getRuntime = function() {
    return runtime;
};
 
/**
 * Get the require path for the runtime-specific implementation of a module.
 *
 * @param {string} partialPath - The partial path to the module. Use the same format as when calling
 * `require()`.
 * @return {object} The require path for the runtime-specific implementation of the module.
 */
exports.getModulePath = function(partialPath) {
    var path = require('path');
 
    return path.join(env.dirname, runtime, partialPath);
};