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

cat.js 1.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. var common = require('./common');
  2. var fs = require('fs');
  3. //@
  4. //@ ### cat(file [, file ...])
  5. //@ ### cat(file_array)
  6. //@
  7. //@ Examples:
  8. //@
  9. //@ ```javascript
  10. //@ var str = cat('file*.txt');
  11. //@ var str = cat('file1', 'file2');
  12. //@ var str = cat(['file1', 'file2']); // same as above
  13. //@ ```
  14. //@
  15. //@ Returns a string containing the given file, or a concatenated string
  16. //@ containing the files if more than one file is given (a new line character is
  17. //@ introduced between each file). Wildcard `*` accepted.
  18. function _cat(options, files) {
  19. var cat = '';
  20. if (!files)
  21. common.error('no paths given');
  22. if (typeof files === 'string')
  23. files = [].slice.call(arguments, 1);
  24. // if it's array leave it as it is
  25. files = common.expand(files);
  26. files.forEach(function(file) {
  27. if (!fs.existsSync(file))
  28. common.error('no such file or directory: ' + file);
  29. cat += fs.readFileSync(file, 'utf8') + '\n';
  30. });
  31. if (cat[cat.length-1] === '\n')
  32. cat = cat.substring(0, cat.length-1);
  33. return common.ShellString(cat);
  34. }
  35. module.exports = _cat;