index.js 573 B

12345678910111213141516171819202122
  1. const { join, resolve } = require('path');
  2. const { readdir, stat } = require('fs');
  3. const { promisify } = require('util');
  4. const toStats = promisify(stat);
  5. const toRead = promisify(readdir);
  6. module.exports = async function list(dir, callback, pre='') {
  7. dir = resolve('.', dir);
  8. await toRead(dir).then(arr => {
  9. return Promise.all(
  10. arr.map(str => {
  11. let abs = join(dir, str);
  12. return toStats(abs).then(stats => {
  13. return stats.isDirectory()
  14. ? list(abs, callback, join(pre, str))
  15. : callback(join(pre, str), abs, stats)
  16. });
  17. })
  18. );
  19. });
  20. }