1 | var common = require('./common');
|
2 | var fs = require('fs');
|
3 | var path = require('path');
|
4 |
|
5 | common.register('to', _to, {
|
6 | pipeOnly: true,
|
7 | wrapOutput: false,
|
8 | });
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | function _to(options, file) {
|
23 | if (!file) common.error('wrong arguments');
|
24 |
|
25 | if (!fs.existsSync(path.dirname(file))) {
|
26 | common.error('no such file or directory: ' + path.dirname(file));
|
27 | }
|
28 |
|
29 | try {
|
30 | fs.writeFileSync(file, this.stdout || this.toString(), 'utf8');
|
31 | return this;
|
32 | } catch (e) {
|
33 |
|
34 | common.error('could not write to file (code ' + e.code + '): ' + file, { continue: true });
|
35 | }
|
36 | }
|
37 | module.exports = _to;
|