1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | 'use strict';
|
18 |
|
19 | var fs = require('fs');
|
20 | var _ = require('lodash');
|
21 | var util = require('util');
|
22 |
|
23 | module.exports.copyFile = function(source, target, cb) {
|
24 | cb = _.once(cb);
|
25 |
|
26 | var rd = fs.createReadStream(source);
|
27 | rd.on('error', function(err) {
|
28 | cb(err);
|
29 | });
|
30 |
|
31 | var wr = fs.createWriteStream(target);
|
32 | wr.on('error', function(err) {
|
33 | cb(err);
|
34 | });
|
35 | wr.on('close', function(err) {
|
36 | cb(err);
|
37 | });
|
38 | rd.pipe(wr);
|
39 | };
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | module.exports.captureOutput = function captureOutput() {
|
46 | var old_stdout_write = process.stdout.write;
|
47 | var old_console_error = console.error;
|
48 |
|
49 | var captured = '';
|
50 | var callback = function(string) {
|
51 | captured += string;
|
52 | };
|
53 |
|
54 | process.stdout.write = (function(write) {
|
55 | return function(string, encoding, fd) {
|
56 | var args = _.toArray(arguments);
|
57 | write.apply(process.stdout, args);
|
58 |
|
59 |
|
60 | callback.call(callback, string);
|
61 | };
|
62 | }(process.stdout.write));
|
63 |
|
64 | console.error = (function(log) {
|
65 | return function() {
|
66 | var args = _.toArray(arguments);
|
67 | args.unshift('[ERROR]');
|
68 | console.log.apply(console.log, args);
|
69 |
|
70 |
|
71 | callback.call(callback, util.format(args));
|
72 | };
|
73 | }(console.error));
|
74 |
|
75 | return {
|
76 | output: function output(err, reply) {
|
77 | return captured;
|
78 | },
|
79 | release: function done(err, reply) {
|
80 | process.stdout.write = old_stdout_write;
|
81 | console.error = old_console_error;
|
82 | return captured;
|
83 | }
|
84 | }
|
85 | };
|