/
serverlessLog.js
75 lines (62 loc) · 1.63 KB
/
serverlessLog.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
import boxen from 'boxen'
import chalk from 'chalk'
const { max } = Math
const blue = chalk.keyword('dodgerblue')
const grey = chalk.keyword('grey')
const lime = chalk.keyword('lime')
const orange = chalk.keyword('orange')
const peachpuff = chalk.keyword('peachpuff')
const plum = chalk.keyword('plum')
const red = chalk.keyword('red')
const yellow = chalk.keyword('yellow')
const colorMethodMapping = new Map([
['DELETE', red],
['GET', blue],
// ['HEAD', ...],
['PATCH', orange],
['POST', plum],
['PUT', blue],
])
let log
export default function serverlessLog(msg) {
if (log) {
log(msg, 'offline')
}
}
export function setLog(serverlessLogRef) {
log = serverlessLogRef
}
// logs based on:
// https://github.com/serverless/serverless/blob/master/lib/classes/CLI.js
function logRoute(method, server, path, maxLength) {
const methodColor = colorMethodMapping.get(method) ?? peachpuff
const methodFormatted = method.padEnd(maxLength, ' ')
return `${methodColor(methodFormatted)} ${yellow.dim('|')} ${grey.dim(
server,
)}${lime(path)}`
}
function getMaxHttpMethodNameLength(routeInfo) {
return max(...routeInfo.map(({ method }) => method.length))
}
export function logRoutes(routeInfo) {
const boxenOptions = {
borderColor: 'yellow',
dimBorder: true,
margin: 1,
padding: 1,
}
const maxLength = getMaxHttpMethodNameLength(routeInfo)
console.log(
boxen(
routeInfo
.map(({ method, path, server }) =>
logRoute(method, server, path, maxLength),
)
.join('\n'),
boxenOptions,
),
)
}
export function logWarning(msg) {
console.log(`offline: ${red(msg)}`)
}