diff --git a/.changeset/green-wolves-wash.md b/.changeset/green-wolves-wash.md new file mode 100644 index 00000000000..d4a47cee66a --- /dev/null +++ b/.changeset/green-wolves-wash.md @@ -0,0 +1,5 @@ +--- +'@shopify/cli-kit': patch +--- + +Open the browser immediately when starting an interactive login flow instead of waiting for a keypress. diff --git a/packages/cli-kit/src/private/node/session/device-authorization.test.ts b/packages/cli-kit/src/private/node/session/device-authorization.test.ts index 32349ba0ee2..3b70a51b702 100644 --- a/packages/cli-kit/src/private/node/session/device-authorization.test.ts +++ b/packages/cli-kit/src/private/node/session/device-authorization.test.ts @@ -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' @@ -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', () => { @@ -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') diff --git a/packages/cli-kit/src/private/node/session/device-authorization.ts b/packages/cli-kit/src/private/node/session/device-authorization.ts index 14e8e367a9f..4a35e06d194 100644 --- a/packages/cli-kit/src/private/node/session/device-authorization.ts +++ b/packages/cli-kit/src/private/node/session/device-authorization.ts @@ -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' @@ -81,21 +79,11 @@ export async function requestDeviceAuthorization(scopes: string[]): Promise { - outputInfo(outputContent`👉 Open this link to start the auth process: ${linkToken}`) - } - - if (isCloudEnvironment() || !isTTY()) { - cloudMessage() + const opened = await openURL(jsonResult.verification_uri_complete) + 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 { diff --git a/packages/e2e/setup/auth.ts b/packages/e2e/setup/auth.ts index e4b88992db3..93caef60194 100644 --- a/packages/e2e/setup/auth.ts +++ b/packages/e2e/setup/auth.ts @@ -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+/) diff --git a/packages/e2e/setup/global-auth.ts b/packages/e2e/setup/global-auth.ts index 739cebccbd5..c1655fc0fda 100644 --- a/packages/e2e/setup/global-auth.ts +++ b/packages/e2e/setup/global-auth.ts @@ -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+/)