This repository has been archived by the owner on Nov 4, 2022. It is now read-only.
/
logger.ts
236 lines (174 loc) · 5.42 KB
/
logger.ts
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/**
* © 2013 Liferay, Inc. <https://liferay.com> and Node GH contributors
* (see file: README.md)
* SPDX-License-Identifier: BSD-3-Clause
*/
import * as color from 'colors/safe'
import * as fs from 'fs'
import * as handlebars from 'handlebars'
import * as moment from 'moment'
import * as path from 'path'
import * as wordwrap from 'wordwrap'
import * as S from 'sanctuary'
const testing = process.env.NODE_ENV === 'testing'
if (testing || process.env.COLOR === 'false') {
color.disable()
}
export const colors = color
const wrap = wordwrap.hard(0, 80)
function stripHandlebarsNewLine(str) {
return str.replace(/[\s\t\r\n](\{\{[#\/])/g, '$1')
}
export function debug(...args) {
if (!process.env.GH_VERBOSE) {
return
}
if (typeof args[0] === 'string') {
args[0] = `DEBUG: ${args[0]}`
console.log(...args)
return
}
console.log('DEBUG:', ...args)
}
export function insane(...args) {
if (!process.env.GH_VERBOSE_INSANE) {
return
}
console.log(...args)
}
export const exitWithError = S.curry2((message, stack) => {
if (process.env.GH_VERBOSE || process.env.GH_VERBOSE_INSANE) {
console.error(`${message}\n${stack}`)
} else {
console.error(message)
}
process.exit(1)
})
export function error(...args) {
if (typeof args[0] === 'string') {
args[0] = `fatal: ${args[0]}`
}
console.error(...args)
process.exit(1)
}
export function warn(...args) {
args[0] = `warning: ${args[0]}`
console.error(...args)
}
export function log(...args) {
console.log(...args)
}
export function getDuration(start: Date, formatter?): string {
if (testing) {
return moment(start).from(new Date('December 17, 2013'))
}
if (formatter) {
return moment(start).format(formatter)
}
return moment(start).fromNow()
}
export function applyReplacements(output, replaceMap?: object) {
var regexPattern
for (regexPattern in replaceMap) {
if (replaceMap.hasOwnProperty(regexPattern)) {
output = output.replace(new RegExp(regexPattern, 'g'), replaceMap[regexPattern])
}
}
return output
}
export function getErrorMessage(err) {
var msg
// General normalizer
if (!err) {
return 'No error message.'
}
if (err.errors) {
return err.errors
}
// Normalize github api error
if (!err.message) {
return err
}
try {
msg = JSON.parse(err.message)
} catch (e) {
return err.message
}
if (typeof msg === 'string') {
return msg
}
if (msg.errors && msg.errors[0] && msg.errors[0].message) {
return msg.errors[0].message
}
if (msg.message) {
return msg.message
}
// Normalize git error
return err.message.replace('Command failed: fatal: ', '').trim()
}
export function compileTemplate(source, map) {
const template = handlebars.compile(source)
const replacements = applyReplacements(template(map))
return replacements
}
export function logTemplate(source, map) {
console.log(compileTemplate(source, map || {}))
}
export function logTemplateFile(file, map) {
let templatePath
let source
templatePath = path.join(file)
if (!fs.existsSync(templatePath)) {
templatePath = path.join(__dirname, 'cmds/templates', file)
}
source = fs.readFileSync(templatePath).toString()
logTemplate(stripHandlebarsNewLine(source), map)
}
export function registerHelper(name, callback) {
handlebars.registerHelper(name, callback)
}
export function registerHelpers_() {
handlebars.registerHelper('date', date => {
return getDuration(date)
})
handlebars.registerHelper('compareLink', function() {
const { github_host, user, repo, pullHeadSHA, currentSHA } = this.options
return `${github_host}/${user}/${repo}/compare/${pullHeadSHA}...${currentSHA}`
})
handlebars.registerHelper('forwardedLink', function() {
const { github_host, fwd, repo, forwardedPull } = this.options
return `${github_host}/${fwd}/${repo}/pull/${forwardedPull}`
})
handlebars.registerHelper('link', function() {
const { github_host, user, repo, number } = this.options
return `${github_host}/${user}/${repo}/pull/${number}`
})
handlebars.registerHelper('submittedLink', function() {
const { github_host, submit, repo, submittedPull } = this.options
return `${github_host}/${submit}/${repo}/pull/${submittedPull}`
})
handlebars.registerHelper('issueLink', function() {
const { github_host, user, repo, number } = this.options
return `${github_host}/${user}/${repo}/issues/${number}`
})
handlebars.registerHelper('gistLink', function() {
const { github_gist_host, loggedUser, id } = this.options
return `${github_gist_host}/${loggedUser}/${id}`
})
handlebars.registerHelper('repoLink', function() {
const { github_gist_host, user, repo } = this.options
return `${github_gist_host}/${user}/${repo}`
})
handlebars.registerHelper('wordwrap', (text, padding, stripNewLines) => {
let gutter = ''
if (stripNewLines !== false) {
text = text.replace(/[\r\n\s\t]+/g, ' ')
}
text = wrap(text).split('\n')
if (padding > 0) {
gutter = ' '.repeat(padding)
}
return text.join(`\n${gutter}`)
})
}
registerHelpers_()