1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const deps_1 = require("./deps");
|
4 | const path = require("path");
|
5 | const util = require("util");
|
6 | const fs = require("./fs");
|
7 | class StreamOutput {
|
8 | constructor(type, stream) {
|
9 | this.type = type;
|
10 | this.stream = stream;
|
11 | }
|
12 | static logToFile(msg, logfile) {
|
13 | try {
|
14 | fs.mkdirpSync(path.dirname(logfile));
|
15 | fs.appendFileSync(logfile, deps_1.default.stripAnsi(msg));
|
16 | }
|
17 | catch (err) {
|
18 | console.error(err);
|
19 | }
|
20 | }
|
21 | get output() {
|
22 | return this.type === 'stdout' ? deps_1.default.Config.stdout : deps_1.default.Config.stderr;
|
23 | }
|
24 | write(msg, options = {}) {
|
25 | msg = msg || '';
|
26 | const log = options.log !== false;
|
27 | if (log)
|
28 | this.writeLogFile(msg, StreamOutput.startOfLine);
|
29 |
|
30 | if (StreamOutput.startOfLine && deps_1.default.Config.displayTimestamps) {
|
31 | msg = this.timestamp(msg);
|
32 | }
|
33 | if (deps_1.default.Config.mock) {
|
34 | let m = deps_1.default.stripAnsi(msg);
|
35 | if (this.type === 'stdout')
|
36 | deps_1.default.Config.stdout += m;
|
37 | else
|
38 | deps_1.default.Config.stderr += m;
|
39 | }
|
40 | else {
|
41 | this.stream.write(msg);
|
42 | }
|
43 | StreamOutput.startOfLine = msg.endsWith('\n');
|
44 | }
|
45 | log(data, ...args) {
|
46 | let msg = data ? util.format(data, ...args) : '';
|
47 | msg += '\n';
|
48 | this.write(msg);
|
49 | }
|
50 | writeLogFile(msg, withTimestamp) {
|
51 | if (!this.logfile) {
|
52 | return;
|
53 | }
|
54 | msg = withTimestamp ? this.timestamp(msg) : msg;
|
55 | StreamOutput.logToFile(msg, this.logfile);
|
56 | }
|
57 | get logfile() {
|
58 | if (this.type === 'stderr')
|
59 | return deps_1.default.Config.errlog;
|
60 | }
|
61 | timestamp(msg) {
|
62 | return `[${deps_1.default.moment().format()}] ${msg}`;
|
63 | }
|
64 | }
|
65 | StreamOutput.startOfLine = false;
|
66 | exports.default = StreamOutput;
|