index.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*!
  2. * is-directory <https://github.com/jonschlinkert/is-directory>
  3. *
  4. * Copyright (c) 2014-2015, Jon Schlinkert.
  5. * Licensed under the MIT License.
  6. */
  7. 'use strict';
  8. var fs = require('fs');
  9. /**
  10. * async
  11. */
  12. function isDirectory(filepath, cb) {
  13. if (typeof cb !== 'function') {
  14. throw new Error('expected a callback function');
  15. }
  16. if (typeof filepath !== 'string') {
  17. cb(new Error('expected filepath to be a string'));
  18. return;
  19. }
  20. fs.stat(filepath, function(err, stats) {
  21. if (err) {
  22. if (err.code === 'ENOENT') {
  23. cb(null, false);
  24. return;
  25. }
  26. cb(err);
  27. return;
  28. }
  29. cb(null, stats.isDirectory());
  30. });
  31. }
  32. /**
  33. * sync
  34. */
  35. isDirectory.sync = function isDirectorySync(filepath) {
  36. if (typeof filepath !== 'string') {
  37. throw new Error('expected filepath to be a string');
  38. }
  39. try {
  40. var stat = fs.statSync(filepath);
  41. return stat.isDirectory();
  42. } catch (err) {
  43. if (err.code === 'ENOENT') {
  44. return false;
  45. } else {
  46. throw err;
  47. }
  48. }
  49. return false;
  50. };
  51. /**
  52. * Expose `isDirectory`
  53. */
  54. module.exports = isDirectory;