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

dirs.js 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. var common = require('./common');
  2. var _cd = require('./cd');
  3. var path = require('path');
  4. // Pushd/popd/dirs internals
  5. var _dirStack = [];
  6. function _isStackIndex(index) {
  7. return (/^[\-+]\d+$/).test(index);
  8. }
  9. function _parseStackIndex(index) {
  10. if (_isStackIndex(index)) {
  11. if (Math.abs(index) < _dirStack.length + 1) { // +1 for pwd
  12. return (/^-/).test(index) ? Number(index) - 1 : Number(index);
  13. } else {
  14. common.error(index + ': directory stack index out of range');
  15. }
  16. } else {
  17. common.error(index + ': invalid number');
  18. }
  19. }
  20. function _actualDirStack() {
  21. return [process.cwd()].concat(_dirStack);
  22. }
  23. //@
  24. //@ ### pushd([options,] [dir | '-N' | '+N'])
  25. //@
  26. //@ Available options:
  27. //@
  28. //@ + `-n`: Suppresses the normal change of directory when adding directories to the stack, so that only the stack is manipulated.
  29. //@
  30. //@ Arguments:
  31. //@
  32. //@ + `dir`: Makes the current working directory be the top of the stack, and then executes the equivalent of `cd dir`.
  33. //@ + `+N`: Brings the Nth directory (counting from the left of the list printed by dirs, starting with zero) to the top of the list by rotating the stack.
  34. //@ + `-N`: Brings the Nth directory (counting from the right of the list printed by dirs, starting with zero) to the top of the list by rotating the stack.
  35. //@
  36. //@ Examples:
  37. //@
  38. //@ ```javascript
  39. //@ // process.cwd() === '/usr'
  40. //@ pushd('/etc'); // Returns /etc /usr
  41. //@ pushd('+1'); // Returns /usr /etc
  42. //@ ```
  43. //@
  44. //@ Save the current directory on the top of the directory stack and then cd to `dir`. With no arguments, pushd exchanges the top two directories. Returns an array of paths in the stack.
  45. function _pushd(options, dir) {
  46. if (_isStackIndex(options)) {
  47. dir = options;
  48. options = '';
  49. }
  50. options = common.parseOptions(options, {
  51. 'n' : 'no-cd'
  52. });
  53. var dirs = _actualDirStack();
  54. if (dir === '+0') {
  55. return dirs; // +0 is a noop
  56. } else if (!dir) {
  57. if (dirs.length > 1) {
  58. dirs = dirs.splice(1, 1).concat(dirs);
  59. } else {
  60. return common.error('no other directory');
  61. }
  62. } else if (_isStackIndex(dir)) {
  63. var n = _parseStackIndex(dir);
  64. dirs = dirs.slice(n).concat(dirs.slice(0, n));
  65. } else {
  66. if (options['no-cd']) {
  67. dirs.splice(1, 0, dir);
  68. } else {
  69. dirs.unshift(dir);
  70. }
  71. }
  72. if (options['no-cd']) {
  73. dirs = dirs.slice(1);
  74. } else {
  75. dir = path.resolve(dirs.shift());
  76. _cd('', dir);
  77. }
  78. _dirStack = dirs;
  79. return _dirs('');
  80. }
  81. exports.pushd = _pushd;
  82. //@
  83. //@ ### popd([options,] ['-N' | '+N'])
  84. //@
  85. //@ Available options:
  86. //@
  87. //@ + `-n`: Suppresses the normal change of directory when removing directories from the stack, so that only the stack is manipulated.
  88. //@
  89. //@ Arguments:
  90. //@
  91. //@ + `+N`: Removes the Nth directory (counting from the left of the list printed by dirs), starting with zero.
  92. //@ + `-N`: Removes the Nth directory (counting from the right of the list printed by dirs), starting with zero.
  93. //@
  94. //@ Examples:
  95. //@
  96. //@ ```javascript
  97. //@ echo(process.cwd()); // '/usr'
  98. //@ pushd('/etc'); // '/etc /usr'
  99. //@ echo(process.cwd()); // '/etc'
  100. //@ popd(); // '/usr'
  101. //@ echo(process.cwd()); // '/usr'
  102. //@ ```
  103. //@
  104. //@ When no arguments are given, popd removes the top directory from the stack and performs a cd to the new top directory. The elements are numbered from 0 starting at the first directory listed with dirs; i.e., popd is equivalent to popd +0. Returns an array of paths in the stack.
  105. function _popd(options, index) {
  106. if (_isStackIndex(options)) {
  107. index = options;
  108. options = '';
  109. }
  110. options = common.parseOptions(options, {
  111. 'n' : 'no-cd'
  112. });
  113. if (!_dirStack.length) {
  114. return common.error('directory stack empty');
  115. }
  116. index = _parseStackIndex(index || '+0');
  117. if (options['no-cd'] || index > 0 || _dirStack.length + index === 0) {
  118. index = index > 0 ? index - 1 : index;
  119. _dirStack.splice(index, 1);
  120. } else {
  121. var dir = path.resolve(_dirStack.shift());
  122. _cd('', dir);
  123. }
  124. return _dirs('');
  125. }
  126. exports.popd = _popd;
  127. //@
  128. //@ ### dirs([options | '+N' | '-N'])
  129. //@
  130. //@ Available options:
  131. //@
  132. //@ + `-c`: Clears the directory stack by deleting all of the elements.
  133. //@
  134. //@ Arguments:
  135. //@
  136. //@ + `+N`: Displays the Nth directory (counting from the left of the list printed by dirs when invoked without options), starting with zero.
  137. //@ + `-N`: Displays the Nth directory (counting from the right of the list printed by dirs when invoked without options), starting with zero.
  138. //@
  139. //@ Display the list of currently remembered directories. Returns an array of paths in the stack, or a single path if +N or -N was specified.
  140. //@
  141. //@ See also: pushd, popd
  142. function _dirs(options, index) {
  143. if (_isStackIndex(options)) {
  144. index = options;
  145. options = '';
  146. }
  147. options = common.parseOptions(options, {
  148. 'c' : 'clear'
  149. });
  150. if (options['clear']) {
  151. _dirStack = [];
  152. return _dirStack;
  153. }
  154. var stack = _actualDirStack();
  155. if (index) {
  156. index = _parseStackIndex(index);
  157. if (index < 0) {
  158. index = stack.length + index;
  159. }
  160. common.log(stack[index]);
  161. return stack[index];
  162. }
  163. common.log(stack.join(' '));
  164. return stack;
  165. }
  166. exports.dirs = _dirs;