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

chore: convert login command #6320

Merged
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/addons/addons-auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const addonsAuth = async (addonName: string, options: OptionValues, comma
log()
log(addon.auth_url)
log()
// @ts-expect-error TS(2345) FIXME: Argument of type '{ url: any; }' is not assignable... Remove this comment to see the full error message

await openBrowser({ url: addon.auth_url })
exit()
}
17 changes: 6 additions & 11 deletions src/commands/base-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import getGlobalConfig from '../utils/get-global-config.js'
import { getSiteByName } from '../utils/get-site.js'
import openBrowser from '../utils/open-browser.js'
import StateConfig from '../utils/state-config.js'
import { NetlifyLog, outro } from '../utils/styles/index.js'
import { identify, reportError, track } from '../utils/telemetry/index.js'

import { type NetlifyOptions } from './types.js'
Expand Down Expand Up @@ -393,7 +394,7 @@ export default class BaseCommand extends Command {

async expensivelyAuthenticate() {
const webUI = process.env.NETLIFY_WEB_UI || 'https://app.netlify.com'
log(`Logging into your Netlify account...`)
NetlifyLog.step('Logging into your Netlify account...')

// Create ticket for auth
const ticket = await this.netlify.api.createTicket({
Expand All @@ -403,8 +404,7 @@ export default class BaseCommand extends Command {
// Open browser for authentication
const authLink = `${webUI}/authorize?response_type=ticket&ticket=${ticket.id}`

log(`Opening ${authLink}`)
// @ts-expect-error TS(2345) FIXME: Argument of type '{ url: string; }' is not assigna... Remove this comment to see the full error message
NetlifyLog.step(`Opening ${authLink}`)
await openBrowser({ url: authLink })

const accessToken = await pollForToken({
Expand Down Expand Up @@ -440,14 +440,9 @@ export default class BaseCommand extends Command {
email,
})

// Log success
log()
log(`${chalk.greenBright('You are now logged into your Netlify account!')}`)
log()
log(`Run ${chalk.cyanBright('netlify status')} for account details`)
log()
log(`To see all available commands run: ${chalk.cyanBright('netlify help')}`)
log()
NetlifyLog.success(`${chalk.greenBright('You are now logged into your Netlify account!')}`)
NetlifyLog.info(`Run ${chalk.cyanBright('netlify status')} for account details`)
outro({ message: `To see all available commands run: ${chalk.cyanBright('netlify help')}` })
return accessToken
}

Expand Down
1 change: 0 additions & 1 deletion src/commands/build/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ 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 settings = await detectFrameworkSettings(command, 'build')

Expand Down
5 changes: 2 additions & 3 deletions src/commands/deploy/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ const uploadDeployBlobs = async ({
phase: 'start',
})

const [token] = await getToken(false)
const [token] = await getToken()

const { success } = await runCoreSteps(['blobs_upload'], {
...options,
Expand Down Expand Up @@ -535,7 +535,6 @@ const handleBuild = async ({ cachedConfig, currentDir, deployHandler, options, p
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,
Expand Down Expand Up @@ -869,7 +868,7 @@ export const deploy = async (options: OptionValues, command: BaseCommand) => {
if (options.open) {
// @ts-expect-error TS(2339) FIXME: Property 'siteUrl' does not exist on type '{}'.
const urlToOpen = deployToProduction ? results.siteUrl : results.deployUrl
// @ts-expect-error TS(2345) FIXME: Argument of type '{ url: any; }' is not assignable... Remove this comment to see the full error message

await openBrowser({ url: urlToOpen })
exit()
}
Expand Down
1 change: 0 additions & 1 deletion src/commands/integration/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,6 @@ 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 workingDir = resolve(command.workingDir)
const buildOptions = await getBuildOptions({
Expand Down
21 changes: 8 additions & 13 deletions src/commands/login/login.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { OptionValues } from 'commander'

import { chalk, exit, getToken, log } from '../../utils/command-helpers.js'
import { chalk, getToken } from '../../utils/command-helpers.js'
import { NetlifyLog, intro, outro } from '../../utils/styles/index.js'
import BaseCommand from '../base-command.js'

// @ts-expect-error TS(7006) FIXME: Parameter 'location' implicitly has an 'any' type.
const msg = function (location) {
const msg = function (location: 'env' | 'flag' | 'config') {
switch (location) {
case 'env':
return 'via process.env.NETLIFY_AUTH_TOKEN set in your terminal session'
Expand All @@ -18,21 +18,16 @@ const msg = function (location) {
}

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

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

if (accessToken && !options.new) {
log(`Already logged in ${msg(location)}`)
log()
log(`Run ${chalk.cyanBright('netlify status')} for account details`)
log()
log(`or run ${chalk.cyanBright('netlify switch')} to switch accounts`)
log()
log(`To see all available commands run: ${chalk.cyanBright('netlify help')}`)
log()
return exit()
NetlifyLog.success(`Already logged in ${msg(location)}`)
NetlifyLog.message(`Run ${chalk.cyanBright('netlify status')} for account details`)
NetlifyLog.message(`or run ${chalk.cyanBright('netlify switch')} to switch accounts`)
outro({ message: `To see all available commands run: ${chalk.cyanBright('netlify help')}`, exit: true })
}

await command.expensivelyAuthenticate()
Expand Down
1 change: 0 additions & 1 deletion src/commands/logout/logout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ 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()

if (!accessToken) {
Expand Down
1 change: 0 additions & 1 deletion src/commands/open/open-admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export const openAdmin = async (options: OptionValues, command: BaseCommand) =>
log(`Opening "${siteInfo.name}" site admin UI:`)
log(`> ${siteInfo.admin_url}`)

// @ts-expect-error TS(2345) FIXME: Argument of type '{ url: any; }' is not assignable... Remove this comment to see the full error message
await openBrowser({ url: siteInfo.admin_url })
exit()
}
1 change: 0 additions & 1 deletion src/commands/open/open-site.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export const openSite = async (options: OptionValues, command: BaseCommand) => {
log(`Opening "${siteInfo.name}" site url:`)
log(`> ${url}`)

// @ts-expect-error TS(2345) FIXME: Argument of type '{ url: any; }' is not assignable... Remove this comment to see the full error message
await openBrowser({ url })
exit()
}
1 change: 0 additions & 1 deletion src/commands/status/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ 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()

if (!accessToken) {
Expand Down
29 changes: 10 additions & 19 deletions src/utils/command-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ import WSL from 'is-wsl'
import debounce from 'lodash/debounce.js'
import terminalLink from 'terminal-link'

import { clearSpinner, startSpinner } from '../lib/spinner.js'

import getGlobalConfig from './get-global-config.js'
import getPackageJson from './get-package-json.js'
import { NetlifyLog, spinner } from './styles/index.js'
import { reportError } from './telemetry/report-error.js'

/** The parsed process argv without the binary only arguments and flags */
Expand Down Expand Up @@ -94,39 +93,31 @@ const TOKEN_TIMEOUT = 3e5
*/
// @ts-expect-error TS(7031) FIXME: Binding element 'api' implicitly has an 'any' type... Remove this comment to see the full error message
export const pollForToken = async ({ api, ticket }) => {
const spinner = startSpinner({ text: 'Waiting for authorization...' })
const loading = spinner()
loading.start('Waiting for authorization...')
try {
const accessToken = await api.getAccessToken(ticket, { timeout: TOKEN_TIMEOUT })
if (!accessToken) {
error('Could not retrieve access token')
NetlifyLog.error('Could not retrieve access token')
}
loading.stop('Retrieved access token')
return accessToken
} catch (error_) {
// @ts-expect-error TS(2571) FIXME: Object is of type 'unknown'.
if (error_.name === 'TimeoutError') {
error(
} catch (error_: unknown) {
if ((error_ as Error).name === 'TimeoutError') {
NetlifyLog.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_)
NetlifyLog.error(error_)
}
} finally {
clearSpinner({ spinner })
}
}

/**
* 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) => {
// 1. First honor command flag --auth
if (tokenFromOptions) {
return [tokenFromOptions, 'flag']
Expand Down
1 change: 0 additions & 1 deletion src/utils/gh-auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ export const authWithNetlify = async () => {
})
const url = `${webUI}/cli?${urlParams.toString()}`

// @ts-expect-error TS(2345) FIXME: Argument of type '{ url: string; }' is not assigna... Remove this comment to see the full error message
await openBrowser({ url })

return deferredPromise
Expand Down
30 changes: 13 additions & 17 deletions src/utils/open-browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,22 @@ import process from 'process'
import open from 'better-opn'
import isDockerContainer from 'is-docker'

import { chalk, log } from './command-helpers.js'
import { chalk } from './command-helpers.js'
import { NetlifyLog } from './styles/index.js'

// @ts-expect-error TS(7031) FIXME: Binding element 'message' implicitly has an 'any' ... Remove this comment to see the full error message
const unableToOpenBrowserMessage = function ({ message, url }) {
log('---------------------------')
log(chalk.redBright(`Error: Unable to open browser automatically: ${message}`))
log(chalk.cyan('Please open your browser and open the URL below:'))
log(chalk.bold(url))
log('---------------------------')
const unableToOpenBrowserMessage = function ({ message, url }: { message: string; url: string }) {
NetlifyLog.error(chalk.redBright(`Error: Unable to open browser automatically: ${message}`), { exit: false })
NetlifyLog.message(chalk.cyan('Please open your browser and open the URL below:'))
NetlifyLog.message(chalk.bold(url))
}

/**
* Opens a browser and logs a message if it is not possible
* @param {object} config
* @param {string} config.url The url to open
* @param {boolean} [config.silentBrowserNoneError]
* @returns {Promise<void>}
*/
// @ts-expect-error TS(7031) FIXME: Binding element 'silentBrowserNoneError' implicitl... Remove this comment to see the full error message
const openBrowser = async function ({ silentBrowserNoneError, url }) {
const openBrowser = async function ({
silentBrowserNoneError,
url,
}: {
silentBrowserNoneError?: boolean
url: string
}) {
if (isDockerContainer()) {
unableToOpenBrowserMessage({ url, message: 'Running inside a docker container' })
return
Expand Down
Loading