paths.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. var fs = require('fs'),
  2. path = require('path');
  3. exports.promiseFiles = function promiseFiles(dir, type, options){
  4. type = type || 'file'
  5. var processor = function(res,rej){
  6. var cb = function(err,data){
  7. if(err)return rej(err)
  8. res(data)
  9. }
  10. exports.files(dir,type,cb,options)
  11. }
  12. return new Promise(processor)
  13. }
  14. /**
  15. * find all files or subdirs (recursive) and pass to callback fn
  16. *
  17. * @param {string} dir directory in which to recurse files or subdirs
  18. * @param {string} type type of dir entry to recurse ('file', 'dir', or 'all', defaults to 'file')
  19. * @param {function(error, <Array.<string>)} callback fn to call when done
  20. * @example
  21. * dir.files(__dirname, function(err, files) {
  22. * if (err) throw err;
  23. * console.log('files:', files);
  24. * });
  25. */
  26. exports.files = function files(dir, type, callback, options) {
  27. var ofType = typeof type
  28. if(ofType == 'object'){
  29. options = options || type
  30. type = 'file'
  31. callback = function(){}
  32. }else if (ofType !== 'string') {
  33. //ignoreType = callback;
  34. callback = type;
  35. type = 'file';
  36. }
  37. options = options || {}
  38. var pending,
  39. results = {
  40. files: [],
  41. dirs: []
  42. };
  43. var done = function() {
  44. if(type==='combine'){
  45. results = results.files.concat(results.dirs)
  46. } else if (!type || options.ignoreType || ['all','combine'].indexOf(type)>=0) {
  47. results = results
  48. } else {
  49. results = results[type + 's']
  50. }
  51. if(options.sync)return;
  52. callback(null, results);
  53. };
  54. var getStatHandler = function(statPath, name, lstatCalled) {
  55. return function(err, stat) {
  56. if (err) {
  57. if (!lstatCalled) {
  58. return fs.lstat(statPath, getStatHandler(statPath, name, true));
  59. }
  60. return callback(err);
  61. }
  62. var pushVal = options.shortName ? name : statPath
  63. if (stat && stat.isDirectory() && stat.mode !== 17115) {
  64. if (type !== 'file') {
  65. results.dirs.push(pushVal);
  66. }
  67. if (options.recursive==null || options.recursive) {
  68. var subloop = function(err, res) {
  69. if (err){
  70. return callback(err)
  71. }
  72. if(type === 'combine'){
  73. results.files = results.files.concat(res);
  74. }else if (type === 'all') {
  75. results.files = results.files.concat(res.files);
  76. results.dirs = results.dirs.concat(res.dirs);
  77. } else if (type === 'file') {
  78. results.files = results.files.concat(res.files);
  79. } else {
  80. results.dirs = results.dirs.concat(res.dirs);
  81. }
  82. if (!--pending){
  83. done();
  84. }
  85. }
  86. var newOptions = Object.assign({}, options)
  87. newOptions.ignoreType = true
  88. var moreResults = files(statPath, type, subloop, newOptions);
  89. if(options.sync){
  90. subloop(null, moreResults)
  91. }
  92. }else if (!--pending){
  93. done()
  94. }
  95. } else {
  96. if (type !== 'dir') {
  97. results.files.push(pushVal);
  98. }
  99. // should be the last statement in statHandler
  100. if (!--pending){
  101. done()
  102. }
  103. }
  104. }
  105. }
  106. var bufdir = Buffer.from(dir);
  107. const onDirRead = function(err, list) {
  108. if (err) return callback(err);
  109. pending = list.length;
  110. if (!pending) return done();
  111. for (var file, i = 0, l = list.length; i < l; i++) {
  112. var fname = list[i].toString();
  113. file = path.join(dir, fname);
  114. var buffile = Buffer.concat([bufdir, Buffer.from(path.sep), list[i]]);
  115. if(options.sync){
  116. var res = fs.statSync(buffile);
  117. getStatHandler(file,fname)(null, res)
  118. }else{
  119. fs.stat(buffile, getStatHandler(file,fname));
  120. }
  121. }
  122. return results
  123. }
  124. const onStat = function(err, stat) {
  125. if (err) return callback(err);
  126. if (stat && stat.mode === 17115) return done();
  127. if(options.sync){
  128. const list = fs.readdirSync(bufdir, {encoding: 'buffer'})
  129. return onDirRead(null, list)
  130. }else{
  131. fs.readdir(bufdir, {encoding: 'buffer'}, onDirRead)
  132. }
  133. }
  134. if(options.sync){
  135. const stat = fs.statSync(bufdir);
  136. return onStat(null, stat)
  137. }else{
  138. fs.stat(bufdir, onStat);
  139. }
  140. };
  141. /**
  142. * find all files and subdirs in a directory (recursive) and pass them to callback fn
  143. *
  144. * @param {string} dir directory in which to recurse files or subdirs
  145. * @param {boolean} combine whether to combine both subdirs and filepaths into one array (default false)
  146. * @param {function(error, Object.<<Array.<string>, Array.<string>>)} callback fn to call when done
  147. * @example
  148. * dir.paths(__dirname, function (err, paths) {
  149. * if (err) throw err;
  150. * console.log('files:', paths.files);
  151. * console.log('subdirs:', paths.dirs);
  152. * });
  153. * dir.paths(__dirname, true, function (err, paths) {
  154. * if (err) throw err;
  155. * console.log('paths:', paths);
  156. * });
  157. */
  158. exports.paths = function paths(dir, combine, callback) {
  159. var type;
  160. if (typeof combine === 'function') {
  161. callback = combine;
  162. combine = false;
  163. }
  164. exports.files(dir, 'all', function(err, results) {
  165. if (err) return callback(err);
  166. if (combine) {
  167. callback(null, results.files.concat(results.dirs));
  168. } else {
  169. callback(null, results);
  170. }
  171. });
  172. };
  173. /**
  174. * find all subdirs (recursive) of a directory and pass them to callback fn
  175. *
  176. * @param {string} dir directory in which to find subdirs
  177. * @param {string} type type of dir entry to recurse ('file' or 'dir', defaults to 'file')
  178. * @param {function(error, <Array.<string>)} callback fn to call when done
  179. * @example
  180. * dir.subdirs(__dirname, function (err, paths) {
  181. * if (err) throw err;
  182. * console.log('files:', paths.files);
  183. * console.log('subdirs:', paths.dirs);
  184. * });
  185. */
  186. exports.subdirs = function subdirs(dir, callback, type, options) {
  187. options = options || {}
  188. const iCallback = function(err, subdirs) {
  189. if (err) return callback(err);
  190. if(type=='combine'){
  191. subdirs = subdirs.files.concat(subdirs.dirs)
  192. }
  193. if(options.sync)return subdirs
  194. callback(null, subdirs);
  195. }
  196. const res = exports.files(dir, 'dir', iCallback, options)
  197. if(options && options.sync){
  198. return iCallback(null,res)
  199. }
  200. };