summaryrefslogtreecommitdiff
path: root/tools/eslint/lib/util/traverse.js
blob: ba5520e341bb4644219318cff42d018839699e9d (plain)
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
/**
 * @fileoverview Simple directory traversal logic.
 * @author Nicholas C. Zakas
 */

"use strict";

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

var fs = require("fs"),
    path = require("path"),
    debug = require("debug");

//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------

debug = debug("eslint:traverse");

/**
 * Walks a path recursively calling the callback on each file.
 * @param {string} name The file or directory path.
 * @param {string[]} extensions The file extensions that should cause the callback
 *      to be called.
 * @param {Function} exclude The function to check if file/path should be excluded.
 * @param {Function} callback The function to call on each file.
 * @returns {void}
 * @private
 */
function walk(name, extensions, exclude, callback) {

    var stat, basename;

    stat = fs.statSync(name);

    function traverse(dir, stack) {
        stack.push(dir);

        fs.readdirSync(path.join.apply(path, stack)).forEach(function(file) {
            var filePath, fileStat;

            // skip all hidded things (dirs, files, links)
            if (file[0] === ".") {
                return;
            }

            filePath = path.join.apply(path, stack.concat([file]));
            fileStat = fs.statSync(filePath);

            // if this file or directory is excluded from linting, skip over it.
            if (exclude && exclude(filePath)) {
                // console.log("Ignoring " + filePath);
                debug("Ignoring " + filePath);
                return;
            }

            // only call callback for files with correct extensions
            if (fileStat.isFile() && extensions.indexOf(path.extname(filePath)) > -1) {
                callback(filePath);
            } else if (fileStat.isDirectory()) {
                traverse(file, stack);
            }
        });
        stack.pop();
    }

    basename = path.basename(name);

    // don't ignore cases like 'eslint ./'
    if ((basename !== "." && basename !== ".." && basename[0] === ".") ||
        (exclude && exclude(name))) {

        debug("Ignoring " + name);
        return;
    }

    // always call callback for any files that are passed on the command line
    if (stat.isFile()) {
        callback(name);
    } else {
        traverse(name, []);
    }
}

/**
 * Traverses multiple directories and calls a callback on each file.
 * @param {Object} options The option for the traversal.
 * param {string[]} options.files An array of file and directory paths to traverse.
 * param {Function} options.exclude The function to check if file/path should be excluded.
 * @param {Function} callback A function to call for each file.
 * @returns {void}
 */
module.exports = function traverse(options, callback) {

    var files = options.files,
        exclude = options.exclude,
        extensions = options.extensions;

    files.forEach(function(file) {
        walk(file, extensions, exclude, callback);
    });

};