forked from netlify/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchalk.js
More file actions
51 lines (47 loc) · 1.08 KB
/
Copy pathchalk.js
File metadata and controls
51 lines (47 loc) · 1.08 KB
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
const chalk = require('chalk')
/**
* Chalk instance for CLI
* @param {boolean} noColors - disable chalk colors
* @return {object} - chalk instance or proxy noOp
*/
function safeChalk(noColors) {
/* if no colors return proxy to chalk API */
if (noColors) {
return neverNull(chalk)
}
return chalk
}
function noop() {}
function neverNull(obj) {
function match(some, none = noop) {
return obj != null ? some(obj) : none()
}
return new Proxy(
(some, none) => {
if (some) {
// has value return it with no chalk wrapper
return some
}
if (!some && !none) return obj
return match(some, none)
},
{
get: (target, key) => {
const obj = target()
if (obj !== null && typeof obj === 'object') {
return neverNull(obj[key])
} else {
return neverNull()
}
},
set: (target, key, val) => {
const obj = target()
if (obj !== null && typeof obj === 'object') {
obj[key] = val
}
return true
},
}
)
}
module.exports = safeChalk