UNPKG

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