1
1
const { Command } = require ( '@oclif/command' )
2
- const chalk = require ( 'chalk' )
3
2
const API = require ( 'netlify' )
3
+ const merge = require ( 'lodash.merge' )
4
+ const { format, inspect } = require ( 'util' )
4
5
const getConfigPath = require ( './utils/get-config-path' )
5
6
const readConfig = require ( './utils/read-config' )
6
7
const globalConfig = require ( './global-config' )
7
8
const StateConfig = require ( './state' )
9
+ const chalkInstance = require ( './utils/chalk' )
8
10
const openBrowser = require ( './utils/open-browser' )
9
11
const findRoot = require ( './utils/find-root' )
10
12
const { track, identify } = require ( './utils/telemetry' )
11
- const merge = require ( 'lodash.merge' )
13
+
12
14
const argv = require ( 'minimist' ) ( process . argv . slice ( 2 ) )
13
15
const { NETLIFY_AUTH_TOKEN } = process . env
14
16
@@ -24,7 +26,9 @@ class BaseCommand extends Command {
24
26
async init ( err ) {
25
27
const projectRoot = findRoot ( process . cwd ( ) )
26
28
// Grab netlify API token
27
- const [ token ] = this . getConfigToken ( argv . auth )
29
+ const authViaFlag = getAuthArg ( argv )
30
+
31
+ const [ token ] = this . getConfigToken ( authViaFlag )
28
32
// Get site config from netlify.toml
29
33
const configPath = getConfigPath ( projectRoot )
30
34
// TODO: https://github.com/request/caseless to handle key casing issues
@@ -65,6 +69,69 @@ class BaseCommand extends Command {
65
69
}
66
70
}
67
71
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
+ }
68
135
/**
69
136
* Get user netlify API token
70
137
* @param {string } - [tokenFromFlag] - value passed in by CLI flag
@@ -150,14 +217,22 @@ class BaseCommand extends Command {
150
217
151
218
// Log success
152
219
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!' ) } ` )
154
221
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` )
156
223
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' ) } ` )
158
225
this . log ( )
159
226
return accessToken
160
227
}
161
228
}
162
229
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
0 commit comments