You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.1 KiB

4 years ago
  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;