Skip to content

Commit

Permalink
Provide next steps for specific package managers
Browse files Browse the repository at this point in the history
  • Loading branch information
graygilmore committed Apr 8, 2024
1 parent 4d8d15d commit b11c1dc
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 15 deletions.
37 changes: 30 additions & 7 deletions packages/cli/src/commands/hydrogen/deploy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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(
Expand Down
29 changes: 21 additions & 8 deletions packages/cli/src/commands/hydrogen/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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);
}
}

Expand Down

0 comments on commit b11c1dc

Please sign in to comment.