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

Add login and logout commands #1022

Merged
merged 22 commits into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from 18 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/brave-suns-hammer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/cli-hydrogen': patch
---

Add more context on MiniOxygen local dev server startup
5 changes: 5 additions & 0 deletions .changeset/brown-parrots-tell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/hydrogen-react': patch
---

Add JSDoc examples to <Money /> and useMoney
5 changes: 5 additions & 0 deletions .changeset/thin-tigers-raise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/cli-hydrogen': patch
---

Fix `dev --codegen-unstable` flag, which was removed by mistake in the previous release.
5 changes: 5 additions & 0 deletions .changeset/twelve-suits-dance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/cli-hydrogen': minor
---

Link local storefront to a newly created storefront on Admin
32 changes: 16 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/cli/oclif.manifest.json

Large diffs are not rendered by default.

16 changes: 5 additions & 11 deletions packages/cli/src/commands/hydrogen/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ export default class Dev extends Command {
env: 'SHOPIFY_HYDROGEN_FLAG_DISABLE_VIRTUAL_ROUTES',
default: false,
}),
shop: commonFlags.shop,
debug: Flags.boolean({
description: 'Attaches a Node inspector',
env: 'SHOPIFY_HYDROGEN_FLAG_DEBUG',
Expand Down Expand Up @@ -77,7 +76,6 @@ async function runDev({
codegen = false,
codegenConfigPath,
disableVirtualRoutes,
shop,
envBranch,
debug = false,
sourcemap = true,
Expand All @@ -87,7 +85,6 @@ async function runDev({
codegen?: boolean;
codegenConfigPath?: string;
disableVirtualRoutes?: boolean;
shop?: string;
envBranch?: string;
debug?: false;
sourcemap?: boolean;
Expand Down Expand Up @@ -116,14 +113,11 @@ async function runDev({

const serverBundleExists = () => fileExists(buildPathWorkerFile);

const hasLinkedStorefront = !!(await getConfig(root))?.storefront?.id;
const environmentVariables = hasLinkedStorefront
? await combinedEnvironmentVariables({
root,
shop,
envBranch,
})
: undefined;
const {shop, storefront} = await getConfig(root);
const environmentVariables =
!!shop && !!storefront?.id
? await combinedEnvironmentVariables({root, shop, envBranch})
: undefined;

const [{watch}, {createFileWatchCache}] = await Promise.all([
import('@remix-run/dev/dist/compiler/watch.js'),
Expand Down
86 changes: 42 additions & 44 deletions packages/cli/src/commands/hydrogen/env/list.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {describe, it, expect, vi, beforeEach, afterEach} from 'vitest';
import type {AdminSession} from '@shopify/cli-kit/node/session';
import {mockAndCaptureOutput} from '@shopify/cli-kit/node/testing/output';
import {inTemporaryDirectory} from '@shopify/cli-kit/node/fs';
import {renderConfirmationPrompt} from '@shopify/cli-kit/node/ui';
Expand All @@ -8,14 +7,13 @@ import {
getStorefrontEnvironments,
type Environment,
} from '../../../lib/graphql/admin/list-environments.js';
import {getAdminSession} from '../../../lib/admin-session.js';
import {getConfig} from '../../../lib/shopify-config.js';
import {type AdminSession, login} from '../../../lib/auth.js';
import {
renderMissingLink,
renderMissingStorefront,
} from '../../../lib/render-errors.js';
import {linkStorefront} from '../link.js';
import {listEnvironments} from './list.js';
import {runEnvList} from './list.js';

const SHOP = 'my-shop';

Expand All @@ -29,22 +27,25 @@ vi.mock('@shopify/cli-kit/node/ui', async () => {
};
});
vi.mock('../link.js');
vi.mock('../../../lib/admin-session.js');
vi.mock('../../../lib/auth.js');
vi.mock('../../../lib/shopify-config.js');
vi.mock('../../../lib/render-errors.js');
vi.mock('../../../lib/graphql/admin/list-environments.js', () => {
return {getStorefrontEnvironments: vi.fn()};
});
vi.mock('../../../lib/shop.js', () => ({
getHydrogenShop: () => SHOP,
}));
vi.mock('../../../lib/graphql/admin/list-environments.js');
vi.mock('../../../lib/shell.js', () => ({getCliCommand: () => 'h2'}));

describe('listEnvironments', () => {
const ADMIN_SESSION: AdminSession = {
token: 'abc123',
storeFqdn: SHOP,
};

const SHOPIFY_CONFIG = {
storefront: {
id: 'gid://shopify/HydrogenStorefront/1',
title: 'Existing Link',
},
};

const PRODUCTION_ENVIRONMENT: Environment = {
id: 'gid://shopify/HydrogenStorefrontEnvironment/1',
branch: 'main',
Expand Down Expand Up @@ -73,23 +74,19 @@ describe('listEnvironments', () => {
};

beforeEach(async () => {
vi.mocked(getAdminSession).mockResolvedValue(ADMIN_SESSION);
vi.mocked(getConfig).mockResolvedValue({
storefront: {
id: 'gid://shopify/HydrogenStorefront/1',
title: 'Existing Link',
},
vi.mocked(login).mockResolvedValue({
session: ADMIN_SESSION,
config: SHOPIFY_CONFIG,
});

vi.mocked(getStorefrontEnvironments).mockResolvedValue({
storefront: {
id: 'gid://shopify/HydrogenStorefront/1',
productionUrl: 'https://example.com',
environments: [
PRODUCTION_ENVIRONMENT,
CUSTOM_ENVIRONMENT,
PREVIEW_ENVIRONMENT,
],
},
id: 'gid://shopify/HydrogenStorefront/1',
productionUrl: 'https://example.com',
environments: [
PRODUCTION_ENVIRONMENT,
CUSTOM_ENVIRONMENT,
PREVIEW_ENVIRONMENT,
],
});
});

Expand All @@ -98,13 +95,13 @@ describe('listEnvironments', () => {
mockAndCaptureOutput().clear();
});

it('makes a GraphQL call to fetch environment variables', async () => {
it('fetchs environment variables', async () => {
await inTemporaryDirectory(async (tmpDir) => {
await listEnvironments({path: tmpDir});
await runEnvList({path: tmpDir});

expect(getStorefrontEnvironments).toHaveBeenCalledWith(
ADMIN_SESSION,
'gid://shopify/HydrogenStorefront/1',
SHOPIFY_CONFIG.storefront.id,
);
});
});
Expand All @@ -113,10 +110,10 @@ describe('listEnvironments', () => {
await inTemporaryDirectory(async (tmpDir) => {
const output = mockAndCaptureOutput();

await listEnvironments({path: tmpDir});
await runEnvList({path: tmpDir});

expect(output.info()).toMatch(
/Showing 3 environments for the Hydrogen storefront Existing Link/,
/Showing 3 environments for the Hydrogen storefront Existing Link/i,
);

expect(output.info()).toMatch(/Production \(Branch: main\)/);
Expand All @@ -129,14 +126,15 @@ describe('listEnvironments', () => {

describe('when there is no linked storefront', () => {
beforeEach(() => {
vi.mocked(getConfig).mockResolvedValue({
storefront: undefined,
vi.mocked(login).mockResolvedValue({
session: ADMIN_SESSION,
config: {},
});
});

it('calls renderMissingLink', async () => {
await inTemporaryDirectory(async (tmpDir) => {
await listEnvironments({path: tmpDir});
await runEnvList({path: tmpDir});

expect(renderMissingLink).toHaveBeenCalledOnce();
});
Expand All @@ -146,30 +144,30 @@ describe('listEnvironments', () => {
vi.mocked(renderConfirmationPrompt).mockResolvedValue(true);

await inTemporaryDirectory(async (tmpDir) => {
await listEnvironments({path: tmpDir});
await runEnvList({path: tmpDir});

expect(renderConfirmationPrompt).toHaveBeenCalledWith({
message: expect.stringMatching(/Run .*npx shopify hydrogen link.*\?/),
message: expect.arrayContaining([{command: 'h2 link'}]),
});

expect(linkStorefront).toHaveBeenCalledWith({
path: tmpDir,
silent: true,
});
expect(linkStorefront).toHaveBeenCalledWith(
tmpDir,
ADMIN_SESSION,
{},
expect.anything(),
);
});
});
});

describe('when there is no matching storefront in the shop', () => {
beforeEach(() => {
vi.mocked(getStorefrontEnvironments).mockResolvedValue({
storefront: null,
});
vi.mocked(getStorefrontEnvironments).mockResolvedValue(null);
});

it('calls renderMissingStorefront', async () => {
await inTemporaryDirectory(async (tmpDir) => {
await listEnvironments({path: tmpDir});
await runEnvList({path: tmpDir});

expect(renderMissingStorefront).toHaveBeenCalledOnce();
});
Expand Down
Loading