From 44e53a6fc926a0507d84b6da3849874421f03845 Mon Sep 17 00:00:00 2001 From: benwolfram Date: Thu, 14 Nov 2024 17:18:34 -0600 Subject: [PATCH] send hydrogen version metadata in deploy --- .../cli/src/commands/hydrogen/deploy.test.ts | 39 ++++++++++++++++++- packages/cli/src/commands/hydrogen/deploy.ts | 26 +++++++++++-- 2 files changed, 60 insertions(+), 5 deletions(-) diff --git a/packages/cli/src/commands/hydrogen/deploy.test.ts b/packages/cli/src/commands/hydrogen/deploy.test.ts index ad5d091453..0923f4e8f1 100644 --- a/packages/cli/src/commands/hydrogen/deploy.test.ts +++ b/packages/cli/src/commands/hydrogen/deploy.test.ts @@ -16,11 +16,13 @@ import { getLatestGitCommit, GitDirectoryNotCleanError, } from '@shopify/cli-kit/node/git'; +import {createRequire} from 'node:module'; -import {deploymentLogger, runDeploy} from './deploy.js'; +import {deploymentLogger, getHydrogenVersion, runDeploy} from './deploy.js'; import {getOxygenDeploymentData} from '../../lib/get-oxygen-deployment-data.js'; import {execAsync} from '../../lib/process.js'; import {createEnvironmentCliChoiceLabel} from '../../lib/common.js'; +import {getSkeletonSourceDir} from '../../lib/build.js'; import { CompletedDeployment, createDeploy, @@ -28,6 +30,7 @@ import { } from '@shopify/oxygen-cli/deploy'; import {ciPlatform} from '@shopify/cli-kit/node/context/local'; import {runBuild} from './build.js'; +import {PackageJson} from 'type-fest'; vi.mock('@shopify/oxygen-cli/deploy'); vi.mock('@shopify/cli-kit/node/dot-env'); @@ -61,7 +64,21 @@ vi.mock('@shopify/cli-kit/node/git', async () => { }; }); -describe('deploy', () => { +async function createHydrogenDependencyPackageJson(version?: string) { + const require = createRequire(import.meta.url); + const packageJson: PackageJson = require(require.resolve( + '@shopify/hydrogen/package.json', + {paths: [getSkeletonSourceDir()]}, + )); + + packageJson.version = version; + + return packageJson; +} + +describe('deploy', async () => { + await createHydrogenDependencyPackageJson('2000.1.1'); + const ADMIN_SESSION: AdminSession = { token: 'abc123', storeFqdn: 'my-shop.myshopify.com', @@ -119,6 +136,7 @@ describe('deploy', () => { url: deployParams.metadataUrl, user: deployParams.metadataUser, version: deployParams.metadataVersion, + hydrogenVersion: '2000.1.1', }, skipVerification: true, rootPath: deployParams.path, @@ -163,6 +181,7 @@ describe('deploy', () => { oxygenDeploymentToken: 'some-encoded-token', environments: [], }); + vi.mocked(parseToken).mockReturnValue(mockToken); }); @@ -943,4 +962,20 @@ describe('deploy', () => { }); }); }); + + describe('getHydrogenVersion', () => { + it('returns the version', async () => { + const version = await getHydrogenVersion({appPath: deployParams.path}); + expect(version).toBe('2000.1.1'); + }); + + describe('when there are no version is available', () => { + it('returns undefined', async () => { + await createHydrogenDependencyPackageJson(undefined); + + const version = await getHydrogenVersion({appPath: deployParams.path}); + expect(version).toBeUndefined(); + }); + }); + }); }); diff --git a/packages/cli/src/commands/hydrogen/deploy.ts b/packages/cli/src/commands/hydrogen/deploy.ts index f48902471f..53fe680787 100644 --- a/packages/cli/src/commands/hydrogen/deploy.ts +++ b/packages/cli/src/commands/hydrogen/deploy.ts @@ -8,13 +8,13 @@ import { } from '@shopify/cli-kit/node/output'; import {readAndParseDotEnv} from '@shopify/cli-kit/node/dot-env'; import {AbortError} from '@shopify/cli-kit/node/error'; -import {writeFile} from '@shopify/cli-kit/node/fs'; +import {readFile, writeFile} from '@shopify/cli-kit/node/fs'; import { ensureIsClean, getLatestGitCommit, GitDirectoryNotCleanError, } from '@shopify/cli-kit/node/git'; -import {relativePath, resolvePath} from '@shopify/cli-kit/node/path'; +import {joinPath, relativePath, resolvePath} from '@shopify/cli-kit/node/path'; import { renderConfirmationPrompt, renderSelectPrompt, @@ -32,6 +32,7 @@ import { DeploymentVerificationDetailsResponse, parseToken, } from '@shopify/oxygen-cli/deploy'; +import {createRequire} from 'node:module'; import { createEnvironmentCliChoiceLabel, @@ -51,7 +52,7 @@ import {runClassicCompilerBuild} from '../../lib/classic-compiler/build.js'; import {runBuild} from './build.js'; import {getViteConfig} from '../../lib/vite-config.js'; import {prepareDiffDirectory} from '../../lib/template-diff.js'; -import {isClassicProject} from '../../lib/remix-config.js'; +import {getProjectPaths, isClassicProject} from '../../lib/remix-config.js'; import {packageManagers} from '../../lib/package-managers.js'; import {setupResourceCleanup} from '../../lib/resource-cleanup.js'; @@ -466,6 +467,8 @@ export async function runDeploy( } } + const metadataHydrogenVersion = await getHydrogenVersion({appPath: root}); + const config: DeploymentConfig = { assetsDir, bugsnag: true, @@ -484,6 +487,9 @@ export async function runDeploy( ...(metadataUrl ? {url: metadataUrl} : {}), ...(metadataUser ? {user: metadataUser} : {}), ...(metadataVersion ? {version: metadataVersion} : {}), + ...(metadataHydrogenVersion + ? {hydrogenVersion: metadataHydrogenVersion} + : {}), }, skipVerification: noVerify, rootPath: root, @@ -673,3 +679,17 @@ Continue?`.value, return deployPromise; } + +/** + * Gets the current @shopify/hydrogen version from the package's package.json + */ +export async function getHydrogenVersion({appPath}: {appPath: string}) { + const {root} = getProjectPaths(appPath); + + const require = createRequire(import.meta.url); + const {version} = require(require.resolve('@shopify/hydrogen/package.json', { + paths: [root], + })); + + return version; +}