index.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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;