index.mjs 556 B

12345678910111213141516171819202122
  1. import { join, resolve } from 'path';
  2. import { readdir, stat } from 'fs';
  3. import { promisify } from 'util';
  4. const toStats = promisify(stat);
  5. const toRead = promisify(readdir);
  6. export default 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. }