Skip to content
Draft
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
5 changes: 5 additions & 0 deletions .changeset/green-wolves-wash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/cli-kit': patch
---

Open the browser immediately when starting an interactive login flow instead of waiting for a keypress.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {shopifyFetch} from '../../../public/node/http.js'
import {isTTY} from '../../../public/node/ui.js'
import {err, ok} from '../../../public/node/result.js'
import {AbortError} from '../../../public/node/error.js'
import {isCI} from '../../../public/node/system.js'
import {isCI, openURL} from '../../../public/node/system.js'
import * as output from '../../../public/node/output.js'

import {beforeEach, describe, expect, test, vi} from 'vitest'
import {Response} from 'node-fetch'
Expand All @@ -26,6 +27,7 @@ vi.mock('../../../public/node/system.js')
beforeEach(() => {
vi.mocked(isTTY).mockReturnValue(true)
vi.mocked(isCI).mockReturnValue(false)
vi.mocked(openURL).mockResolvedValue(true)
})

describe('requestDeviceAuthorization', () => {
Expand Down Expand Up @@ -66,6 +68,22 @@ describe('requestDeviceAuthorization', () => {
expect(got).toEqual(dataExpected)
})

test('opens the browser directly in an interactive terminal', async () => {
// Given
const outputInfo = vi.spyOn(output, 'outputInfo')
const response = new Response(JSON.stringify(data))
vi.mocked(shopifyFetch).mockResolvedValue(response)
vi.mocked(identityFqdn).mockResolvedValue('fqdn.com')
vi.mocked(clientId).mockReturnValue('clientId')

// When
await requestDeviceAuthorization(['scope1', 'scope2'])

// Then
expect(openURL).toHaveBeenCalledWith(data.verification_uri_complete)
expect(outputInfo).not.toHaveBeenCalledWith('👉 Press any key to open the login page on your browser')
})

test('when the response is not valid JSON, throw an error with context', async () => {
// Given
const response = new Response('not valid JSON')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import {identityFqdn} from '../../../public/node/context/fqdn.js'
import {shopifyFetch} from '../../../public/node/http.js'
import {outputContent, outputDebug, outputInfo, outputToken} from '../../../public/node/output.js'
import {AbortError, BugError} from '../../../public/node/error.js'
import {isCloudEnvironment} from '../../../public/node/context/local.js'
import {isCI, openURL} from '../../../public/node/system.js'
import {isTTY, keypress} from '../../../public/node/ui.js'

import {Response} from 'node-fetch'

Expand Down Expand Up @@ -81,21 +79,11 @@ export async function requestDeviceAuthorization(scopes: string[]): Promise<Devi
outputInfo(outputContent`User verification code: ${jsonResult.user_code}`)
const linkToken = outputToken.link(jsonResult.verification_uri_complete)

const cloudMessage = () => {
outputInfo(outputContent`👉 Open this link to start the auth process: ${linkToken}`)
}

if (isCloudEnvironment() || !isTTY()) {
cloudMessage()
const opened = await openURL(jsonResult.verification_uri_complete)
Comment thread
gonzaloriestra marked this conversation as resolved.
if (opened) {
outputInfo(outputContent`Opened link to start the auth process: ${linkToken}`)
} else {
outputInfo('👉 Press any key to open the login page on your browser')
await keypress()
const opened = await openURL(jsonResult.verification_uri_complete)
if (opened) {
outputInfo(outputContent`Opened link to start the auth process: ${linkToken}`)
} else {
cloudMessage()
}
outputInfo(outputContent`👉 Open this link to start the auth process: ${linkToken}`)
}

return {
Expand Down
3 changes: 3 additions & 0 deletions packages/cli-kit/src/public/node/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {isTruthy} from './context/utilities.js'
import {renderWarning} from './ui.js'
import {platformAndArch} from './os.js'
import {shouldDisplayColors, outputDebug} from './output.js'
import {isCloudEnvironment} from './context/local.js'
import {execa, ExecaChildProcess} from 'execa'
import supportsHyperlinks from 'supports-hyperlinks'
import which from 'which'
Expand Down Expand Up @@ -51,6 +52,8 @@ interface BuildExecOptions {
* @returns A promise that resolves true if the URL was opened successfully, false otherwise.
*/
export async function openURL(url: string): Promise<boolean> {
if (isCloudEnvironment()) return false

const externalOpen = await import('open')
try {
await externalOpen.default(url)
Expand Down
2 changes: 1 addition & 1 deletion packages/e2e/setup/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export const authFixture = browserFixture.extend<{}, {authLogin: void}>({
if (process.env.DEBUG === '1') process.stdout.write(data)
})

await waitForText(() => output, 'Open this link to start the auth process', CLI_TIMEOUT.short)
await waitForText(() => output, 'link to start the auth process', CLI_TIMEOUT.short)

const stripped = stripAnsi(output)
const urlMatch = stripped.match(/https:\/\/accounts\.shopify\.com\S+/)
Expand Down
2 changes: 1 addition & 1 deletion packages/e2e/setup/global-auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export default async function globalSetup() {
})

try {
await waitForText(() => output, 'Open this link to start the auth process', CLI_TIMEOUT.short)
await waitForText(() => output, 'link to start the auth process', CLI_TIMEOUT.short)

const stripped = stripAnsi(output)
const urlMatch = stripped.match(/https:\/\/accounts\.shopify\.com\S+/)
Expand Down
Loading