@@ -22,8 +22,7 @@ class BaseCommand extends Command {
22
22
async init ( err ) {
23
23
const projectRoot = findRoot ( process . cwd ( ) )
24
24
// Grab netlify API token
25
- const token = this . configToken
26
-
25
+ const token = getConfigToken ( )
27
26
// Get site config from netlify.toml
28
27
const configPath = getConfigPath ( projectRoot )
29
28
// TODO: https://github.com/request/caseless to handle key casing issues
@@ -63,11 +62,6 @@ class BaseCommand extends Command {
63
62
this . netlify . api . accessToken = token
64
63
}
65
64
66
- get configToken ( ) {
67
- const userId = globalConfig . get ( 'userId' )
68
- return globalConfig . get ( `users.${ userId } .auth.token` )
69
- }
70
-
71
65
async isLoggedIn ( ) {
72
66
try {
73
67
await this . netlify . api . getCurrentUser ( )
@@ -77,8 +71,8 @@ class BaseCommand extends Command {
77
71
}
78
72
}
79
73
80
- async authenticate ( authToken ) {
81
- const token = authToken || process . env . NETLIFY_AUTH_TOKEN || this . configToken
74
+ async authenticate ( authTokenFromFlag ) {
75
+ const token = getConfigToken ( authTokenFromFlag )
82
76
if ( ! token ) {
83
77
return this . expensivelyAuthenticate ( )
84
78
} else {
@@ -152,4 +146,23 @@ class BaseCommand extends Command {
152
146
}
153
147
}
154
148
149
+ /**
150
+ * Get user netlify API token
151
+ * @param {string } authTokenFromFlag - value passed in by CLI flag
152
+ * @return {string } - resolved Netlify API token
153
+ */
154
+ function getConfigToken ( authTokenFromFlag ) {
155
+ // 1. First honor command flag --auth
156
+ if ( authTokenFromFlag ) {
157
+ return authTokenFromFlag
158
+ }
159
+ // 2. then Check ENV var
160
+ if ( process . env . NETLIFY_AUTH_TOKEN ) {
161
+ return process . env . NETLIFY_AUTH_TOKEN
162
+ }
163
+ // 3. If no env var use global user setting
164
+ const userId = globalConfig . get ( 'userId' )
165
+ return globalConfig . get ( `users.${ userId } .auth.token` )
166
+ }
167
+
155
168
module . exports = BaseCommand
0 commit comments