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

Commit b7689ca

Browse files
authored
Merge pull request #13 from netlify/addGlobalFlagSupport
add global flag support for —silent, —json, —auth
2 parents d1007bd + d1f89d6 commit b7689ca

File tree

4 files changed

+147
-11
lines changed

4 files changed

+147
-11
lines changed

package-lock.json

Lines changed: 16 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
"dependencies": {
7373
"@iarna/toml": "^2.2.1",
7474
"@oclif/command": "^1.5.8",
75+
"@oclif/parser": "^3.8.3",
7576
"chalk": "^2.4.1",
7677
"ci-info": "^2.0.0",
7778
"cli-ux": "^5.0.0",

src/index.js

Lines changed: 82 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
const { Command } = require('@oclif/command')
2-
const chalk = require('chalk')
32
const API = require('netlify')
3+
const merge = require('lodash.merge')
4+
const { format, inspect } = require('util')
45
const getConfigPath = require('./utils/get-config-path')
56
const readConfig = require('./utils/read-config')
67
const globalConfig = require('./global-config')
78
const StateConfig = require('./state')
9+
const chalkInstance = require('./utils/chalk')
810
const openBrowser = require('./utils/open-browser')
911
const findRoot = require('./utils/find-root')
1012
const { track, identify } = require('./utils/telemetry')
11-
const merge = require('lodash.merge')
13+
1214
const argv = require('minimist')(process.argv.slice(2))
1315
const { NETLIFY_AUTH_TOKEN } = process.env
1416

@@ -24,7 +26,9 @@ class BaseCommand extends Command {
2426
async init(err) {
2527
const projectRoot = findRoot(process.cwd())
2628
// Grab netlify API token
27-
const [ token ] = this.getConfigToken(argv.auth)
29+
const authViaFlag = getAuthArg(argv)
30+
31+
const [ token ] = this.getConfigToken(authViaFlag)
2832
// Get site config from netlify.toml
2933
const configPath = getConfigPath(projectRoot)
3034
// TODO: https://github.com/request/caseless to handle key casing issues
@@ -65,6 +69,69 @@ class BaseCommand extends Command {
6569
}
6670
}
6771

72+
logJson(message = '', ...args) {
73+
/* Only run json logger when --json flag present */
74+
if (!argv.json) {
75+
return
76+
}
77+
process.stdout.write(JSON.stringify(message, null, 2))
78+
}
79+
80+
log(message = '', ...args) {
81+
/* If --silent or --json flag passed disable logger */
82+
if (argv.silent || argv.json) {
83+
return
84+
}
85+
message = typeof message === 'string' ? message : inspect(message)
86+
process.stdout.write(format(message, ...args) + '\n')
87+
}
88+
89+
/* Modified flag parser to support global --auth, --json, & --silent flags */
90+
parse(opts, argv = this.argv) {
91+
/* Set flags object for commands without flags */
92+
if (!opts.flags) {
93+
opts.flags = {}
94+
}
95+
/* enrich parse with global flags */
96+
const globalFlags = {}
97+
if (!opts.flags.silent) {
98+
globalFlags['silent'] = {
99+
parse: (b, _) => b,
100+
description: 'Silence CLI output',
101+
allowNo: false,
102+
type: 'boolean'
103+
}
104+
}
105+
if (!opts.flags.json) {
106+
globalFlags['json'] = {
107+
parse: (b, _) => b,
108+
description: 'Output return values as JSON',
109+
allowNo: false,
110+
type: 'boolean'
111+
}
112+
}
113+
if (!opts.flags.auth) {
114+
globalFlags['auth'] = {
115+
parse: (b, _) => b,
116+
description: 'Netlify auth token',
117+
input: [],
118+
multiple: false,
119+
type: 'option'
120+
}
121+
}
122+
123+
// enrich with flags here
124+
opts.flags = Object.assign({}, opts.flags, globalFlags)
125+
126+
return require('@oclif/parser').parse(argv, Object.assign({}, {
127+
context: this,
128+
}, opts))
129+
}
130+
131+
get chalk() {
132+
// If --json flag disable chalk colors
133+
return chalkInstance(argv.json)
134+
}
68135
/**
69136
* Get user netlify API token
70137
* @param {string} - [tokenFromFlag] - value passed in by CLI flag
@@ -150,14 +217,22 @@ class BaseCommand extends Command {
150217

151218
// Log success
152219
this.log()
153-
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!')}`)
154221
this.log()
155-
this.log(`Run ${chalk.cyanBright('netlify status')} for account details`)
222+
this.log(`Run ${this.chalk.cyanBright('netlify status')} for account details`)
156223
this.log()
157-
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')}`)
158225
this.log()
159226
return accessToken
160227
}
161228
}
162229

163-
module.exports = BaseCommand
230+
function getAuthArg(cliArgs) {
231+
// If deploy command. Support shorthand 'a' flag
232+
if (cliArgs && cliArgs._ && cliArgs._[0] === 'deploy') {
233+
return cliArgs.auth || cliArgs.a
234+
}
235+
return cliArgs.auth
236+
}
237+
238+
module.exports = BaseCommand

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)