Skip to content

Commit ec0f91a

Browse files
committed
fix: honor --auth option
1 parent 89b2423 commit ec0f91a

19 files changed

+34
-36
lines changed

src/commands/base-command.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ export default class BaseCommand extends Command {
405405
}
406406
}
407407

408-
async authenticate(tokenFromFlag?: string) {
408+
async authenticate(tokenFromFlag: string) {
409409
const [token] = await getToken(tokenFromFlag)
410410
if (token) {
411411
return token

src/commands/build/build.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ export const build = async (options: OptionValues, command: BaseCommand) => {
2727
const { cachedConfig, siteInfo } = command.netlify
2828
command.setAnalyticsPayload({ dry: options.dry })
2929
// Retrieve Netlify Build options
30-
// @ts-expect-error TS(2554) FIXME: Expected 1 arguments, but got 0.
31-
const [token] = await getToken()
30+
const [token] = await getToken(options.auth)
3231
const settings = await detectFrameworkSettings(command, 'build')
3332

3433
const buildOptions = await getBuildOptions({

src/commands/deploy/deploy.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ const uploadDeployBlobs = async ({
373373
phase: 'start',
374374
})
375375

376-
const [token] = await getToken(false)
376+
const [token] = await getToken(options.auth)
377377

378378
const { success } = await runCoreSteps(['blobs_upload'], {
379379
...options,
@@ -382,7 +382,7 @@ const uploadDeployBlobs = async ({
382382
packagePath,
383383
deployId,
384384
siteId,
385-
token,
385+
token: token === null ? undefined : token,
386386
})
387387

388388
if (!success) {
@@ -563,8 +563,8 @@ const handleBuild = async ({ cachedConfig, currentDir, defaultConfig, deployHand
563563
if (!options.build) {
564564
return {}
565565
}
566-
// @ts-expect-error TS(2554) FIXME: Expected 1 arguments, but got 0.
567-
const [token] = await getToken()
566+
567+
const [token] = await getToken(options.auth)
568568
const resolvedOptions = await getBuildOptions({
569569
cachedConfig,
570570
defaultConfig,

src/commands/init/init.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ export const init = async (options: OptionValues, command: BaseCommand) => {
188188
let { siteInfo } = command.netlify
189189

190190
// Check logged in status
191-
await command.authenticate()
191+
await command.authenticate(options.auth)
192192

193193
// Add .netlify to .gitignore file
194194
await ensureNetlifyIgnore(repositoryRoot)

src/commands/integration/deploy.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -394,8 +394,7 @@ export const getConfiguration = (workingDir) => {
394394
export const deploy = async (options: OptionValues, command: BaseCommand) => {
395395
const { api, cachedConfig, site, siteInfo } = command.netlify
396396
const { id: siteId } = site
397-
// @ts-expect-error TS(2554) FIXME: Expected 1 arguments, but got 0.
398-
const [token] = await getToken()
397+
const [token] = await command.authenticate(options.auth)
399398
const workingDir = resolve(command.workingDir)
400399
const buildOptions = await getBuildOptions({
401400
cachedConfig,

src/commands/link/link.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ or run ${chalk.cyanBright('netlify sites:create')} to create a site.`)
249249
}
250250

251251
export const link = async (options: OptionValues, command: BaseCommand) => {
252-
await command.authenticate()
252+
await command.authenticate(options.auth)
253253

254254
const {
255255
api,

src/commands/lm/lm-setup.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ const configureLFSURL = async function (siteId, api) {
5656
}
5757

5858
export const lmSetup = async (options: OptionValues, command: BaseCommand) => {
59-
await command.authenticate()
59+
await command.authenticate(options.auth)
6060

6161
const { api, site } = command.netlify
6262

src/commands/login/login.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ const msg = function (location) {
1818
}
1919

2020
export const login = async (options: OptionValues, command: BaseCommand) => {
21-
// @ts-expect-error TS(2554) FIXME: Expected 1 arguments, but got 0.
22-
const [accessToken, location] = await getToken()
21+
const [accessToken, location] = await getToken(options.auth)
2322

2423
command.setAnalyticsPayload({ new: options.new })
2524

src/commands/logout/logout.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import { track } from '../../utils/telemetry/index.js'
55
import BaseCommand from '../base-command.js'
66

77
export const logout = async (options: OptionValues, command: BaseCommand) => {
8-
// @ts-expect-error TS(2554) FIXME: Expected 1 arguments, but got 0.
9-
const [accessToken, location] = await getToken()
8+
// for logout, we explicitly don't want to pass `--auth` CLI switch, so instead we are passing empty (falsy) string
9+
const [accessToken, location] = await getToken('')
1010

1111
if (!accessToken) {
1212
log(`Already logged out`)

src/commands/logs/build.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export function getName({ deploy, userId }: { deploy: any; userId: string }) {
3333
}
3434

3535
export const logsBuild = async (options: OptionValues, command: BaseCommand) => {
36-
await command.authenticate()
36+
await command.authenticate(options.auth)
3737
const client = command.netlify.api
3838
const { site } = command.netlify
3939
const { id: siteId } = site

src/commands/open/open-admin.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import BaseCommand from '../base-command.js'
77
export const openAdmin = async (options: OptionValues, command: BaseCommand) => {
88
const { siteInfo } = command.netlify
99

10-
await command.authenticate()
10+
await command.authenticate(options.auth)
1111

1212
log(`Opening "${siteInfo.name}" site admin UI:`)
1313
log(`> ${siteInfo.admin_url}`)

src/commands/open/open-site.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import BaseCommand from '../base-command.js'
77
export const openSite = async (options: OptionValues, command: BaseCommand) => {
88
const { siteInfo } = command.netlify
99

10-
await command.authenticate()
10+
await command.authenticate(options.auth)
1111

1212
const url = siteInfo.ssl_url || siteInfo.url
1313
log(`Opening "${siteInfo.name}" site url:`)

src/commands/sites/sites-create-template.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ const getGitHubLink = ({ options, templateName }) => options.url || `https://git
7272
export const sitesCreateTemplate = async (repository: string, options: OptionValues, command: BaseCommand) => {
7373
const { api } = command.netlify
7474

75-
await command.authenticate()
75+
await command.authenticate(options.auth)
7676

7777
const { globalConfig } = command.netlify
7878
const ghToken = await getGitHubToken({ globalConfig })

src/commands/sites/sites-create.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export const getSiteNameInput = async (name) => {
3131
export const sitesCreate = async (options: OptionValues, command: BaseCommand) => {
3232
const { api } = command.netlify
3333

34-
await command.authenticate()
34+
await command.authenticate(options.auth)
3535

3636
const accounts = await api.listAccountsForUser()
3737

src/commands/sites/sites-list.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export const sitesList = async (options: OptionValues, command: BaseCommand) =>
1212
if (!options.json) {
1313
spinner = startSpinner({ text: 'Loading your sites' })
1414
}
15-
await command.authenticate()
15+
await command.authenticate(options.auth)
1616

1717
const sites = await listSites({ api, options: { filter: 'all' } })
1818
if (!options.json) {

src/commands/status/status-hooks.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import BaseCommand from '../base-command.js'
77
export const statusHooks = async (options: OptionValues, command: BaseCommand) => {
88
const { api, siteInfo } = command.netlify
99

10-
await command.authenticate()
10+
await command.authenticate(options.auth)
1111

1212
const ntlHooks = await api.listHooksBySiteId({ siteId: siteInfo.id })
1313
const data = {

src/commands/status/status.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ import BaseCommand from '../base-command.js'
88
export const status = async (options: OptionValues, command: BaseCommand) => {
99
const { api, globalConfig, site, siteInfo } = command.netlify
1010
const current = globalConfig.get('userId')
11-
// @ts-expect-error TS(2554) FIXME: Expected 1 arguments, but got 0.
12-
const [accessToken] = await getToken()
11+
const [accessToken] = await getToken(options.auth)
1312

1413
if (!accessToken) {
1514
log(`Not logged in. Please log in to see site status.`)

src/commands/watch/watch.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const waitForBuildFinish = async function (api, siteId, spinner) {
5757
}
5858

5959
export const watch = async (options: OptionValues, command: BaseCommand) => {
60-
await command.authenticate()
60+
await command.authenticate(options.auth)
6161
const client = command.netlify.api
6262
let siteId = command.netlify.site.id
6363

src/utils/command-helpers.ts

+12-10
Original file line numberDiff line numberDiff line change
@@ -98,23 +98,22 @@ export const pollForToken = async ({ api, ticket }) => {
9898
try {
9999
const accessToken = await api.getAccessToken(ticket, { timeout: TOKEN_TIMEOUT })
100100
if (!accessToken) {
101-
error('Could not retrieve access token')
101+
return error('Could not retrieve access token')
102102
}
103-
return accessToken
103+
return accessToken as string
104104
} catch (error_) {
105105
// @ts-expect-error TS(2571) FIXME: Object is of type 'unknown'.
106106
if (error_.name === 'TimeoutError') {
107-
error(
107+
return error(
108108
`Timed out waiting for authorization. If you do not have a ${chalk.bold.greenBright(
109109
'Netlify',
110110
)} account, please create one at ${chalk.magenta(
111111
'https://app.netlify.com/signup',
112112
)}, then run ${chalk.cyanBright('netlify login')} again.`,
113113
)
114-
} else {
115-
// @ts-expect-error TS(2345) FIXME: Argument of type 'unknown' is not assignable to pa... Remove this comment to see the full error message
116-
error(error_)
117114
}
115+
// @ts-expect-error TS(2345) FIXME: Argument of type 'unknown' is not assignable to pa... Remove this comment to see the full error message
116+
return error(error_)
118117
} finally {
119118
clearSpinner({ spinner })
120119
}
@@ -123,10 +122,10 @@ export const pollForToken = async ({ api, ticket }) => {
123122
/**
124123
* Get a netlify token
125124
* @param {string} [tokenFromOptions] optional token from the provided --auth options
126-
* @returns {Promise<[null|string, 'flag' | 'env' |'config' |'not found']>}
127125
*/
128-
// @ts-expect-error TS(7006) FIXME: Parameter 'tokenFromOptions' implicitly has an 'an... Remove this comment to see the full error message
129-
export const getToken = async (tokenFromOptions) => {
126+
export const getToken = async (
127+
tokenFromOptions: string,
128+
): Promise<[null | string, 'flag' | 'env' | 'config' | 'not found']> => {
130129
// 1. First honor command flag --auth
131130
if (tokenFromOptions) {
132131
return [tokenFromOptions, 'flag']
@@ -186,7 +185,10 @@ export const warn = (message = '') => {
186185
}
187186

188187
/** Throws an error or logs it */
189-
export const error = (message: Error | string = '', options: { exit?: boolean } = {}) => {
188+
export function error(message: Error | string, options: { exit: false }): void
189+
export function error(message: Error | string): never
190+
export function error(message: Error | string, options: { exit: true }): never
191+
export function error(message: Error | string = '', options: { exit?: boolean } = {}): never | void {
190192
const err =
191193
message instanceof Error
192194
? message

0 commit comments

Comments
 (0)