Code coverage report for lib/firedoc.js

Statements: 46.48% (33 / 71)      Branches: 36.11% (13 / 36)      Functions: 12.5% (1 / 8)      Lines: 47.06% (32 / 68)      Ignored: none     

All files » lib/ » firedoc.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                                1 1 1 1 1                 1 2                   2             2             2               2                                         2 2 2     2 2     2       2 2     2 2     2 2 1             2 4       2     2     2 1     1   1                                                                                                                                              
 
/**
 * This is the __module__ description for the `YUIDoc` module.
 * ```
 * var options = {
 *   paths: [ './lib' ],
 *   outdir: './out'
 * };
 * ```
 *
 * @module firedoc
 * @submodule helpers
 * @submodule utils
 * @main firedoc
 */
 
const _ = require('underscore');
const path = require('path');
const fs = require('graceful-fs');
const readdirp = require('readdirp');
const debug = require('debug')('firedoc');
 
/**
 * Firedoc main class
 *
 * @class Firedoc
 * @constructor
 * @param config The config object
 */
function Firedoc (config) {
  Iif (!(this instanceof Firedoc)) {
    return new Firedoc(config);
  }
 
  /**
   * Holds the number of files that we are processing.
   * @property filecount
   * @type Boolean
   * @private
   */
  this.filecount = 0;
  /**
   * Holder for the list of files we are processing.
   * @property filemap
   * @type Object
   * @private
   */
  this.filemap = {};
  /**
   * Holder for the list of directories we are processing.
   * @property dirmap
   * @type Object
   * @private
   */
  this.dirmap = {};
 
  /**
   * Internal holder for configuration options.
   * @property options
   * @type Object
   * @private
   */
  this.options = {
    writeJSON: true,
    extensions: '.js',
    excludes: [
      '.git',
      '.svn',
      'CVS',
      'build_rollup_tmp',
      'build_tmp',
      'node_modules'
    ],
    norecurse: false,
    path: './',
    cwd: process.cwd(),
    http: false,
    dest: path.join(process.cwd(), 'out'),
    theme: path.join(__dirname, '../themes/default'),
    syntaxtype: 'js'
  };
 
  // setup options
  var cwd = config.cwd || this.options.cwd;
  Eif (fs.existsSync(cwd + '/package.json')) {
    var pkg = require(cwd + '/package.json');
 
    // load firedoc options
    var firedocOption = pkg.firedoc;
    delete pkg.firedoc;
 
    //compatible with firedoc.options
    Iif (pkg.yuidoc && pkg.yuidoc.options) {
      firedocOption = pkg.yuidoc.options;
      delete pkg.yuidoc;
    }
    this.options.project = pkg;
    this.options = _.extend(this.options, firedocOption);
 
    // compatible with the old options
    this.options.dest = this.options.outdir;
    this.options.path = this.options.paths && this.options.paths[0];
 
    // extend by NODE_ENV
    var envOption = pkg['firedoc.' + process.env.NODE_ENV];
    if (envOption) {
      this.options = _.extend(this.options, envOption);
    }
  } else {
    this.options.path = config.path || this.options.path;
  }
 
  // setup options from config
  for (var key in config) {
    Eif (!_.isUndefined(config[key])) this.options[key] = config[key];
  }
 
  // if `markdown` enabled, set theme
  Iif (this.options.markdown === true) {
    this.options.theme = path.join(__dirname, '../themes/markdown');
  }
  Iif (!path.isAbsolute(this.options.path)) {
    this.options.path = path.join(process.cwd(), this.options.path);
  }
  Eif (!path.isAbsolute(this.options.dest)) {
    this.options.dest = path.join(process.cwd(), this.options.dest);
  }
};
exports.Firedoc = Firedoc;
 
Firedoc.prototype = {
 
  /**
   * Walks the paths and parses the directory contents
   *
   * @method walk
   * @private
   */
  walk: function (callback) {
    var self = this;
    readdirp(
      {
        root: this.options.path,
        fileFilter: '*.@(js|rs|ts|coffee)',
        directoryFilter: self.options.excludes.map(
          function (ex) {
            return '!' + ex;
          }
        )
      }
    ).on('data', function (entry) {
      var text = fs.readFileSync(entry.fullPath, 'utf8');
      self.filecount += 1;
      self.filemap[entry.fullPath] = text.replace(/\r?\n|\r/g, '\n');
      self.dirmap[entry.fullPath] = entry.fullParentDir;
    }).on('end', callback);
  },
 
  lint: function (warnings) {
    var code = 0, count = 0;
    if (warnings && warnings.length) {
      code = 1;
      console.log('YUIDoc found', warnings.length, 'lint errors in your docs');
      warnings.forEach(function (item) {
        count++;
        console.log('#' + count, item.message, item.line + '\n');
      });
    }
  },
 
  /**
   * Process the config, walk the file tree and write out the JSON data.
   * @method build
   * @param {Function} callback
   */
  build: function (callback) {
    debug('Starting from: ' + this.options.path);
    var self = this;
    this.walk(function () {
      var parser = require('./docparser');
      var builder = require('./builder');
      var ast = parser.parse(
        self.options.syntaxtype,
        self.filemap,
        self.dirmap);
 
      debug('Parsing completed');
      if (self.options.lint) {
        debug('lint the warnings from ast');
        self.lint(ast.warnings);
        if (_.isFunction(callback)) return callback(ast.warnings);
      }
      if (self.options.parseOnly) {
        debug('skip the build because parse only');
        if (_.isFunction(callback)) callback(null, ast, self.options);
        return;
      }
      builder.compile(ast, self.options, callback);
    });
  }
};