Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: honor --auth option #6838

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/commands/base-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ export default class BaseCommand extends Command {
}
}

async authenticate(tokenFromFlag?: string) {
async authenticate(tokenFromFlag: string) {
const [token] = await getToken(tokenFromFlag)
if (token) {
return token
Expand Down
3 changes: 1 addition & 2 deletions src/commands/build/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ export const build = async (options: OptionValues, command: BaseCommand) => {
const { cachedConfig, siteInfo } = command.netlify
command.setAnalyticsPayload({ dry: options.dry })
// Retrieve Netlify Build options
// @ts-expect-error TS(2554) FIXME: Expected 1 arguments, but got 0.
const [token] = await getToken()
const [token] = await getToken(options.auth)
const settings = await detectFrameworkSettings(command, 'build')

const buildOptions = await getBuildOptions({
Expand Down
23 changes: 15 additions & 8 deletions src/commands/deploy/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import {
chalk,
error,
exit,
getToken,
log,
logJson,
warn,
Expand Down Expand Up @@ -357,13 +356,15 @@ const uploadDeployBlobs = async ({
packagePath,
silent,
siteId,
token,
}: {
cachedConfig: $TSFixMe
deployId: string
options: OptionValues
packagePath?: string
silent: boolean
siteId: string
token: string
}) => {
const statusCb = silent ? () => {} : deployProgressCb()

Expand All @@ -373,16 +374,14 @@ const uploadDeployBlobs = async ({
phase: 'start',
})

const [token] = await getToken(false)

const { success } = await runCoreSteps(['blobs_upload'], {
...options,
quiet: silent,
cachedConfig,
packagePath,
deployId,
siteId,
token,
token: token === null ? undefined : token,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

token here is of type string | null, while runCoreSteps expect it to be string | undefined

Overall at this stage there should be a token always, otherwise we would bail much earlier, but stuff like this is annoying with TS and probably would need more refactoring to not have to deal with null / undefined cases.

})

if (!success) {
Expand Down Expand Up @@ -435,9 +434,11 @@ const runDeploy = async ({
skipFunctionsCache,
// @ts-expect-error TS(7031) FIXME: Binding element 'title' implicitly has an 'any' ty... Remove this comment to see the full error message
title,
token,
}: {
functionsFolder?: string
command: BaseCommand
token: string
}): Promise<{
siteId: string
siteName: string
Expand Down Expand Up @@ -501,6 +502,7 @@ const runDeploy = async ({
options,
cachedConfig: command.netlify.cachedConfig,
packagePath: command.workspacePackage,
token,
})

results = await deploySite(command, api, siteId, deployFolder, {
Expand Down Expand Up @@ -559,12 +561,11 @@ const runDeploy = async ({
* @returns
*/
// @ts-expect-error TS(7031) FIXME: Binding element 'cachedConfig' implicitly has an '... Remove this comment to see the full error message
const handleBuild = async ({ cachedConfig, currentDir, defaultConfig, deployHandler, options, packagePath }) => {
const handleBuild = async ({ cachedConfig, currentDir, defaultConfig, deployHandler, options, packagePath, token }) => {
if (!options.build) {
return {}
}
// @ts-expect-error TS(2554) FIXME: Expected 1 arguments, but got 0.
const [token] = await getToken()

const resolvedOptions = await getBuildOptions({
cachedConfig,
defaultConfig,
Expand Down Expand Up @@ -704,6 +705,8 @@ const prepAndRunDeploy = async ({
siteData,
// @ts-expect-error TS(7031) FIXME: Binding element 'siteId' implicitly has an 'any' t... Remove this comment to see the full error message
siteId,
// @ts-expect-error TS(7031) FIXME: Binding element 'token' implicitly has an 'any' ty... Remove this comment to see the full error message
token,
// @ts-expect-error TS(7031) FIXME: Binding element 'workingDir' implicitly has an 'an... Remove this comment to see the full error message
workingDir,
}) => {
Expand Down Expand Up @@ -777,6 +780,7 @@ const prepAndRunDeploy = async ({
siteId,
skipFunctionsCache: options.skipFunctionsCache,
title: options.message,
token,
})

return results
Expand All @@ -798,7 +802,7 @@ export const deploy = async (options: OptionValues, command: BaseCommand) => {
return error('--context flag is only available when using the --build flag')
}

await command.authenticate(options.auth)
const token = await command.authenticate(options.auth)

let siteId = site.id || options.site

Expand Down Expand Up @@ -853,6 +857,7 @@ export const deploy = async (options: OptionValues, command: BaseCommand) => {
defaultConfig: getDefaultConfig(settings),
currentDir: command.workingDir,
options,
token,
// @ts-expect-error TS(7031) FIXME: Binding element 'netlifyConfig' implicitly has an ... Remove this comment to see the full error message
deployHandler: async ({ netlifyConfig }) => {
results = await prepAndRunDeploy({
Expand All @@ -864,6 +869,7 @@ export const deploy = async (options: OptionValues, command: BaseCommand) => {
config: netlifyConfig,
siteData,
siteId,
token,
deployToProduction,
})

Expand All @@ -880,6 +886,7 @@ export const deploy = async (options: OptionValues, command: BaseCommand) => {
config: command.netlify.config,
siteData,
siteId,
token,
deployToProduction,
})
}
Expand Down
2 changes: 1 addition & 1 deletion src/commands/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ export const init = async (options: OptionValues, command: BaseCommand) => {
let { siteInfo } = command.netlify

// Check logged in status
await command.authenticate()
await command.authenticate(options.auth)

// Add .netlify to .gitignore file
await ensureNetlifyIgnore(repositoryRoot)
Expand Down
5 changes: 2 additions & 3 deletions src/commands/integration/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import fetch from 'node-fetch'
import { z } from 'zod'

import { getBuildOptions } from '../../lib/build.js'
import { chalk, getToken, log } from '../../utils/command-helpers.js'
import { chalk, log } from '../../utils/command-helpers.js'
import { getSiteInformation } from '../../utils/dev.js'
import BaseCommand from '../base-command.js'
import { checkOptions } from '../build/build.js'
Expand Down Expand Up @@ -394,8 +394,7 @@ export const getConfiguration = (workingDir) => {
export const deploy = async (options: OptionValues, command: BaseCommand) => {
const { api, cachedConfig, site, siteInfo } = command.netlify
const { id: siteId } = site
// @ts-expect-error TS(2554) FIXME: Expected 1 arguments, but got 0.
const [token] = await getToken()
const token = await command.authenticate(options.auth)
const workingDir = resolve(command.workingDir)
const buildOptions = await getBuildOptions({
cachedConfig,
Expand Down
2 changes: 1 addition & 1 deletion src/commands/link/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ or run ${chalk.cyanBright('netlify sites:create')} to create a site.`)
}

export const link = async (options: OptionValues, command: BaseCommand) => {
await command.authenticate()
await command.authenticate(options.auth)

const {
api,
Expand Down
2 changes: 1 addition & 1 deletion src/commands/lm/lm-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const configureLFSURL = async function (siteId, api) {
}

export const lmSetup = async (options: OptionValues, command: BaseCommand) => {
await command.authenticate()
await command.authenticate(options.auth)

const { api, site } = command.netlify

Expand Down
5 changes: 3 additions & 2 deletions src/commands/login/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ const msg = function (location) {
}

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

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

Expand Down
5 changes: 3 additions & 2 deletions src/commands/logout/logout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import { track } from '../../utils/telemetry/index.js'
import BaseCommand from '../base-command.js'

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

if (!accessToken) {
log(`Already logged out`)
Expand Down
2 changes: 1 addition & 1 deletion src/commands/logs/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function getName({ deploy, userId }: { deploy: any; userId: string }) {
}

export const logsBuild = async (options: OptionValues, command: BaseCommand) => {
await command.authenticate()
await command.authenticate(options.auth)
const client = command.netlify.api
const { site } = command.netlify
const { id: siteId } = site
Expand Down
2 changes: 1 addition & 1 deletion src/commands/open/open-admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import BaseCommand from '../base-command.js'
export const openAdmin = async (options: OptionValues, command: BaseCommand) => {
const { siteInfo } = command.netlify

await command.authenticate()
await command.authenticate(options.auth)

log(`Opening "${siteInfo.name}" site admin UI:`)
log(`> ${siteInfo.admin_url}`)
Expand Down
2 changes: 1 addition & 1 deletion src/commands/open/open-site.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import BaseCommand from '../base-command.js'
export const openSite = async (options: OptionValues, command: BaseCommand) => {
const { siteInfo } = command.netlify

await command.authenticate()
await command.authenticate(options.auth)

const url = siteInfo.ssl_url || siteInfo.url
log(`Opening "${siteInfo.name}" site url:`)
Expand Down
2 changes: 1 addition & 1 deletion src/commands/sites/sites-create-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const getGitHubLink = ({ options, templateName }) => options.url || `https://git
export const sitesCreateTemplate = async (repository: string, options: OptionValues, command: BaseCommand) => {
const { api } = command.netlify

await command.authenticate()
await command.authenticate(options.auth)

const { globalConfig } = command.netlify
const ghToken = await getGitHubToken({ globalConfig })
Expand Down
2 changes: 1 addition & 1 deletion src/commands/sites/sites-create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const getSiteNameInput = async (name) => {
export const sitesCreate = async (options: OptionValues, command: BaseCommand) => {
const { api } = command.netlify

await command.authenticate()
await command.authenticate(options.auth)

const accounts = await api.listAccountsForUser()

Expand Down
2 changes: 1 addition & 1 deletion src/commands/sites/sites-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const sitesList = async (options: OptionValues, command: BaseCommand) =>
if (!options.json) {
spinner = startSpinner({ text: 'Loading your sites' })
}
await command.authenticate()
await command.authenticate(options.auth)

const sites = await listSites({ api, options: { filter: 'all' } })
if (!options.json) {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/status/status-hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import BaseCommand from '../base-command.js'
export const statusHooks = async (options: OptionValues, command: BaseCommand) => {
const { api, siteInfo } = command.netlify

await command.authenticate()
await command.authenticate(options.auth)

const ntlHooks = await api.listHooksBySiteId({ siteId: siteInfo.id })
const data = {
Expand Down
3 changes: 1 addition & 2 deletions src/commands/status/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import BaseCommand from '../base-command.js'
export const status = async (options: OptionValues, command: BaseCommand) => {
const { api, globalConfig, site, siteInfo } = command.netlify
const current = globalConfig.get('userId')
// @ts-expect-error TS(2554) FIXME: Expected 1 arguments, but got 0.
const [accessToken] = await getToken()
const [accessToken] = await getToken(options.auth)

if (!accessToken) {
log(`Not logged in. Please log in to see site status.`)
Expand Down
2 changes: 1 addition & 1 deletion src/commands/watch/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const waitForBuildFinish = async function (api, siteId, spinner) {
}

export const watch = async (options: OptionValues, command: BaseCommand) => {
await command.authenticate()
await command.authenticate(options.auth)
const client = command.netlify.api
let siteId = command.netlify.site.id

Expand Down
22 changes: 12 additions & 10 deletions src/utils/command-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,23 +98,22 @@ export const pollForToken = async ({ api, ticket }) => {
try {
const accessToken = await api.getAccessToken(ticket, { timeout: TOKEN_TIMEOUT })
if (!accessToken) {
error('Could not retrieve access token')
return error('Could not retrieve access token')
}
return accessToken
return accessToken as string
} catch (error_) {
// @ts-expect-error TS(2571) FIXME: Object is of type 'unknown'.
if (error_.name === 'TimeoutError') {
error(
return error(
`Timed out waiting for authorization. If you do not have a ${chalk.bold.greenBright(
'Netlify',
)} account, please create one at ${chalk.magenta(
'https://app.netlify.com/signup',
)}, then run ${chalk.cyanBright('netlify login')} again.`,
)
} else {
// @ts-expect-error TS(2345) FIXME: Argument of type 'unknown' is not assignable to pa... Remove this comment to see the full error message
error(error_)
}
// @ts-expect-error TS(2345) FIXME: Argument of type 'unknown' is not assignable to pa... Remove this comment to see the full error message
return error(error_)
} finally {
clearSpinner({ spinner })
}
Expand All @@ -123,10 +122,10 @@ export const pollForToken = async ({ api, ticket }) => {
/**
* Get a netlify token
* @param {string} [tokenFromOptions] optional token from the provided --auth options
* @returns {Promise<[null|string, 'flag' | 'env' |'config' |'not found']>}
*/
// @ts-expect-error TS(7006) FIXME: Parameter 'tokenFromOptions' implicitly has an 'an... Remove this comment to see the full error message
export const getToken = async (tokenFromOptions) => {
export const getToken = async (
tokenFromOptions: string,
): Promise<[null | string, 'flag' | 'env' | 'config' | 'not found']> => {
// 1. First honor command flag --auth
if (tokenFromOptions) {
return [tokenFromOptions, 'flag']
Expand Down Expand Up @@ -186,7 +185,10 @@ export const warn = (message = '') => {
}

/** Throws an error or logs it */
export const error = (message: Error | string = '', options: { exit?: boolean } = {}) => {
export function error(message: Error | string, options: { exit: false }): void
export function error(message: Error | string): never
export function error(message: Error | string, options: { exit: true }): never
export function error(message: Error | string = '', options: { exit?: boolean } = {}): never | void {
const err =
message instanceof Error
? message
Expand Down
Loading
Loading