paths.js 4.6 KB

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