Skip to content

Commit

Permalink
Add login and logout commands (#1022)
Browse files Browse the repository at this point in the history
* Add login and logout commands

* Refactor graphql client and requests, add unit tests

* Update shopify-config methods and tests

* Update renderErrors to use cli Command

* Rework env-pull to call login and remove silent param

* Rework link command to call login and remove --shop flag

* Update list command to call login and remove --shop flag

* Update env-list command to call login and remove --shop flag

* Oclif manifest

* Cleanup

* Rework log replacer

* Mute cli-kit auth logs

* Fix mock

* Merge main branch

* Fix tests

* Minor changes

* Update package-lock

* Skip writing config when no directory is passed to login

* Improve unit tests

* Changesets
  • Loading branch information
frandiox authored Jun 28, 2023
1 parent 00f3e59 commit 5530d98
Show file tree
Hide file tree
Showing 46 changed files with 1,237 additions and 1,115 deletions.
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/tough-seahorses-tan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/cli-hydrogen': minor
---

Add `login` and `logout` commands. Rework how other commands interact with auth.
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
---

Support creating new storefronts from the `link` command.
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 @@ -44,7 +44,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 @@ -72,7 +71,6 @@ async function runDev({
useCodegen = false,
codegenConfigPath,
disableVirtualRoutes,
shop,
envBranch,
debug = false,
sourcemap = true,
Expand All @@ -82,7 +80,6 @@ async function runDev({
useCodegen?: boolean;
codegenConfigPath?: string;
disableVirtualRoutes?: boolean;
shop?: string;
envBranch?: string;
debug?: false;
sourcemap?: boolean;
Expand Down Expand Up @@ -111,14 +108,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

0 comments on commit 5530d98

Please sign in to comment.