|
1 | 1 | 'use strict' |
2 | | -var path = require('path') |
3 | | -var mm = require('micromatch') |
4 | | -var fs |
| 2 | +const path = require('path') |
| 3 | +const mm = require('micromatch') |
| 4 | +let fs |
5 | 5 | try { |
6 | 6 | fs = require('graceful-fs') |
7 | 7 | } catch (e) { |
8 | 8 | fs = require('fs') |
9 | 9 | } |
10 | 10 |
|
11 | | -function _procPath (dir, pathItem, opts, list) { |
12 | | - var nestedPath |
13 | | - var stat |
14 | | - // here since dir already resolved, we use string concatenation |
15 | | - // which showed faster performance than path.join() and path.resolve() |
16 | | - if (path.sep === '/') { |
17 | | - nestedPath = dir + '/' + pathItem |
18 | | - } else { |
19 | | - nestedPath = dir + '\\' + pathItem |
20 | | - } |
21 | | - stat = fs.lstatSync(nestedPath) |
22 | | - if (stat.isDirectory()) { |
23 | | - if (!opts.nodir) { |
24 | | - list.push({path: nestedPath, stats: stat}) |
25 | | - } |
26 | | - list = walkSync(nestedPath, opts, list) |
27 | | - } else { |
28 | | - if (!opts.nofile) { |
29 | | - list.push({path: nestedPath, stats: stat}) |
| 11 | +function klawSync (dir, opts, ls) { |
| 12 | + function procPath (pathItem) { |
| 13 | + const stat = fs.lstatSync(pathItem) |
| 14 | + const item = {path: pathItem, stats: stat} |
| 15 | + if (stat.isDirectory()) { |
| 16 | + if (!opts.nodir) { |
| 17 | + if (opts.ignore) { |
| 18 | + if (mm(pathItem, opts.ignore).length === 0) ls.push(item) |
| 19 | + } else if (opts.filter) { |
| 20 | + if (opts.filter(item)) ls.push(item) |
| 21 | + } else { |
| 22 | + ls.push(item) |
| 23 | + } |
| 24 | + } |
| 25 | + ls = klawSync(pathItem, opts, ls) |
| 26 | + } else { |
| 27 | + if (!opts.nofile) { |
| 28 | + if (opts.ignore) { |
| 29 | + if (mm(pathItem, opts.ignore).length === 0) ls.push(item) |
| 30 | + } else if (opts.filter) { |
| 31 | + if (opts.filter(item)) ls.push(item) |
| 32 | + } else { |
| 33 | + ls.push(item) |
| 34 | + } |
| 35 | + } |
30 | 36 | } |
31 | 37 | } |
32 | | -} |
33 | 38 |
|
34 | | -function walkSync (dir, opts, list) { |
35 | | - var files |
36 | | - var ignore = [] |
37 | 39 | opts = opts || {} |
38 | | - list = list || [] |
| 40 | + ls = ls || [] |
39 | 41 | dir = path.resolve(dir) |
40 | | - try { |
41 | | - files = fs.readdirSync(dir) |
42 | | - if (opts.ignore) { |
43 | | - ignore = mm(files, opts.ignore) |
44 | | - } |
45 | | - } catch (er) { |
46 | | - throw er |
47 | | - } |
48 | | - |
49 | | - for (var i = 0; i < files.length; i += 1) { |
50 | | - var file = files[i] |
51 | | - if (ignore.length > 0) { |
52 | | - if (ignore.indexOf(file) === -1) _procPath(dir, file, opts, list) |
53 | | - } else { |
54 | | - _procPath(dir, file, opts, list) |
55 | | - } |
| 42 | + const files = fs.readdirSync(dir) |
| 43 | + for (let i = 0; i < files.length; i += 1) { |
| 44 | + // here dir already resolved, we use string concatenation since |
| 45 | + // showed better performance than path.join() and path.resolve() |
| 46 | + let pathItem |
| 47 | + if (path.sep === '/') pathItem = dir + '/' + files[i] |
| 48 | + else pathItem = dir + '\\' + files[i] |
| 49 | + procPath(pathItem) |
56 | 50 | } |
57 | | - return list |
| 51 | + return ls |
58 | 52 | } |
59 | 53 |
|
60 | | -module.exports = walkSync |
| 54 | +module.exports = klawSync |
0 commit comments