-
Notifications
You must be signed in to change notification settings - Fork 3
/
WebLogger.js
153 lines (142 loc) · 4.19 KB
/
WebLogger.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import $ from 'jquery'
import AnsiUp from 'ansi_up'
import _ from 'lodash'
import {
createSpan,
toggleClass,
createEl,
createButton,
createEnvsInput,
} from '../helpers/dom_utils'
export default class WebLogger {
watchTaskLogsScrollTop = true
autoScrollButton
loggerId
element
ansiUp
scrollTo(direction, animate, scrollPixels) {
const scrollTop = scrollPixels
? $(this.element).scrollTop()
: direction === 'bottom'
? this.element.scrollHeight
: 0
if (scrollPixels) {
$(this.element).scrollTop(
direction === 'bottom'
? scrollTop - scrollPixels
: scrollTop + scrollPixels
)
} else if (animate) {
$(this.element).animate({ scrollTop }, animate)
} else {
this.element.scrollTop = scrollTop
}
}
createHTMLLog = logData => {
const log = this.ansiUp.ansi_to_html(
logData.replace(/\n/g, '<br>').replace(/ /g, ' ')
)
if (log.includes('Warning:')) {
return createSpan('ansi-yellow-fg', log)
}
if (log.includes('Exited Successfully')) {
return createSpan('ended ok', log)
}
if (log.includes('Exited with exit code ')) {
return createSpan('ended', log)
}
return log
.replace(/(<span class="ansi-bright-black-fg">.<\/span>)/g, () =>
createSpan('ansi-bright-black-fg mocha-test', '.')
)
.replace(/(<span class="ansi-red-fg">!<\/span>)/g, () =>
createSpan('ansi-red-fg mocha-test', '!')
)
}
push(log, isRaw) {
this.element.insertAdjacentHTML(
'beforeend',
isRaw ? log : this.createHTMLLog(log)
)
}
clear() {
while (this.element.lastChild) {
this.element.removeChild(this.element.lastChild)
}
}
updateEnvs(envs) {
if (_.keys(envs).length) {
const container = document.createElement('div')
container.classList.add('envs-log')
container.innerHTML =
_.map(
envs,
(value, key) =>
`${createSpan('ansi-bright-magenta-fg', key)}=${createEnvsInput(
key,
value
)}`
).join(', ') +
createButton('logs-button', 'edit', 'global.enableEnvsForm()') +
createButton('logs-button cancel', 'times', 'global.cancelEnvs()') +
createButton('logs-button apply', 'check', 'global.updateEnvs()')
this.element.appendChild(container)
}
}
updateDescription(description) {
const container = createEl('div', { className: 'task-data' })
const span = createEl('span', { innerText: description })
container.appendChild(span)
this.element.appendChild(container)
}
onScroll() {
toggleClass(this, 'scrolling', this.scrollTop > 70)
}
triggerScrollWatcher = () => {
this.scrollTo('bottom', '1500')
this.watchTaskLogsScrollTop = !this.watchTaskLogsScrollTop
toggleClass(this.autoScrollButton, 'active', this.watchTaskLogsScrollTop)
}
createScrollActions(parent) {
const scrollActions = createEl('div', {
className: 'scroll-actions',
parent,
})
createEl('button', {
className: 'logs-button scroll-top',
onclick: () => this.scrollTo('top', '500'),
innerHTML: '<i class="fas fa-angle-double-down"></i>',
parent: scrollActions,
})
createEl('button', {
className: 'logs-button clear',
onclick: () => window.global.clearLogs(this.loggerId),
innerHTML: '<i class="fas fa-eraser"></i>',
parent: scrollActions,
})
this.autoScrollButton = createEl('button', {
className: `logs-button autoscroll ${
this.watchTaskLogsScrollTop ? 'active' : ''
}`,
onclick: () => this.triggerScrollWatcher(),
innerHTML: '<i class="fas fa-angle-double-down"></i>',
parent: scrollActions,
})
}
constructor(logsContainer, id) {
this.loggerId = id
const wrapper = createEl('div', {
className: 'logs-wrapper',
parent: logsContainer,
})
this.createScrollActions(wrapper)
this.element = createEl('div', {
className: 'logs-container',
parent: wrapper,
onscroll: this.onScroll,
})
this.ansiUp = new AnsiUp()
this.ansiUp.use_classes = true
this.ansiUp.escape_for_html = false
}
}