From b11c1dc0bf57e18a28f310ebfd706bd11fa1f180 Mon Sep 17 00:00:00 2001 From: Gray Gilmore Date: Mon, 8 Apr 2024 14:29:23 -0700 Subject: [PATCH] Provide next steps for specific package managers --- .../cli/src/commands/hydrogen/deploy.test.ts | 37 +++++++++++++++---- packages/cli/src/commands/hydrogen/deploy.ts | 29 +++++++++++---- 2 files changed, 51 insertions(+), 15 deletions(-) diff --git a/packages/cli/src/commands/hydrogen/deploy.test.ts b/packages/cli/src/commands/hydrogen/deploy.test.ts index d90d79bb70..4093a12466 100644 --- a/packages/cli/src/commands/hydrogen/deploy.test.ts +++ b/packages/cli/src/commands/hydrogen/deploy.test.ts @@ -42,13 +42,6 @@ vi.mock('../../lib/graphql/admin/link-storefront.js'); vi.mock('../../lib/graphql/admin/create-storefront.js'); vi.mock('../../lib/graphql/admin/fetch-job.js'); vi.mock('../../lib/shell.js', () => ({getCliCommand: () => 'h2'})); -vi.mock('@shopify/cli-kit/node/output', async () => { - return { - outputContent: () => ({value: ''}), - outputInfo: () => {}, - outputWarn: () => {}, - }; -}); vi.mock('@shopify/cli-kit/node/ui', async () => { return { renderFatalError: vi.fn(), @@ -337,6 +330,36 @@ describe('deploy', () => { expect(vi.mocked(createDeploy)).not.toHaveBeenCalled; }); + describe('and there are untracked lockfiles', () => { + it('includes additional options for next steps', async () => { + vi.mocked(execAsync).mockReturnValue( + Promise.resolve({ + stdout: ' M package-lock.json\n', + stderr: '', + }) as any, + ); + + vi.mocked(ensureIsClean).mockRejectedValue( + new GitDirectoryNotCleanError('Uncommitted changes'), + ); + + await expect(runDeploy(deployParams)).rejects.toThrow( + expect.objectContaining({ + message: 'Uncommitted changes detected:\n\n M package-lock.json', + nextSteps: expect.arrayContaining([ + [ + 'If you are using npm, try running', + {command: 'npm ci'}, + 'to avoid changes to package-lock.json.', + ], + ]), + }), + ); + + expect(vi.mocked(createDeploy)).not.toHaveBeenCalled; + }); + }); + describe('and the force flag is used', () => { it('proceeds with a warning and modifies the description', async () => { vi.mocked(ensureIsClean).mockRejectedValue( diff --git a/packages/cli/src/commands/hydrogen/deploy.ts b/packages/cli/src/commands/hydrogen/deploy.ts index 516d794fa5..7a19ae1631 100644 --- a/packages/cli/src/commands/hydrogen/deploy.ts +++ b/packages/cli/src/commands/hydrogen/deploy.ts @@ -48,6 +48,7 @@ import {runViteBuild} from './build-vite.js'; import {getViteConfig} from '../../lib/vite-config.js'; import {prepareDiffDirectory} from '../../lib/template-diff.js'; import {hasRemixConfigFile} from '../../lib/remix-config.js'; +import {packageManagers} from '../../lib/package-managers.js'; const DEPLOY_OUTPUT_FILE_HANDLE = 'h2_deploy_log.json'; @@ -255,21 +256,33 @@ export async function runDeploy( let errorMessage = 'Uncommitted changes detected'; let changedFiles = undefined; + const nextSteps = [ + [ + 'Commit your changes before deploying or use the', + {command: '--force'}, + 'flag to deploy with uncommitted changes.', + ], + ]; + try { changedFiles = (await execAsync('git status -s', {cwd: root})).stdout; - } catch (error) {} + } catch {} if (changedFiles) { errorMessage += `:\n\n${changedFiles.trimEnd()}`; + + packageManagers.forEach(({name, lockfile, installCommand}) => { + if (changedFiles.includes(lockfile)) { + nextSteps.push([ + `If you are using ${name}, try running`, + {command: installCommand}, + `to avoid changes to ${lockfile}.`, + ]); + } + }); } - throw new AbortError(errorMessage, null, [ - [ - 'Commit your changes before deploying or use the', - {command: '--force'}, - 'flag to deploy with uncommitted changes.', - ], - ]); + throw new AbortError(errorMessage, null, nextSteps); } }