Skip to content

Commit

Permalink
fix: honor --auth option
Browse files Browse the repository at this point in the history
  • Loading branch information
pieh committed Sep 25, 2024
1 parent 89b2423 commit ec0f91a
Show file tree
Hide file tree
Showing 19 changed files with 34 additions and 36 deletions.
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
8 changes: 4 additions & 4 deletions src/commands/deploy/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ const uploadDeployBlobs = async ({
phase: 'start',
})

const [token] = await getToken(false)
const [token] = await getToken(options.auth)

const { success } = await runCoreSteps(['blobs_upload'], {
...options,
Expand All @@ -382,7 +382,7 @@ const uploadDeployBlobs = async ({
packagePath,
deployId,
siteId,
token,
token: token === null ? undefined : token,
})

if (!success) {
Expand Down Expand Up @@ -563,8 +563,8 @@ const handleBuild = async ({ cachedConfig, currentDir, defaultConfig, deployHand
if (!options.build) {
return {}
}
// @ts-expect-error TS(2554) FIXME: Expected 1 arguments, but got 0.
const [token] = await getToken()

const [token] = await getToken(options.auth)
const resolvedOptions = await getBuildOptions({
cachedConfig,
defaultConfig,
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
3 changes: 1 addition & 2 deletions src/commands/integration/deploy.ts
Original file line number Diff line number Diff line change
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
3 changes: 1 addition & 2 deletions src/commands/login/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ 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()
const [accessToken, location] = await getToken(options.auth)

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

Expand Down
4 changes: 2 additions & 2 deletions src/commands/logout/logout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ 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 logout, 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

0 comments on commit ec0f91a

Please sign in to comment.