index.js 799 B

1234567891011121314151617181920212223242526
  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).then(stats => stats[fn2]());
  9. }
  10. function typeSync(fn, fn2, fp) {
  11. if (typeof fp !== 'string') {
  12. throw new TypeError(`Expected a string, got ${typeof fp}`);
  13. }
  14. return fs[fn](fp)[fn2]();
  15. }
  16. exports.file = type.bind(null, 'stat', 'isFile');
  17. exports.dir = type.bind(null, 'stat', 'isDirectory');
  18. exports.symlink = type.bind(null, 'lstat', 'isSymbolicLink');
  19. exports.fileSync = typeSync.bind(null, 'statSync', 'isFile');
  20. exports.dirSync = typeSync.bind(null, 'statSync', 'isDirectory');
  21. exports.symlinkSync = typeSync.bind(null, 'lstatSync', 'isSymbolicLink');