Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

ls.js 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. var path = require('path');
  2. var fs = require('fs');
  3. var common = require('./common');
  4. var _cd = require('./cd');
  5. var _pwd = require('./pwd');
  6. //@
  7. //@ ### ls([options ,] path [,path ...])
  8. //@ ### ls([options ,] path_array)
  9. //@ Available options:
  10. //@
  11. //@ + `-R`: recursive
  12. //@ + `-A`: all files (include files beginning with `.`, except for `.` and `..`)
  13. //@
  14. //@ Examples:
  15. //@
  16. //@ ```javascript
  17. //@ ls('projs/*.js');
  18. //@ ls('-R', '/users/me', '/tmp');
  19. //@ ls('-R', ['/users/me', '/tmp']); // same as above
  20. //@ ```
  21. //@
  22. //@ Returns array of files in the given path, or in current directory if no path provided.
  23. function _ls(options, paths) {
  24. options = common.parseOptions(options, {
  25. 'R': 'recursive',
  26. 'A': 'all',
  27. 'a': 'all_deprecated'
  28. });
  29. if (options.all_deprecated) {
  30. // We won't support the -a option as it's hard to image why it's useful
  31. // (it includes '.' and '..' in addition to '.*' files)
  32. // For backwards compatibility we'll dump a deprecated message and proceed as before
  33. common.log('ls: Option -a is deprecated. Use -A instead');
  34. options.all = true;
  35. }
  36. if (!paths)
  37. paths = ['.'];
  38. else if (typeof paths === 'object')
  39. paths = paths; // assume array
  40. else if (typeof paths === 'string')
  41. paths = [].slice.call(arguments, 1);
  42. var list = [];
  43. // Conditionally pushes file to list - returns true if pushed, false otherwise
  44. // (e.g. prevents hidden files to be included unless explicitly told so)
  45. function pushFile(file, query) {
  46. // hidden file?
  47. if (path.basename(file)[0] === '.') {
  48. // not explicitly asking for hidden files?
  49. if (!options.all && !(path.basename(query)[0] === '.' && path.basename(query).length > 1))
  50. return false;
  51. }
  52. if (common.platform === 'win')
  53. file = file.replace(/\\/g, '/');
  54. list.push(file);
  55. return true;
  56. }
  57. paths.forEach(function(p) {
  58. if (fs.existsSync(p)) {
  59. var stats = fs.statSync(p);
  60. // Simple file?
  61. if (stats.isFile()) {
  62. pushFile(p, p);
  63. return; // continue
  64. }
  65. // Simple dir?
  66. if (stats.isDirectory()) {
  67. // Iterate over p contents
  68. fs.readdirSync(p).forEach(function(file) {
  69. if (!pushFile(file, p))
  70. return;
  71. // Recursive?
  72. if (options.recursive) {
  73. var oldDir = _pwd();
  74. _cd('', p);
  75. if (fs.statSync(file).isDirectory())
  76. list = list.concat(_ls('-R'+(options.all?'A':''), file+'/*'));
  77. _cd('', oldDir);
  78. }
  79. });
  80. return; // continue
  81. }
  82. }
  83. // p does not exist - possible wildcard present
  84. var basename = path.basename(p);
  85. var dirname = path.dirname(p);
  86. // Wildcard present on an existing dir? (e.g. '/tmp/*.js')
  87. if (basename.search(/\*/) > -1 && fs.existsSync(dirname) && fs.statSync(dirname).isDirectory) {
  88. // Escape special regular expression chars
  89. var regexp = basename.replace(/(\^|\$|\(|\)|<|>|\[|\]|\{|\}|\.|\+|\?)/g, '\\$1');
  90. // Translates wildcard into regex
  91. regexp = '^' + regexp.replace(/\*/g, '.*') + '$';
  92. // Iterate over directory contents
  93. fs.readdirSync(dirname).forEach(function(file) {
  94. if (file.match(new RegExp(regexp))) {
  95. if (!pushFile(path.normalize(dirname+'/'+file), basename))
  96. return;
  97. // Recursive?
  98. if (options.recursive) {
  99. var pp = dirname + '/' + file;
  100. if (fs.lstatSync(pp).isDirectory())
  101. list = list.concat(_ls('-R'+(options.all?'A':''), pp+'/*'));
  102. } // recursive
  103. } // if file matches
  104. }); // forEach
  105. return;
  106. }
  107. common.error('no such file or directory: ' + p, true);
  108. });
  109. return list;
  110. }
  111. module.exports = _ls;