index.js 416 B

123456789101112131415161718
  1. const { dirname, resolve } = require('path');
  2. const { readdirSync, statSync } = require('fs');
  3. module.exports = function (start, callback) {
  4. let dir = resolve('.', start);
  5. let tmp, stats = statSync(dir);
  6. if (!stats.isDirectory()) {
  7. dir = dirname(dir);
  8. }
  9. while (true) {
  10. tmp = callback(dir, readdirSync(dir));
  11. if (tmp) return resolve(dir, tmp);
  12. dir = dirname(tmp = dir);
  13. if (tmp === dir) break;
  14. }
  15. }