| 1672 | * @returns {void} |
| 1673 | */ |
| 1674 | function readdirRecursive(basePath, options, callback) { |
| 1675 | const context = { |
| 1676 | withFileTypes: Boolean(options.withFileTypes), |
| 1677 | encoding: options.encoding, |
| 1678 | basePath, |
| 1679 | readdirResults: [], |
| 1680 | pathsQueue: [basePath], |
| 1681 | }; |
| 1682 | |
| 1683 | let i = 0; |
| 1684 | |
| 1685 | function read(path) { |
| 1686 | const req = new FSReqCallback(); |
| 1687 | req.oncomplete = (err, result) => { |
| 1688 | if (err) { |
| 1689 | callback(err); |
| 1690 | return; |
| 1691 | } |
| 1692 | |
| 1693 | if (result === undefined) { |
| 1694 | callback(null, context.readdirResults); |
| 1695 | return; |
| 1696 | } |
| 1697 | |
| 1698 | processReaddirResult({ |
| 1699 | result, |
| 1700 | currentPath: path, |
| 1701 | context, |
| 1702 | }); |
| 1703 | |
| 1704 | if (i < context.pathsQueue.length) { |
| 1705 | read(context.pathsQueue[i++]); |
| 1706 | } else { |
| 1707 | callback(null, context.readdirResults); |
| 1708 | } |
| 1709 | }; |
| 1710 | |
| 1711 | binding.readdir( |
| 1712 | path, |
| 1713 | context.encoding, |
| 1714 | context.withFileTypes, |
| 1715 | req, |
| 1716 | ); |
| 1717 | } |
| 1718 | |
| 1719 | read(context.pathsQueue[i++]); |
| 1720 | } |
| 1721 | |
| 1722 | // Calling `readdir` with `withFileTypes=true`, the result is an array of arrays. |
| 1723 | // The first array is the names, and the second array is the types. |