-
Notifications
You must be signed in to change notification settings - Fork 262
Add shopify store open command
#7955
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
Merged
+193
−0
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import StoreOpen from './open.js' | ||
| import {openStore} from '../../services/store/open.js' | ||
| import {describe, expect, test, vi} from 'vitest' | ||
|
|
||
| vi.mock('../../services/store/open.js') | ||
|
|
||
| describe('store open command', () => { | ||
| test('passes the store flag through to the service', async () => { | ||
| await StoreOpen.run(['--store', 'shop.myshopify.com']) | ||
|
|
||
| expect(openStore).toHaveBeenCalledWith({store: 'shop.myshopify.com'}) | ||
| }) | ||
|
|
||
| test('defines the expected flags', () => { | ||
| expect(StoreOpen.flags.store).toBeDefined() | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import {openStore} from '../../services/store/open.js' | ||
| import StoreCommand from '../../utilities/store-command.js' | ||
| import {storeFlags} from '../../flags.js' | ||
| import {globalFlags} from '@shopify/cli-kit/node/cli' | ||
|
|
||
| export default class StoreOpen extends StoreCommand { | ||
| static hidden = true | ||
|
amcaplan marked this conversation as resolved.
|
||
|
|
||
| static summary = 'Open your Shopify store in the default web browser.' | ||
|
|
||
| static descriptionWithMarkdown = `Opens the storefront for a store you have access to in your default web browser.` | ||
|
|
||
| static description = this.descriptionWithoutMarkdown() | ||
|
|
||
| static examples = ['<%= config.bin %> <%= command.id %> --store shop.myshopify.com'] | ||
|
|
||
| static flags = { | ||
| ...globalFlags, | ||
| store: storeFlags.store, | ||
| } | ||
|
|
||
| public async run(): Promise<void> { | ||
| const {flags} = await this.parse(StoreOpen) | ||
|
|
||
| await openStore({store: flags.store}) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import {openStore} from './open.js' | ||
| import {getStoreInfo} from './info/index.js' | ||
| import {openURL} from '@shopify/cli-kit/node/system' | ||
| import {renderInfo} from '@shopify/cli-kit/node/ui' | ||
| import {beforeEach, describe, expect, test, vi} from 'vitest' | ||
|
|
||
| vi.mock('./info/index.js') | ||
| vi.mock('@shopify/cli-kit/node/system') | ||
| vi.mock('@shopify/cli-kit/node/ui') | ||
|
|
||
| describe('openStore', () => { | ||
| beforeEach(() => { | ||
| vi.mocked(openURL).mockResolvedValue(true) | ||
| }) | ||
|
|
||
| test('opens the canonical storefront URL for a regular store', async () => { | ||
| vi.mocked(getStoreInfo).mockResolvedValue({subdomain: 'shop.myshopify.com'}) | ||
|
|
||
| await openStore({store: 'shop.myshopify.com'}) | ||
|
|
||
| expect(getStoreInfo).toHaveBeenCalledWith({store: 'shop.myshopify.com'}) | ||
| expect(openURL).toHaveBeenCalledWith('https://shop.myshopify.com') | ||
| expect(renderInfo).toHaveBeenCalledWith( | ||
| expect.objectContaining({headline: expect.stringContaining('Opening the storefront')}), | ||
| ) | ||
| }) | ||
|
|
||
| test('prefers the preview-store access URL when present', async () => { | ||
| vi.mocked(getStoreInfo).mockResolvedValue({ | ||
| subdomain: 'preview.myshopify.com', | ||
| accessUrl: 'https://preview.myshopify.com/?token=abc', | ||
| }) | ||
|
|
||
| await openStore({store: 'preview.myshopify.com'}) | ||
|
|
||
| expect(openURL).toHaveBeenCalledWith('https://preview.myshopify.com/?token=abc') | ||
| }) | ||
|
|
||
| test('prints the URL manually when the browser does not open', async () => { | ||
| vi.mocked(getStoreInfo).mockResolvedValue({subdomain: 'shop.myshopify.com'}) | ||
| vi.mocked(openURL).mockResolvedValue(false) | ||
|
|
||
| await openStore({store: 'shop.myshopify.com'}) | ||
|
|
||
| expect(renderInfo).toHaveBeenCalledWith( | ||
| expect.objectContaining({headline: expect.stringContaining("didn't open automatically")}), | ||
| ) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import {getStoreInfo} from './info/index.js' | ||
| import {openURL as defaultOpenURL} from '@shopify/cli-kit/node/system' | ||
| import {renderInfo} from '@shopify/cli-kit/node/ui' | ||
| import {outputContent, outputToken} from '@shopify/cli-kit/node/output' | ||
| import type {StoreInfoResult} from './info/types.js' | ||
|
|
||
| interface OpenStoreOptions { | ||
| store: string | ||
| } | ||
|
|
||
| interface OpenStoreDependencies { | ||
| getStoreInfo: typeof getStoreInfo | ||
| openURL: typeof defaultOpenURL | ||
| } | ||
|
|
||
| const defaultDependencies: OpenStoreDependencies = { | ||
| getStoreInfo, | ||
| openURL: defaultOpenURL, | ||
| } | ||
|
|
||
| /** | ||
| * Opens a store's storefront in the default browser. | ||
| */ | ||
| export async function openStore( | ||
| options: OpenStoreOptions, | ||
| dependencies: Partial<OpenStoreDependencies> = {}, | ||
| ): Promise<void> { | ||
| const {getStoreInfo: getInfo, openURL} = {...defaultDependencies, ...dependencies} | ||
|
|
||
| const info = await getInfo({store: options.store}) | ||
| const url = storefrontUrl(info) | ||
|
|
||
| const opened = await openURL(url) | ||
| if (opened) { | ||
| renderInfo({headline: `Opening the storefront for ${options.store} in your browser.`}) | ||
| return | ||
| } | ||
|
|
||
| renderInfo({ | ||
| headline: `Browser didn't open automatically. Open the storefront manually:`, | ||
| body: [outputContent`${outputToken.link(url, url)}`.value], | ||
| }) | ||
| } | ||
|
|
||
| function storefrontUrl(info: StoreInfoResult): string { | ||
| // Preview stores surface a tokenized access URL; everyone else resolves to the canonical domain. | ||
| return info.accessUrl ?? `https://${info.subdomain}` | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense to include a password flag for those non preview stores that ask for password to get in?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would treat that as an optional later enhancement. It still works for preview stores, which is the main focus at present, and for other stores they'll have to enter the password manually.