paths.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. var fs = require('fs'),
  2. path = require('path');
  3. /**
  4. * find all files or subdirs (recursive) and pass to callback fn
  5. *
  6. * @param {string} dir directory in which to recurse files or subdirs
  7. * @param {string} type type of dir entry to recurse ('file', 'dir', or 'all', defaults to 'file')
  8. * @param {function(error, <Array.<string>)} callback fn to call when done
  9. * @example
  10. * dir.files(__dirname, function(err, files) {
  11. * if (err) throw err;
  12. * console.log('files:', files);
  13. * });
  14. */
  15. exports.files = function files(dir, type, callback, /* used internally */ ignoreType) {
  16. var pending,
  17. results = {
  18. files: [],
  19. dirs: []
  20. };
  21. var done = function() {
  22. if (ignoreType || type === 'all') {
  23. callback(null, results);
  24. } else {
  25. callback(null, results[type + 's']);
  26. }
  27. };
  28. var getStatHandler = function(statPath) {
  29. return function(err, stat) {
  30. if (err) return callback(err);
  31. if (stat && stat.isDirectory() && stat.mode !== 17115) {
  32. if (type !== 'file') {
  33. results.dirs.push(statPath);
  34. }
  35. files(statPath, type, function(err, res) {
  36. if (err) return callback(err);
  37. if (type === 'all') {
  38. results.files = results.files.concat(res.files);
  39. results.dirs = results.dirs.concat(res.dirs);
  40. } else if (type === 'file') {
  41. results.files = results.files.concat(res.files);
  42. } else {
  43. results.dirs = results.dirs.concat(res.dirs);
  44. }
  45. if (!--pending) done();
  46. }, true);
  47. } else {
  48. if (type !== 'dir') {
  49. results.files.push(statPath);
  50. }
  51. // should be the last statement in statHandler
  52. if (!--pending) done();
  53. }
  54. };
  55. };
  56. if (typeof type !== 'string') {
  57. ignoreType = callback;
  58. callback = type;
  59. type = 'file';
  60. }
  61. if (fs.statSync(dir).mode !== 17115) {
  62. fs.readdir(dir, function(err, list) {
  63. if (err) return callback(err);
  64. pending = list.length;
  65. if (!pending) return done();
  66. for (var file, i = 0, l = list.length; i < l; i++) {
  67. file = path.join(dir, list[i]);
  68. fs.stat(file, getStatHandler(file));
  69. }
  70. });
  71. } else {
  72. return done();
  73. }
  74. };
  75. /**
  76. * find all files and subdirs in a directory (recursive) and pass them to callback fn
  77. *
  78. * @param {string} dir directory in which to recurse files or subdirs
  79. * @param {boolean} combine whether to combine both subdirs and filepaths into one array (default false)
  80. * @param {function(error, Object.<<Array.<string>, Array.<string>>)} callback fn to call when done
  81. * @example
  82. * dir.paths(__dirname, function (err, paths) {
  83. * if (err) throw err;
  84. * console.log('files:', paths.files);
  85. * console.log('subdirs:', paths.dirs);
  86. * });
  87. * dir.paths(__dirname, true, function (err, paths) {
  88. * if (err) throw err;
  89. * console.log('paths:', paths);
  90. * });
  91. */
  92. exports.paths = function paths(dir, combine, callback) {
  93. var type;
  94. if (typeof combine === 'function') {
  95. callback = combine;
  96. combine = false;
  97. }
  98. exports.files(dir, 'all', function(err, results) {
  99. if (err) return callback(err);
  100. if (combine) {
  101. callback(null, results.files.concat(results.dirs));
  102. } else {
  103. callback(null, results);
  104. }
  105. });
  106. };
  107. /**
  108. * find all subdirs (recursive) of a directory and pass them to callback fn
  109. *
  110. * @param {string} dir directory in which to find subdirs
  111. * @param {string} type type of dir entry to recurse ('file' or 'dir', defaults to 'file')
  112. * @param {function(error, <Array.<string>)} callback fn to call when done
  113. * @example
  114. * dir.subdirs(__dirname, function (err, paths) {
  115. * if (err) throw err;
  116. * console.log('files:', paths.files);
  117. * console.log('subdirs:', paths.dirs);
  118. * });
  119. */
  120. exports.subdirs = function subdirs(dir, callback) {
  121. exports.files(dir, 'dir', function(err, subdirs) {
  122. if (err) return callback(err);
  123. callback(null, subdirs);
  124. });
  125. };