( path: string, ignores: Ignoreable[] )
| 50 | } |
| 51 | |
| 52 | export default function readdir( |
| 53 | path: string, |
| 54 | ignores: Ignoreable[] |
| 55 | ): Promise<string[]> { |
| 56 | ignores = ignores.map(toMatcherFunction); |
| 57 | |
| 58 | let list: string[] = []; |
| 59 | |
| 60 | return new Promise(function (resolve, reject) { |
| 61 | fs.readdir(path, function (err, files) { |
| 62 | if (err) { |
| 63 | return reject(err); |
| 64 | } |
| 65 | |
| 66 | let pending = files.length; |
| 67 | if (!pending) { |
| 68 | return resolve(list); |
| 69 | } |
| 70 | |
| 71 | files.forEach(function (file) { |
| 72 | const filePath = p.join(path, file); |
| 73 | fs.lstat(filePath, function (_err, stats) { |
| 74 | if (_err) { |
| 75 | return reject(_err); |
| 76 | } |
| 77 | |
| 78 | const matches = ignores.some(matcher => matcher(filePath, stats)); |
| 79 | if (matches) { |
| 80 | pending -= 1; |
| 81 | if (!pending) { |
| 82 | return resolve(list); |
| 83 | } |
| 84 | return null; |
| 85 | } |
| 86 | |
| 87 | if (stats.isDirectory()) { |
| 88 | readdir(filePath, ignores) |
| 89 | .then(function (res) { |
| 90 | if (res.length === 0) { |
| 91 | // Empty directories get returned |
| 92 | list.push(filePath); |
| 93 | } |
| 94 | list = list.concat(res); |
| 95 | pending -= 1; |
| 96 | if (!pending) { |
| 97 | return resolve(list); |
| 98 | } |
| 99 | }) |
| 100 | .catch(reject); |
| 101 | } else { |
| 102 | list.push(filePath); |
| 103 | pending -= 1; |
| 104 | if (!pending) { |
| 105 | return resolve(list); |
| 106 | } |
| 107 | } |
| 108 | }); |
| 109 | }); |
no test coverage detected