1 | import {injectable, inject} from "inversify";
|
2 | import {CommandUtil} from "../interfaces/command-util";
|
3 | import path = require('path');
|
4 | import {IPostal} from "../interfaces/postal";
|
5 | import {LogConsole} from "../interfaces/log-console";
|
6 | import {ForceErrorImpl} from "./force-error-impl";
|
7 |
|
8 | const blackHoleStream = new (require('black-hole-stream'))();
|
9 | const fs = require('fs');
|
10 |
|
11 | @injectable()
|
12 | export class CommandUtilImpl extends ForceErrorImpl implements CommandUtil {
|
13 | private _console: LogConsole = {
|
14 | log: this.stdoutLog.bind(this),
|
15 | info: this.stdoutLog.bind(this),
|
16 | warn: this.stdoutLog.bind(this),
|
17 | error: this.stderrLog.bind(this)
|
18 | };
|
19 |
|
20 | private exitHandler(options, err: Error) {
|
21 | if (options.cleanup) {
|
22 | }
|
23 | if (err) {
|
24 | if (err && err.message) {
|
25 | this.stderrLog(err.message)
|
26 | }
|
27 | }
|
28 | if (options.exit) {
|
29 | process.exit();
|
30 | }
|
31 | }
|
32 |
|
33 | private registerProcessManagementEvents() {
|
34 | process.stdin.resume();
|
35 | process.on('exit', this.exitHandler.bind(this, {cleanup: true}));
|
36 | process.on('SIGINT', this.exitHandler.bind(this, {exit: true}));
|
37 | process.on('uncaughtException', this.exitHandler.bind(this, {exit: true}));
|
38 | }
|
39 |
|
40 | constructor(@inject('IPostal') private postal: IPostal) {
|
41 | super();
|
42 | this.registerProcessManagementEvents();
|
43 | const cacheStdoutWrite = process.stdout.write;
|
44 | const cacheStderrWrite = process.stderr.write;
|
45 | this.postal.subscribe({
|
46 | channel: 'CommandUtil',
|
47 | topic: 'SuppressConsoleOutput',
|
48 | callback: (data) => {
|
49 | if (data.suppressConsoleOutput) {
|
50 | process.stdout.write = process.stderr.write = blackHoleStream.write.bind(blackHoleStream);
|
51 | } else {
|
52 | process.stdout.write = cacheStdoutWrite;
|
53 | process.stderr.write = cacheStderrWrite;
|
54 | }
|
55 | }
|
56 | });
|
57 | this.postal.subscribe({
|
58 | channel: 'CommandUtil',
|
59 | topic: 'ProgressBarStarted',
|
60 | callback: (data) => {
|
61 | let dataConsole: LogConsole = data.console;
|
62 | this._console.log = dataConsole.log;
|
63 | this._console.info = dataConsole.info;
|
64 | this._console.warn = dataConsole.warn;
|
65 | this._console.error = dataConsole.error;
|
66 | }
|
67 | });
|
68 | }
|
69 |
|
70 | stderrWrite(msg: string) {
|
71 | process.stderr.write(msg);
|
72 | }
|
73 |
|
74 | stdoutWrite(msg: string) {
|
75 | process.stdout.write(msg);
|
76 | }
|
77 |
|
78 | private stderrLog(msg: string) {
|
79 | this.stderrWrite(`${msg}\n`);
|
80 | }
|
81 |
|
82 | private stdoutLog(msg: string) {
|
83 | this.stdoutWrite(`${msg}\n`);
|
84 | }
|
85 |
|
86 | returnErrorStringOrMessage(err: Error, message: string) {
|
87 | let errorMessage = this.logError(err, false);
|
88 | return errorMessage.length ? errorMessage : message;
|
89 | }
|
90 |
|
91 | error(msg: string) {
|
92 | this._console.error(msg);
|
93 | }
|
94 |
|
95 | log(msg: string) {
|
96 | this._console.log(msg);
|
97 | }
|
98 |
|
99 | logErrors(errs: Error[], writeErrorToConsole: boolean = true): string[] {
|
100 | let retVal: string[] = [];
|
101 | errs.forEach(err => {
|
102 | retVal.push(this.logError(err, writeErrorToConsole));
|
103 | });
|
104 | return retVal;
|
105 | }
|
106 |
|
107 | logError(err: Error, writeErrorToConsole: boolean = true): string {
|
108 | if (err) {
|
109 | if (writeErrorToConsole) {
|
110 | this._console.error(err.message);
|
111 | }
|
112 | return err.message;
|
113 | }
|
114 | return '';
|
115 | }
|
116 |
|
117 | processExitIfError(err: Error) {
|
118 | if (err) {
|
119 | this.processExitWithError(err);
|
120 | }
|
121 | }
|
122 |
|
123 | processExitWithError(err: Error, nonErrorMessage: string = null) {
|
124 | this.processExit(!!err ? 1 : 0, !!err ? err.message : !!nonErrorMessage ? nonErrorMessage : '');
|
125 | }
|
126 |
|
127 | processExit(exitCode: number = 0, msg: string = null) {
|
128 | this._console.log(!!msg ? msg : '');
|
129 | process.exit(exitCode);
|
130 | }
|
131 |
|
132 | callbackIfError(cb: (err: Error, anything: any, anything2?: any) => void,
|
133 | err: Error = null,
|
134 | result: any = null): boolean {
|
135 | cb = this.checkCallback(cb);
|
136 | err && cb(err, result);
|
137 | return !!err;
|
138 | }
|
139 |
|
140 | logAndCallback(msg: string,
|
141 | cb: (err: Error, anything: any, anything2?: any) => void,
|
142 | err: Error = null,
|
143 | result: any = null): boolean {
|
144 | this._console.log(err ? err.message : msg);
|
145 | cb = this.checkCallback(cb);
|
146 | cb(err, result);
|
147 | return !!err;
|
148 | }
|
149 |
|
150 | getConfigFilePath(filename: string, extension: string = '.json') {
|
151 | let regex = new RegExp('(.*)\\' + extension + '$', 'i');
|
152 | let cwd = process.cwd();
|
153 | filename = regex.test(filename)
|
154 | ? filename.replace(regex, '$1' + extension)
|
155 | : filename + extension;
|
156 | return path.resolve(cwd, filename);
|
157 | }
|
158 | }
|
159 |
|