toEnd.js 918 B

123456789101112131415161718192021222324252627282930313233343536
  1. var common = require('./common');
  2. var fs = require('fs');
  3. var path = require('path');
  4. common.register('toEnd', _toEnd, {
  5. pipeOnly: true,
  6. wrapOutput: false,
  7. });
  8. //@
  9. //@ ### ShellString.prototype.toEnd(file)
  10. //@
  11. //@ Examples:
  12. //@
  13. //@ ```javascript
  14. //@ cat('input.txt').toEnd('output.txt');
  15. //@ ```
  16. //@
  17. //@ Analogous to the redirect-and-append operator `>>` in Unix, but works with
  18. //@ ShellStrings (such as those returned by `cat`, `grep`, etc).
  19. function _toEnd(options, file) {
  20. if (!file) common.error('wrong arguments');
  21. if (!fs.existsSync(path.dirname(file))) {
  22. common.error('no such file or directory: ' + path.dirname(file));
  23. }
  24. try {
  25. fs.appendFileSync(file, this.stdout || this.toString(), 'utf8');
  26. return this;
  27. } catch (e) {
  28. /* istanbul ignore next */
  29. common.error('could not append to file (code ' + e.code + '): ' + file, { continue: true });
  30. }
  31. }
  32. module.exports = _toEnd;