index.js 989 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. 'use strict';
  2. const fs = require('fs');
  3. const pify = require('pify');
  4. function type(fn, fn2, fp) {
  5. if (typeof fp !== 'string') {
  6. return Promise.reject(new TypeError(`Expected a string, got ${typeof fp}`));
  7. }
  8. return pify(fs[fn])(fp)
  9. .then(stats => stats[fn2]())
  10. .catch(err => {
  11. if (err.code === 'ENOENT') {
  12. return false;
  13. }
  14. throw err;
  15. });
  16. }
  17. function typeSync(fn, fn2, fp) {
  18. if (typeof fp !== 'string') {
  19. throw new TypeError(`Expected a string, got ${typeof fp}`);
  20. }
  21. try {
  22. return fs[fn](fp)[fn2]();
  23. } catch (err) {
  24. if (err.code === 'ENOENT') {
  25. return false;
  26. }
  27. throw err;
  28. }
  29. }
  30. exports.file = type.bind(null, 'stat', 'isFile');
  31. exports.dir = type.bind(null, 'stat', 'isDirectory');
  32. exports.symlink = type.bind(null, 'lstat', 'isSymbolicLink');
  33. exports.fileSync = typeSync.bind(null, 'statSync', 'isFile');
  34. exports.dirSync = typeSync.bind(null, 'statSync', 'isDirectory');
  35. exports.symlinkSync = typeSync.bind(null, 'lstatSync', 'isSymbolicLink');