This repository has been archived by the owner on Oct 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
119 lines (94 loc) · 2.58 KB
/
index.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
const boxen = require('boxen')
const chalk = require('chalk')
const {
isHyper,
isITerm,
isCI,
isSilentMode
} = require('./lib/detect')
const { isShownRecently, markShown } = require('./lib/limit')
const { checkMessage } = require('./lib/check')
const messages = require('./messages.json')
const wrap = require('./lib/wrap')
function formatTitle (title) {
title = wrap(title)
if (!isCI()) {
title = chalk.black(title)
}
if (!isHyper() && !isITerm()) {
title = chalk.bold(title)
}
return title
}
function formatText (text) {
text = wrap(text)
text = text.replace(
/{{([^}]*?)}}/g,
(match, url) => chalk.blue.underline(url)
)
if (!isCI()) {
text = chalk.black(text)
}
return text
}
function formatUrl (url) {
url = wrap(url, { cut: true })
return chalk.blue.underline(url)
}
function formatMessage (message) {
const { title, text, url } = message
const coloredMessage = formatTitle(title) + '\n\n' + formatText(text) +
'\n\n' + formatUrl(url)
const opts = {
align: 'center',
borderStyle: {
topLeft: ' ',
topRight: ' ',
bottomLeft: ' ',
bottomRight: ' ',
horizontal: ' ',
vertical: ' '
},
float: 'center',
margin: 0,
padding: {
top: 1,
right: 4,
bottom: 1,
left: 4
}
}
if (!isCI()) {
Object.assign(opts, {
backgroundColor: 'white'
})
}
return boxen(coloredMessage, opts)
}
function printMessage () {
// Do not print message when npm is run in silent mode
if (isSilentMode()) return
// Do not print message when one has been shown recently
if (isShownRecently()) return
// Skip running if no messages are available
if (messages.length === 0) return
// Select a random message
const i = Math.floor(Math.random() * messages.length)
const message = messages[i]
// Check if the strings are safe to print to the terminal. Specifically, the
// string should be plain ASCII, excluding control characters. This is
// paranoid and not strictly necessary since (1) we curate the messages.json
// file by hand and will never include non-ASCII text, and (2) we check the
// strings at package publish time (see test/messages.js). But it doesn't hurt
// to check again in the client and assert that messages are plain ASCII. This
// is the security principle of defense-in-depth.
checkMessage(message)
// Format the message and print it
const formattedMessage = formatMessage(message)
console.log(formattedMessage + '\n')
// Limit the frequency that messages are shown
markShown()
}
module.exports = {
printMessage
}