Code coverage report for lib/yuidoc.js

Statements: 70.37% (76 / 108)      Branches: 44.44% (24 / 54)      Functions: 73.68% (14 / 19)      Lines: 70.37% (76 / 108)     

All files » lib/ » yuidoc.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 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353          1                                       1                     1                                                         1             3             3             3             3               3   3 3             1               3     3 21                 3 3 3               3 3                   87     87     87     87   87 324 324   324   324 84   240       87 87                     87 87 87 240   240 240 131   131 131 131   131 131 131                   131     131                                             3     3   3   3                                       3       3   3 3 3 3   3 3 3 3           3     3 3     3                                                     3 3   3   3 3   3           3                 3 3   3          
/*
Copyright (c) 2011, Yahoo! Inc. All rights reserved.
Code licensed under the BSD License:
http://yuilibrary.com/license/
*/
var fs = require("fs"),
    rimraf = require('rimraf'),
    path = require("path");
 
/**
This is the __module__ description for the `YUIDoc` module.
 
    var options = {
        paths: [ './lib' ],
        outdir: './out'
    };
 
    var Y = require('yuidocjs');
    var json = (new Y.YUIDoc(options)).run();
 
@class YUIDoc
@main yuidoc
*/
 
 
YUI.add('yuidoc', function(Y) {
 
 
    /**
     * The default list of configuration options
     * @property OPTIONS
     * @type Object
     * @final
     * @for YUIDoc
     */
 
    var OPTIONS = {
        quiet: false,
        writeJSON: true,
        outdir: path.join(process.cwd(),  'out'),
        extension: '.js',
        exclude: '.DS_Store,.svn,CVS,.git,build_rollup_tmp,build_tmp,node_modules',
        norecurse: false,
        version: '0.1.0',
        paths: [],
        themedir: path.join(__dirname,  'themes', 'default'),
        syntaxtype: 'js'
    };
 
    /**
     * YUIDoc main class
 
        var options = {
            paths: [ './lib' ],
            outdir: './out'
        };
 
        var Y = require('yuidoc');
        var json = (new Y.YUIDoc(options)).run();
     
     * @class YUIDoc
     * @module yuidoc
     * @constructor
     * @param config The config object
     */
    Y.YUIDoc = function(config) {
        /**
         * Holds the number of files that we are processing.
         * @property filecount
         * @type Boolean
         * @private
         */
        this.filecount = 0;
        /**
         * Hash map of dirnames to selleck config options.
         * @property selleck
         * @type Object
         * @private
         */
        this.selleck = {};
        /**
         * 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 = Y.merge(OPTIONS, config);
 
        Eif (this.options.quiet) {
            Y.applyConfig({
                debug: false
            });
        }
 
    };
 
    Y.YUIDoc.prototype = {
        /**
         * Always exclude these directories
         * @method _setDefaultExcludes
         * @private
         */
        _setDefaultExcludes: function() {
            //These should always be excluded
            var excludes = '.DS_Store,.svn,CVS,.git,build_rollup_tmp,build_tmp,node_modules'.split(','),
                self = this;
 
            excludes.forEach(function(item) {
                self.options.excludes[item] = true;
            });
        },
        /**
         * Does post process on self.options.
         * @method _processConfig
         * @private
         */
        _processConfig: function() {
            this.options.extensions = Y.Array.hash(this.options.extension.split(','));
            this.options.excludes = Y.Array.hash(this.options.exclude.split(','));
            this._setDefaultExcludes();
        },
        /**
         * Walks the paths and parses the directory contents
         * @method walk
         * @private
         */
        walk: function() {
            Y.each(this.options.paths, function(dir) {
                this.parsedir(dir);
            }, this);
        },
        /**
         * Walks the passed directory and grabs all the files recursively.
         * @method parsedir
         * @param {String} dir The directory to parse the contents of.
         * @private
         */
        parsedir: function(dir) {
            Iif (!Y.Files.isDirectory(dir)) {
                throw('Can not find directory: ' + dir);
            }
            var allfiles = fs.readdirSync(dir), stats,
                files = [], fullpath, self = this;
	    
            Iif (dir in self.options.excludes) {
                return;
            }
            allfiles = allfiles.sort();
 
            Y.each(allfiles, function(filename) {
                Eif (!(filename in self.options.excludes)) {
                    fullpath = path.join(dir, filename);
 
                    stats = fs.statSync(fullpath);
 
                    if (stats.isDirectory() && !self.options.norecurse) {
                        self.parsedir(fullpath);
                    } else {
                        files.push(filename);
                    }
                }
            });
            Eif (!(dir in self.options.excludes)) {
                this.parsefiles(dir, files);
            }
        },
        /**
         * Gathers all the file data and populates the filemap and dirmap hashes.
         * @method parsefiles
         * @param {String} dir The directory to start from.
         * @param {Array} files List of files to parse.
         * @private
         */
        parsefiles: function(dir, files) {
            var self = this;
	    files = files.sort();
            Y.each(files, function(filename) {
                var ext = path.extname(filename), text, fullpath;
 
                Eif (ext) {
                    if (ext in self.options.extensions) {
                        fullpath = path.join(dir, filename);
 
                        Eif (Y.Files.exists(fullpath)) {
                            self.filecount++;
                            text = fs.readFileSync(fullpath, Y.charset);
 
                            self.filemap[fullpath] = text;
                            self.dirmap[fullpath] = dir;
                            self.getSelleck(fullpath);
 
                        } else {
                            Y.log('File skipped: ' + fullpath, 'warn', 'yuidoc');
                        }
                    }
                }
            });
        },
        getSelleck: function(fullpath) {
            var self = this,
                base, comp, json;
 
            Iif (self.options.selleck) {
                base = path.dirname(fullpath);
                comp = path.join(base, '../', 'docs', 'component.json');
                //Y.log('Checking for Selleck data: ' + comp, 'info', 'yuidoc');
                if (Y.Files.exists(comp)) {
                    try {
                        var json = JSON.parse(fs.readFileSync(comp, 'utf8'));
                        delete json.examples; //Remove the selleck example data, we only want the comp info
                        self.selleck[fullpath] = json;
                    } catch (e) {
                        Y.log('JSON parse failed on Selleck component.json file: ' + comp, 'error', 'yuidoc');
                    }
                }
            }
        },
        /**
         * Writes the parser JSON data to disk.
         * @method writeJSON
         * @param {Object} parser The DocParser instance to use
         * @private
         * @return {Object} The JSON data returned from the DocParser
         */
        writeJSON: function(parser) {
            var self = this,
            data;
 
            data = parser.data;
 
            data.warnings = parser.warnings;
 
            Iif (self.selleck && self.options.selleck && data.files && data.modules) {
                Object.keys(self.selleck).forEach(function(file) {
                    Object.keys(data.files).forEach(function(f) {
                        if (file === f) {
                            var mods = data.files[f].modules;
                            if (mods) {
                                Object.keys(mods).forEach(function(mod) {
                                    if (data.modules[mod]) {
                                        if (!data.modules[mod].extra) {
                                            data.modules[mod].extra = {};
                                        }
                                        data.modules[mod].extra.selleck = self.selleck[file];
                                    }
                                });
                            }
                        }
                    });
                });
            }
 
            Iif (self.options.project) {
                parser.data.project = self.options.project;
            }
 
            Eif (self.options.writeJSON) {
                // Y.log(Y.JSON.stringify(parser.data, null, 4));
                var file = path.join(self.options.outdir, 'data.json'), out;
                Eif (Y.Files.exists(self.options.outdir) && !self.options.nodeleteout) {
                    Y.log('Found out dir, deleting: ' + self.options.outdir, 'warn', 'yuidoc');
                    rimraf.sync(self.options.outdir);
                }
                Eif (!Y.Files.exists(self.options.outdir)) {
                    Y.log('Making out dir: ' + self.options.outdir, 'info', 'yuidoc');
                    try {
                        fs.mkdirSync(self.options.outdir, 0777);
                    } catch (e) {
                        Y.log('Outdir creation failed', 'warn', 'yuidoc');
                    }
                }
                
                out = fs.createWriteStream(file, {
                        flags: "w", encoding: Y.charset, mode: 0644
                });
                out.write(JSON.stringify(data, null, 4));
                out.end();
            }
 
            return data;
        },
        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.exit(code);
        },
        /**
         * Process the config, walk the file tree and write out the JSON data.
         * @method run
         * @return {Object} The JSON data returned from the DocParser
         */
        run: function() {
            /**
             * Timestamp holder so we know when YUIDoc started the parse process.
             * @property starttime
             * @type Timestamp
             */
            Y.log('YUIDoc Starting from: ' + this.options.paths.join(','), 'info', 'yuidoc');
            this.starttime = new Date().getTime();
 
            var self = this;
 
            this._processConfig();
            this.walk();
 
            var json = this.writeJSON(new Y.DocParser({
                syntaxtype: self.options.syntaxtype,
                filemap: self.filemap,
                dirmap: self.dirmap
            }).parse());
 
            Iif (this.options.lint) {
                this.lint(json.warnings);
            }
 
            /**
             * Timestamp holder so we know when YUIDoc has finished the parse process.
             * @property endtime
             * @type Timestamp
             */
            this.endtime = new Date().getTime();
            Y.log('Parsed ' + this.filecount + ' files in ' + ((this.endtime - this.starttime) / 1000) + ' seconds', 'info', 'yuidoc');
 
            return json;
        }
    };
 
});