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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. var fs = require('fs');
  2. var path = require('path');
  3. var common = require('./common');
  4. //@
  5. //@ ### mv(source [, source ...], dest')
  6. //@ ### mv(source_array, dest')
  7. //@ Available options:
  8. //@
  9. //@ + `f`: force
  10. //@
  11. //@ Examples:
  12. //@
  13. //@ ```javascript
  14. //@ mv('-f', 'file', 'dir/');
  15. //@ mv('file1', 'file2', 'dir/');
  16. //@ mv(['file1', 'file2'], 'dir/'); // same as above
  17. //@ ```
  18. //@
  19. //@ Moves files. The wildcard `*` is accepted.
  20. function _mv(options, sources, dest) {
  21. options = common.parseOptions(options, {
  22. 'f': 'force'
  23. });
  24. // Get sources, dest
  25. if (arguments.length < 3) {
  26. common.error('missing <source> and/or <dest>');
  27. } else if (arguments.length > 3) {
  28. sources = [].slice.call(arguments, 1, arguments.length - 1);
  29. dest = arguments[arguments.length - 1];
  30. } else if (typeof sources === 'string') {
  31. sources = [sources];
  32. } else if ('length' in sources) {
  33. sources = sources; // no-op for array
  34. } else {
  35. common.error('invalid arguments');
  36. }
  37. sources = common.expand(sources);
  38. var exists = fs.existsSync(dest),
  39. stats = exists && fs.statSync(dest);
  40. // Dest is not existing dir, but multiple sources given
  41. if ((!exists || !stats.isDirectory()) && sources.length > 1)
  42. common.error('dest is not a directory (too many sources)');
  43. // Dest is an existing file, but no -f given
  44. if (exists && stats.isFile() && !options.force)
  45. common.error('dest file already exists: ' + dest);
  46. sources.forEach(function(src) {
  47. if (!fs.existsSync(src)) {
  48. common.error('no such file or directory: '+src, true);
  49. return; // skip file
  50. }
  51. // If here, src exists
  52. // When copying to '/path/dir':
  53. // thisDest = '/path/dir/file1'
  54. var thisDest = dest;
  55. if (fs.existsSync(dest) && fs.statSync(dest).isDirectory())
  56. thisDest = path.normalize(dest + '/' + path.basename(src));
  57. if (fs.existsSync(thisDest) && !options.force) {
  58. common.error('dest file already exists: ' + thisDest, true);
  59. return; // skip file
  60. }
  61. if (path.resolve(src) === path.dirname(path.resolve(thisDest))) {
  62. common.error('cannot move to self: '+src, true);
  63. return; // skip file
  64. }
  65. fs.renameSync(src, thisDest);
  66. }); // forEach(src)
  67. } // mv
  68. module.exports = _mv;