Repositorio del curso CCOM4030 el semestre B91 del proyecto Paz para la Mujer

find.js 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. var fs = require('fs');
  2. var common = require('./common');
  3. var _ls = require('./ls');
  4. //@
  5. //@ ### find(path [,path ...])
  6. //@ ### find(path_array)
  7. //@ Examples:
  8. //@
  9. //@ ```javascript
  10. //@ find('src', 'lib');
  11. //@ find(['src', 'lib']); // same as above
  12. //@ find('.').filter(function(file) { return file.match(/\.js$/); });
  13. //@ ```
  14. //@
  15. //@ Returns array of all files (however deep) in the given paths.
  16. //@
  17. //@ The main difference from `ls('-R', path)` is that the resulting file names
  18. //@ include the base directories, e.g. `lib/resources/file1` instead of just `file1`.
  19. function _find(options, paths) {
  20. if (!paths)
  21. common.error('no path specified');
  22. else if (typeof paths === 'object')
  23. paths = paths; // assume array
  24. else if (typeof paths === 'string')
  25. paths = [].slice.call(arguments, 1);
  26. var list = [];
  27. function pushFile(file) {
  28. if (common.platform === 'win')
  29. file = file.replace(/\\/g, '/');
  30. list.push(file);
  31. }
  32. // why not simply do ls('-R', paths)? because the output wouldn't give the base dirs
  33. // to get the base dir in the output, we need instead ls('-R', 'dir/*') for every directory
  34. paths.forEach(function(file) {
  35. pushFile(file);
  36. if (fs.statSync(file).isDirectory()) {
  37. _ls('-RA', file+'/*').forEach(function(subfile) {
  38. pushFile(subfile);
  39. });
  40. }
  41. });
  42. return list;
  43. }
  44. module.exports = _find;