1 | | /** |
2 | | * Core dependencies. |
3 | | */ |
4 | | |
5 | 1 | var fs = require('fs'); |
6 | 1 | var path = require('path'); |
7 | | |
8 | | /** |
9 | | * Find all files in given `location`. |
10 | | * |
11 | | * Features: |
12 | | * |
13 | | * - Recursive |
14 | | * - Sync |
15 | | * - Ignore list |
16 | | * |
17 | | * Options: |
18 | | * |
19 | | * - ext Extension |
20 | | * - ignore Patterns to ignore |
21 | | * |
22 | | * @param {String} path |
23 | | * @param {Object} options |
24 | | * @returns {Array} |
25 | | * @api public |
26 | | */ |
27 | | |
28 | | function fine(location, options) { |
29 | 42 | options = options || {}; |
30 | 42 | location = path.normalize(location); |
31 | | |
32 | 42 | var files = []; |
33 | 42 | var ext = options.ext; |
34 | 42 | var dir = isDirectory(location) ? fs.readdirSync(location) : ['']; |
35 | 41 | var ignore = options.ignore || []; |
36 | | |
37 | 41 | if (!Array.isArray(ignore)) { |
38 | 16 | ignore = [ignore]; |
39 | | } |
40 | | |
41 | 41 | dir.forEach(function(file) { |
42 | 61 | var full = path.join(location, file); |
43 | | |
44 | 61 | if (isDirectory(location) === true) { |
45 | 35 | files = files.concat(fine(full, options)); |
46 | 35 | return; |
47 | | } |
48 | | |
49 | 26 | if (ext && path.extname(full) != ext) { |
50 | 4 | return; |
51 | | } |
52 | | |
53 | 22 | for (var i = 0, len = ignore.length; i < len; i++) { |
54 | 25 | if (new RegExp('^' + ignore[i]).test(full)) return; |
55 | | } |
56 | | |
57 | 14 | files.push(full); |
58 | | }); |
59 | | |
60 | 41 | return files; |
61 | | } |
62 | | |
63 | | /** |
64 | | * Check if `path` is a directory. |
65 | | * |
66 | | * @param {String} path |
67 | | * @returns {Boolean} |
68 | | * @api private |
69 | | */ |
70 | | |
71 | | function isDirectory(path) { |
72 | 103 | return fs.statSync(path).isDirectory(); |
73 | | } |
74 | | |
75 | | /** |
76 | | * Primary export. |
77 | | */ |
78 | | |
79 | 1 | module.exports = fine; |
80 | | |