|
3 | 3 | "use strict";
|
4 | 4 | "use babel";
|
5 | 5 |
|
6 |
| -import path from "path"; |
7 |
| - |
8 |
| -const packageRoot = atom.packages.resolvePackagePath("build-ibmstreams"); |
9 |
| -const STREAMING_ANALYTICS_ICON_PATH = `${packageRoot}${path.sep}assets${path.sep}streaming_analytics_200x200.png`; |
10 |
| - |
11 | 6 | export class MessageHandler {
|
12 | 7 | consoleService: null;
|
13 | 8 |
|
14 | 9 | constructor(service) {
|
15 | 10 | this.consoleService = service;
|
16 | 11 | }
|
17 | 12 |
|
18 |
| - handleBuildProgressMessage(messageOutput: Array<any> | string, showNotification?: boolean) { |
19 |
| - if (Array.isArray(messageOutput)) { |
20 |
| - const message = this.getLoggableMessage(messageOutput); |
21 |
| - if (message) { |
22 |
| - this.consoleService.log(message); |
23 |
| - if (showNotification) { |
24 |
| - atom.notifications.addInfo("building...", {detail: message}); |
25 |
| - } |
26 |
| - } |
27 |
| - } else if (typeof(messageOutput) === "string") { |
28 |
| - this.consoleService.log(messageOutput); |
29 |
| - if (showNotification) { |
30 |
| - atom.notifications.addInfo(messageOutput, {}); |
31 |
| - } |
| 13 | + handleInfo( |
| 14 | + message, |
| 15 | + { |
| 16 | + detail = null, |
| 17 | + description = null, |
| 18 | + showNotification = true, |
| 19 | + showConsoleMessage = true, |
| 20 | + notificationAutoDismiss = true, |
| 21 | + notificationButtons = [] |
| 22 | + } = {} |
| 23 | + ) { |
| 24 | + const addedButtons = this.processButtons(notificationButtons); |
| 25 | + const detailMessage = this.joinMessageArray(detail); |
| 26 | + |
| 27 | + if (showConsoleMessage) { |
| 28 | + this.consoleService.log(`${message}${detailMessage ? "\n"+detailMessage: ""}`); |
32 | 29 | }
|
33 |
| - } |
34 |
| - |
35 |
| - handleBuildSuccess(messageOutput: Array<any>) { |
36 |
| - const message = this.getLoggableMessage(messageOutput); |
37 |
| - if (message) { |
38 |
| - this.consoleService.success(message); |
39 |
| - atom.notifications.addSuccess("Build succeeded", {detail: message, dismissable: true}); |
40 |
| - } else { |
41 |
| - this.consoleService.success("Build succeeded"); |
42 |
| - atom.notifications.addSuccess("Build succeeded", {dismissable: true}); |
| 30 | + if (showNotification && typeof(message) === "string") { |
| 31 | + const notificationOptions = { |
| 32 | + ...addedButtons, |
| 33 | + dismissable: !notificationAutoDismiss, |
| 34 | + detail: detailMessage ? detailMessage : "", |
| 35 | + description: description ? description : "" |
| 36 | + }; |
| 37 | + return atom.notifications.addInfo(message, notificationOptions); |
43 | 38 | }
|
44 | 39 | }
|
45 | 40 |
|
46 |
| - handleBuildFailure(messageOutput: Array<any>) { |
47 |
| - const message = this.getLoggableMessage(messageOutput); |
48 |
| - if (message) { |
| 41 | + handleError( |
| 42 | + message, |
| 43 | + { |
| 44 | + detail, |
| 45 | + description, |
| 46 | + stack, |
| 47 | + showNotification = true, |
| 48 | + showConsoleMessage = true, |
| 49 | + consoleErrorLog = true, |
| 50 | + notificationAutoDismiss = false, |
| 51 | + notificationButtons = [] |
| 52 | + } = {} |
| 53 | + ) { |
| 54 | + const addedButtons = this.processButtons(notificationButtons); |
| 55 | + const detailMessage = this.joinMessageArray(detail); |
| 56 | + const stackMessage = this.joinMessageArray(stack); |
| 57 | + |
| 58 | + if (consoleErrorLog) { |
| 59 | + if (stack) { |
| 60 | + console.error(message, stack); |
| 61 | + } else { |
| 62 | + console.error(message); |
| 63 | + } |
| 64 | + } |
| 65 | + if (showConsoleMessage) { |
49 | 66 | this.consoleService.error(message);
|
50 |
| - atom.notifications.addError("Build failed", {detail: message, dismissable: true}); |
51 |
| - } else { |
52 |
| - this.consoleService.error("Build failed"); |
53 |
| - atom.notifications.addError("Build failed", {dismissable: true}); |
| 67 | + if (typeof(detailMessage) === "string" && detailMessage.length > 0) { |
| 68 | + this.consoleService.error(detailMessage); |
| 69 | + } |
54 | 70 | }
|
55 |
| - } |
56 |
| - |
57 |
| - handleSubmitProgressMessage(input) { |
58 |
| - if (typeof(input) === "string") { |
59 |
| - atom.notifications.addInfo(input, {}); |
| 71 | + if (showNotification && typeof(message) === "string") { |
| 72 | + const notificationOptions = { |
| 73 | + ...addedButtons, |
| 74 | + dismissable: !notificationAutoDismiss, |
| 75 | + detail: detailMessage ? detailMessage : "", |
| 76 | + stack: stackMessage ? stackMessage: "", |
| 77 | + description: description ? description : "" |
| 78 | + }; |
| 79 | + return atom.notifications.addError(message, notificationOptions); |
60 | 80 | }
|
61 |
| - this.consoleService.log(input); |
62 | 81 | }
|
63 | 82 |
|
64 |
| - handleSubmitSuccess(input, notificationButtons = []) { |
65 |
| - let addedButtons = {}; |
66 |
| - if (Array.isArray(notificationButtons)) { |
67 |
| - addedButtons.buttons = notificationButtons.map(obj => ({onDidClick: obj.callbackFn, text: obj.label})); |
| 83 | + handleSuccess( |
| 84 | + message, |
| 85 | + { |
| 86 | + detail = null, |
| 87 | + description = null, |
| 88 | + showNotification = true, |
| 89 | + showConsoleMessage = true, |
| 90 | + notificationAutoDismiss = false, |
| 91 | + notificationButtons = [] |
| 92 | + } = {} |
| 93 | + ) { |
| 94 | + const addedButtons = this.processButtons(notificationButtons); |
| 95 | + const detailMessage = this.joinMessageArray(detail); |
| 96 | + |
| 97 | + if (showConsoleMessage) { |
| 98 | + this.consoleService.log(`${message}${detailMessage ? "\n"+detailMessage: ""}`); |
68 | 99 | }
|
69 |
| - atom.notifications.addSuccess(`Job ${input.name} is ${input.health}`, {...addedButtons, dismissable: true}); |
70 |
| - |
71 |
| - if (this.consoleService) { |
72 |
| - this.consoleService.success(`Job ${input.name} is ${input.health}`); |
| 100 | + if (showNotification && typeof(message) === "string") { |
| 101 | + const notificationOptions = { |
| 102 | + ...addedButtons, |
| 103 | + dismissable: !notificationAutoDismiss, |
| 104 | + detail: detailMessage ? detailMessage : "", |
| 105 | + description: description ? description : "" |
| 106 | + }; |
| 107 | + return atom.notifications.addSuccess(message, notificationOptions); |
73 | 108 | }
|
74 | 109 | }
|
75 | 110 |
|
76 |
| - handleSubmitFailure(input) { |
77 |
| - const errorString = input.errors.map(err => err.message).join("\n"); |
78 |
| - atom.notifications.addError(`Job submission failed`, {detail: errorString, dismissable: true}); |
79 |
| - |
80 |
| - if (this.consoleService) { |
81 |
| - this.consoleService.error(`Job submission failed\n${errorString}`); |
82 |
| - } |
83 |
| - } |
84 |
| - handleError(input, notificationButtons = []) { |
85 |
| - let addedButtons = {}; |
86 |
| - if (Array.isArray(notificationButtons)) { |
87 |
| - addedButtons.buttons = notificationButtons.map(obj => ({onDidClick: obj.callbackFn, text: obj.label})); |
88 |
| - } |
89 |
| - if (typeof(input) === "string") { |
90 |
| - atom.notifications.addError(input, {...addedButtons, dismissable: true}); |
91 |
| - this.consoleService.error(input); |
92 |
| - } else if (input.message) { |
93 |
| - atom.notifications.addError( |
94 |
| - input.message, |
95 |
| - {...addedButtons, dismissable: true, detail: input.stack, stack: input.stack} |
96 |
| - ); |
97 |
| - this.consoleService.error(input.message); |
98 |
| - } |
99 |
| - console.error(input); |
100 |
| - } |
101 |
| - handleSuccess(input, detail, showNotification, showConsoleMsg, notificationButtons = []) { |
102 |
| - let addedButtons = {}; |
103 |
| - if (Array.isArray(notificationButtons)) { |
104 |
| - addedButtons.buttons = notificationButtons.map(obj => ({onDidClick: obj.callbackFn, text: obj.label})); |
105 |
| - } |
106 |
| - if (showNotification) { |
107 |
| - atom.notifications.addSuccess(input, {...addedButtons, detail: detail, dismissable: true}); |
108 |
| - } |
109 |
| - if (showConsoleMsg) { |
110 |
| - if (this.consoleService) { |
111 |
| - this.consoleService.success(`${input}\n${detail}`); |
112 |
| - } |
113 |
| - } |
114 |
| - } |
115 |
| - |
116 |
| - handleWarning(message) { |
117 |
| - if (message && typeof(message) === "string") { |
118 |
| - this.consoleService.warn(message); |
119 |
| - atom.notifications.addWarning(message, {}); |
120 |
| - } |
121 |
| - } |
122 |
| - |
123 |
| - showDialog(message, detail, buttonObjs) { |
124 |
| - |
125 |
| - const nativeImage = require("electron").nativeImage; |
126 |
| - |
127 |
| - const labels = buttonObjs.map(obj => obj.label); |
128 |
| - const callbacks = buttonObjs.map(obj => obj.callbackFn); |
129 |
| - let buttons = {}; |
130 |
| - labels.forEach((label, index) => { |
131 |
| - buttons[label] = callbacks[index]; |
132 |
| - }); |
133 |
| - atom.confirm( |
134 |
| - { |
135 |
| - message: message, |
136 |
| - detail: detail, |
137 |
| - buttons: labels, |
138 |
| - icon: STREAMING_ANALYTICS_ICON_PATH |
139 |
| - }, |
140 |
| - (chosen, checkboxChecked) => { |
141 |
| - const callback = callbacks[chosen]; |
142 |
| - if (typeof(callback) === "function") { |
143 |
| - return callback(); |
144 |
| - } |
145 |
| - } |
146 |
| - ); |
147 |
| - } |
148 |
| - |
149 |
| - handleCredentialsMissing() { |
150 |
| - atom.notifications.addError( |
| 111 | + handleCredentialsMissing(errorNotification) { |
| 112 | + const n = atom.notifications.addError( |
151 | 113 | "Copy and paste the Streaming Analytics service credentials into the build-ibmstreams package settings page.",
|
152 | 114 | {
|
153 | 115 | dismissable: true,
|
154 | 116 | buttons: [{
|
155 | 117 | text: "Open package settings",
|
156 |
| - onDidClick: () => {atom.workspace.open("atom://config/packages/build-ibmstreams")} |
| 118 | + onDidClick: () => { |
| 119 | + this.dismissNotification(errorNotification); |
| 120 | + this.dismissNotification(n); |
| 121 | + atom.workspace.open("atom://config/packages/build-ibmstreams"); |
| 122 | + } |
157 | 123 | }]
|
158 | 124 | }
|
159 | 125 | );
|
| 126 | + return n; |
| 127 | + } |
| 128 | + |
| 129 | + processButtons(btns) { |
| 130 | + let buttons = {}; |
| 131 | + if (Array.isArray(btns)) { |
| 132 | + buttons.buttons = btns.map(obj => ({onDidClick: obj.callbackFn, text: obj.label})); |
| 133 | + } |
| 134 | + return buttons; |
| 135 | + } |
| 136 | + |
| 137 | + joinMessageArray(msgArray) { |
| 138 | + if (Array.isArray(msgArray)) { |
| 139 | + return msgArray.join("\n").trimRight(); |
| 140 | + } |
| 141 | + return msgArray; |
| 142 | + } |
| 143 | + |
| 144 | + dismissNotification(notification) { |
| 145 | + if (notification && typeof(notification.dismiss) === "function") { |
| 146 | + notification.dismiss(); |
| 147 | + } |
160 | 148 | }
|
161 | 149 |
|
162 | 150 | getLoggableMessage(messages: Array<any>) {
|
163 |
| - return messages |
164 |
| - .map(outputMsg => outputMsg.message_text) |
165 |
| - .join("\n") |
166 |
| - .trimRight(); |
| 151 | + return this.joinMessageArray(messages.map(outputMsg => outputMsg.message_text)); |
167 | 152 | }
|
168 | 153 | }
|
0 commit comments