Code coverage report for lib/utils.js

Statements: 36.09% (61 / 169)      Branches: 21.85% (26 / 119)      Functions: 34.62% (9 / 26)      Lines: 36.53% (61 / 167)     

All files » lib/ » utils.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 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471          1                     1   1   1                                 1 789 1594                             1                                   1 153   1                 1       1                     1 1 306 304   2   2   2 12   12 12     2   2   1                   1 153   1                       1 153       153                                                         153 153 153 153         153                               153     153 1     153       153 1     153     1             1                                                                                                                                               1                         1                                       1                   1                                   1                 1                                                                                                                                               1                   1   1978 1796 1796     1796 195       1783     1        
/*
Copyright (c) 2011, Yahoo! Inc. All rights reserved.
Code licensed under the BSD License:
http://yuilibrary.com/license/
*/
var path = require('path'),
    util = require('util'),
    minimatch = require('minimatch'),
    fs = require('graceful-fs');
 
/**
* Utilities Class
* @class Utils
* @module yuidoc
*/
 
YUI.add('utils', function(Y) {
 
    Y.charset = 'utf8';
 
var HTML_CHARS = {
    '&': '&',
    '<': '&lt;',
    '>': '&gt;',
    '"': '&quot;',
    "'": '&#x27;',
    '/': '&#x2F;',
    '`': '&#x60;'
};
 
    /**
    Escapes HTML characters in _html_.
 
    @method escapeHTML
    @param {String} html String to escape.
    @return {String} Escaped string.
    **/
    Y.escapeHTML = function(html) {
        return html.replace(/[&<>"'\/`]/g, function (match) {
            return HTML_CHARS[match];
        });
    };
 
    /**
    Normalizes the initial indentation of the given _content_ so that the first line
    is unindented, and all other lines are unindented to the same degree as the
    first line. So if the first line has four spaces at the beginning, then all
    lines will be unindented four spaces.
 
    @method unindent
    @param {String} content Text to unindent.
    @return {String} Unindented text.
    @private
    **/
    Y.unindent = function(content) {
        var indent = content.match(/^(\s+)/);
 
        if (indent) {
            content = content.replace(new RegExp('^' + indent[1], 'gm'), '');
        }
 
        return content;
    };
    
/**
Like `getPages()`, but returns only the files under the `layout/` subdirectory
of the specified _dir_.
 
@method getLayouts
@param {String} dir Directory path.
@return {Object} Mapping of layout names to layout content.
**/
function getLayouts(dir) {
    return getPages(path.join(dir, 'layouts'));
}
Y.getLayouts = getLayouts;
 
/**
Loads and returns the content of the specified page file.
 
@method getPage
@param {String} pagePath Path to a single `.handlebars` page.
@return {String|null} Page content, or `null` if not found.
**/
function getPage(pagePath) {
    if (!Y.Files.isFile(pagePath)) { return null; }
    return fs.readFileSync(pagePath, Y.charset);
}
Y.getPage = getPage;
 
/**
Loads pages (files with a `.handlebars` extension) in the specified directory and
returns an object containing a mapping of page names (the part of the filename)
preceding the `.handlebars` extension) to page content.
 
@method getPages
@param {String} dir Directory path.
@return {Object} Mapping of page names to page content.
**/
var cache = {};
function getPages(dir) {
    if (cache[dir]) {
        return cache[dir];
    }
    var pages = {};
 
    Iif (!Y.Files.isDirectory(dir)) { return pages; }
 
    fs.readdirSync(dir).forEach(function (filename) {
        var filePath = path.join(dir, filename);
 
        Eif (path.extname(filename) === '.handlebars' && Y.Files.isFile(filePath)) {
            pages[path.basename(filename, '.handlebars')] = fs.readFileSync(filePath, Y.charset);
        }
    });
    cache[dir] = pages;
 
    return pages;
}
Y.getPages = getPages;
 
/**
Like `getPages()`, but returns only the files under the `partial/` subdirectory
of the specified _dir_.
 
@method getPartials
@param {String} dir Directory path.
@return {Object} Mapping of partial names to partial content.
**/
function getPartials(dir) {
    return getPages(path.join(dir, 'partials'));
}
Y.getPartials = getPartials;
 
 
/**
Mix/merge/munge data into the template.
@method prepare
@param {String} inDir The starting directory
@param {Object} options The `options` for the meta data.
@param {callback} callback The callback to excecute when complete
@param {Error} callback.err
@param {Object} callback.options Merged options.
**/
function prepare(inDirs, options, callback) {
    var compiled = {},
        meta     = {},
        type     = 'project';
 
    Iif (options && options.skipLoad) {
        // Skip loading layouts, metadata, pages, and partials and assume that
        // the caller has provided them if they want them.
        options = Y.merge({
            layouts  : {},
            meta     : {},
            pages    : {},
            partials : {},
            viewClass: Y.DocView
        }, options);
    } else {
 
        // Gather layouts, metadata, pages, and partials from the specified
        // input directory, then merge them into the provided options (if any).
        //
        // Gathered data will override provided data if there are conflicts, in
        // order to support a use case where global data are provided by the
        // caller and overridden by more specific component-level data gathered
        // from the input directory.
        //
        // The metadata inheritance chain looks like this:
        //
        //   - override metadata specified via CLI (highest precedence)
        //   - component metadata (if this is a component)
        //   - project-level component default metadata (if specified and this is a component)
        //   - theme-level component default metadata (if specified and this is a component)
        //   - project metadata
        //   - theme metadata (lowest precedence)
 
        try {
            Eif (inDirs[0] === inDirs[1]) {
                layouts = getLayouts(inDirs[0]);
                partials = getPartials(inDirs[0]);
            } else {
                layouts = Y.merge(getLayouts(inDirs[0]), getLayouts(inDirs[1]));
                partials = Y.merge(getPartials(inDirs[0]), getPartials(inDirs[1]));
            }
            options = Y.merge(
                {viewClass: Y.DocView},
                options || {},
                {
                    layouts : layouts,
                    meta    : options.meta,
                    partials: partials
                }
            );
        } catch (ex) {
            return callback(ex);
        }
    }
 
    // Mix in the override metadata, if any. It takes precedence over everything
    // else.
    Y.mix(options.meta, options.overrideMeta);
 
    // Set a default asset path if one isn't specified in the metadata.
    if (!options.meta.projectAssets) {
        options.meta.projectAssets = options.component ? '../assets' : 'assets';
    }
 
    Iif (!options.meta.componentAssets && options.component) {
        options.meta.componentAssets = '../assets/' + options.meta.name;
    }
 
    if (typeof options.meta.layout === 'undefined') {
        options.meta.layout = options.layouts[type] ? type : 'main';
    }
    
    callback(null, options);
}
 
Y.prepare = prepare;
 
/**
* Walk the directory tree to locate the yuidoc.json file.
* @method getProjectData
* @param {Path} [dir=process.cwd()] The directory to start from
*/
var getProjectData = function(dir) {
    var dirs = [dir || process.cwd()];
	var projectData, packageData;
    var dirCount = 0;
    // keep looping until 
    //  * data is found
    //  * there are no more dirs to process
    //  * we abort due to failsafe
    while (dirs.length && !projectData) {
        if (dirCount++ > 5000) {
            Y.log('Scanned ' + dirCount + ' directories looking for a yuidoc.json file, something is probably wrong here..', 'error', 'yuidoc');
            process.exit(1);
        }
        // accumulator for directories at this level
        var childDirs = [];
        // for each directory at the previous level
        dirs.forEach(function(dir) {
            // abort iterating if we have project data
            if (projectData) {
                return;
            }
            // squelch (but log) any complaints about this particular directory
            try {
                // for each item in this directory
                var names = fs.readdirSync(dir);
                names.forEach(function(name) {
                    // abort iterating a folder if we have found both data
                    if (projectData && packageData) {
                        return;
                    }
                    // build a full path
                    var p = path.join(dir, name);
                    // acquire project data from this item if possible
                    if (Y.Files.isFile(p)) {
                        projectData = getFileData(p, name, 'yuidoc.json');
                        // 'package.json' is used for auxilliary configuration
                        // if it's found. Formerly, it was only found if it
                        // came _before_'yuidoc.json' in the folder tree
                        // (never in the same folder).
                        // This code will find 'package.json' in the same
                        // folder as 'yuidoc.json'.
                        // If there is no 'yuidoc.json', former algorithm would
                        // use the deepest 'package.json' it can find, this one
                        // will use the first (most shallow) one.
                        packageData = packageData || getFileData(p, name, 'package.json');
                    }
                    // if we are a folder, but not ., .., or node_modules,
                    // then add to directory accumulator
                    if (Y.Files.isDirectory(p)) {
                        if (name.indexOf('.') === 0) {
                            return;
                        }
                        if (name === 'node_modules') {
                            Y.log('Skipping node_modules directory while scanning for yuidoc.json', 'warn', 'yuidoc');
                            return;
                        }
                        childDirs.push(p);
                    }
                });
            } catch (dirPerm) {
                Y.log('Accessing dir (' + dir + ') threw an error', 'warn', 'yuidoc');
            }
        });
        // iterate over new set of folders
        dirs = childDirs;
    }
    if ((packageData && projectData) || (packageData && packageData.yuidoc)) {
        projectData = mergeData(packageData, projectData);
    }
    return projectData;
};
 
var getFileData = function(p, name, file) {
    if (name === file) {
        Y.log('Loading ' + name + ' data from: ' + p, 'info', 'yuidoc');
        try {
            return Y.Files.getJSON(p);
        } catch (e) {
            var err = 'Failed to parse ' + name + ' file, please make sure it is valid JSON';
            Y.log(err, 'error', 'yuidoc');
            throw(e+'');
        }
    }
};
 
var mergeData = function(pack, project) {
    project = project || {};
 
    if (pack.yuidoc) {
        Object.keys(pack.yuidoc).forEach(function(key) {
            if (!project[key]) {
                project[key] = pack.yuidoc[key];
            }
        });
    }
 
    ['name', 'description', 'version', 'url'].forEach(function(key) {
        if (pack[key] && !project[key]) {
            project[key] = pack[key];
        }
    });
 
    return project;
};
 
Y.getProjectData = getProjectData;
 
 
/**
* Walks the tree from this dir and returns all the subdirs
* @method getDirs
* @param {String} dir The dir to begin at
* @return {Array} The array of directories..
*/
 
var getDirs = function(dir) {
    var dirs = fs.readdirSync(dir),
        paths = [];
 
    dirs.forEach(function(d) {
        var _dir = path.join(dir, d),
            stat = fs.statSync(_dir);
 
        if (stat.isDirectory()) {
            if (_dir.indexOf('.') !== 0) {
                paths = [].concat(paths, _dir, getDirs(_dir));
            }
        }
    });
 
    return paths;
};
 
Y.getDirs = getDirs;
 
/**
* Make sure all the paths passed are directories and that they are not in the ignore list.
* @method validatePaths
* @param {Array} paths The array of paths to validate
* @param {String} [ignore=false] A string to call `.indexOf` on a path to determine if it should be ignored
*/
 
var validatePaths = function(paths, ignore) {
    var newpaths = [];
    //Shortcut the *, . & ./ shortcuts that shall globbing fixes for us
    if (paths === '*' || paths === '.' || paths === './') {
        paths = [process.cwd()];
    }
 
    // Ensure that we always have an array of some kind.
    paths = paths || [];
    if (!Y.Lang.isArray(paths)) {
        paths = [ paths ];
    }
    paths.forEach(function(path) {
        var glob = path || '';
 
        if (process.platform === 'win32') {
            glob = path.replace(/\//g, '\\\\');
        }
 
        var glob_paths = getDirs('.'), is_globbed = false;
        glob_paths.forEach(function(dir) {
            //Don't scan things in node_modules
            if (dir.indexOf('node_modules') > -1) {
                return;
            }
            if (minimatch(dir, glob, { period: true })) {
                newpaths.push(dir);
                is_globbed = true;
            }
        });
        if (!is_globbed && (Y.Files.isDirectory(glob))) {
            //If minimatch fails, check to see if it's a relative directory
            // if it is, add it directly
            newpaths.push(glob);
        }
    });
    
    paths = newpaths;
    paths.forEach(function(p) {
        try {
            if (!Y.Files.isDirectory(p)) {
                throw('Path not a directory: ' + p);
            }
        } catch (e) {}
    });
    if (!paths || !paths.forEach) {
        throw('Paths should be an array of paths');
    }
    if (ignore) {
        if (!(ignore instanceof Array)) {
            ignore = [ignore];
        }
        var p = [],
            shouldIgnore = false;
 
        paths.forEach(function(v) {
            shouldIgnore = false;
            ignore.forEach(function(i) {
                if (!shouldIgnore && v.indexOf(i) !== -1) {
                    shouldIgnore = true;
                }
            });
            if (!shouldIgnore) {
                p.push(v);
            }
        });
        paths = p;
    }
    paths = paths.sort();
    return paths;
};
 
Y.validatePaths = validatePaths;
 
 
 
/**
* Takes a type string and converts it to a "First letter upper cased" type. e.g. `(string -> String, object -> Object)`
* @method fixType
* @param {String} t The type string to convert
* @return {String} The fixed string
*/
var fixType = function(t) {
    
    if (t && t.indexOf('.') === -1) {
        t = t.replace(/{/g, '').replace(/}/g, '');
        var firstChar      = t.charAt(0),
            upperFirstChar = firstChar.toUpperCase();
 
        if (firstChar !== upperFirstChar) {
            return upperFirstChar + t.substring(1);
        }
    }
 
    return t;
};
 
Y.Lang.fixType = fixType;
 
});