Skip to content
This repository was archived by the owner on Aug 10, 2020. It is now read-only.

Commit d1f89d6

Browse files
committed
add chalk API proxy
1 parent e77acca commit d1f89d6

File tree

2 files changed

+58
-5
lines changed

2 files changed

+58
-5
lines changed

src/index.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
const { Command } = require('@oclif/command')
2-
const chalk = require('chalk')
32
const API = require('netlify')
3+
const merge = require('lodash.merge')
44
const { format, inspect } = require('util')
55
const getConfigPath = require('./utils/get-config-path')
66
const readConfig = require('./utils/read-config')
77
const globalConfig = require('./global-config')
88
const StateConfig = require('./state')
9+
const chalkInstance = require('./utils/chalk')
910
const openBrowser = require('./utils/open-browser')
1011
const findRoot = require('./utils/find-root')
1112
const { track, identify } = require('./utils/telemetry')
12-
const merge = require('lodash.merge')
13+
1314
const argv = require('minimist')(process.argv.slice(2))
1415
const { NETLIFY_AUTH_TOKEN } = process.env
1516

@@ -127,6 +128,10 @@ class BaseCommand extends Command {
127128
}, opts))
128129
}
129130

131+
get chalk() {
132+
// If --json flag disable chalk colors
133+
return chalkInstance(argv.json)
134+
}
130135
/**
131136
* Get user netlify API token
132137
* @param {string} - [tokenFromFlag] - value passed in by CLI flag
@@ -212,11 +217,11 @@ class BaseCommand extends Command {
212217

213218
// Log success
214219
this.log()
215-
this.log(`${chalk.greenBright('You are now logged into your Netlify account!')}`)
220+
this.log(`${this.chalk.greenBright('You are now logged into your Netlify account!')}`)
216221
this.log()
217-
this.log(`Run ${chalk.cyanBright('netlify status')} for account details`)
222+
this.log(`Run ${this.chalk.cyanBright('netlify status')} for account details`)
218223
this.log()
219-
this.log(`To see all available commands run: ${chalk.cyanBright('netlify help')}`)
224+
this.log(`To see all available commands run: ${this.chalk.cyanBright('netlify help')}`)
220225
this.log()
221226
return accessToken
222227
}

src/utils/chalk.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
const chalk = require('chalk')
2+
3+
/**
4+
* Chalk instance for CLI
5+
* @param {boolean} noColors - disable chalk colors
6+
* @return {object} - chalk instance or proxy noOp
7+
*/
8+
function safeChalk(noColors) {
9+
/* if no colors return proxy to chalk API */
10+
if (noColors) {
11+
return neverNull(chalk)
12+
}
13+
return chalk
14+
}
15+
16+
function noop() {}
17+
18+
function neverNull(obj) {
19+
function match(some, none = noop) {
20+
return obj != null ? some(obj) : none()
21+
}
22+
return new Proxy((some, none) => {
23+
if (some) {
24+
// has value return it with no chalk wrapper
25+
return some
26+
}
27+
if (!some && !none) return obj
28+
return match(some, none)
29+
}, {
30+
get: (target, key) => {
31+
const obj = target()
32+
if (obj !== null && typeof obj === 'object') {
33+
return neverNull(obj[key])
34+
} else {
35+
return neverNull()
36+
}
37+
},
38+
set: (target, key, val) => {
39+
const obj = target()
40+
if (obj !== null && typeof obj === 'object') {
41+
obj[key] = val
42+
}
43+
return true
44+
}
45+
})
46+
}
47+
48+
module.exports = safeChalk

0 commit comments

Comments
 (0)