Skip to content

Commit

Permalink
Fix bug in create CLI using --install-deps flag (#644)
Browse files Browse the repository at this point in the history
* Fix bug in create CLI using `--install-deps` flag

* Use flags to camel case util

* Add tests for init

---------

Co-authored-by: Matt Seccafien <462077+cartogram@users.noreply.github.com>
  • Loading branch information
Matt Seccafien and cartogram committed Mar 7, 2023
1 parent 94dfd8a commit 3344b79
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/silent-pianos-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/cli-hydrogen': patch
---

Fix bug in CLI not recognising the --install-deps flag when creating projects
93 changes: 93 additions & 0 deletions packages/cli/src/commands/hydrogen/init.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import {describe, it, expect, vi, beforeEach} from 'vitest';
import {temporaryDirectoryTask} from 'tempy';
import {runInit} from './init.js';
import {ui} from '@shopify/cli-kit';
import {installNodeModules} from '@shopify/cli-kit/node/node-package-manager';

describe('init', () => {
beforeEach(() => {
vi.resetAllMocks();
vi.mock('@shopify/cli-kit');
vi.mock('../../utils/transpile-ts.js');
vi.mock('../../utils/template-downloader.js', async () => ({
getLatestTemplates: () => Promise.resolve({}),
}));
vi.mock('@shopify/cli-kit/node/node-package-manager');
vi.mocked(ui.prompt).mockImplementation(() =>
Promise.resolve({installDeps: 'false'}),
);
});

const defaultOptions = (stubs: Record<any, unknown>) => ({
template: 'hello-world',
language: 'js',
path: 'path/to/project',
...stubs,
});

describe('installDeps', () => {
it('prompts the user to install dependencies when installDeps is not passed', async () => {
await temporaryDirectoryTask(async (tmpDir) => {
// Given
const options = defaultOptions({path: tmpDir});

vi.mocked(ui.prompt).mockImplementation(() =>
Promise.resolve({installDeps: 'false'}),
);

// When
await runInit(options);

// Then
expect(ui.prompt).toHaveBeenCalledWith(
expect.arrayContaining([
expect.objectContaining({
name: 'installDeps',
}),
]),
);
expect(installNodeModules).not.toHaveBeenCalled();
});
});

it('does not prompt the user to install dependencies when installDeps is true', async () => {
await temporaryDirectoryTask(async (tmpDir) => {
// Given
const options = defaultOptions({installDeps: true, path: tmpDir});

// When
await runInit(options);

// Then
expect(ui.prompt).not.toHaveBeenCalledWith(
expect.arrayContaining([
expect.objectContaining({
name: 'installDeps',
}),
]),
);
expect(installNodeModules).toHaveBeenCalled();
});
});

it('does not show a prompt to install dependencies when installDeps is false', async () => {
await temporaryDirectoryTask(async (tmpDir) => {
// Given
const options = defaultOptions({installDeps: false, path: tmpDir});

// When
await runInit(options);

// Then
expect(ui.prompt).not.toHaveBeenCalledWith(
expect.arrayContaining([
expect.objectContaining({
name: 'installDeps',
}),
]),
);
expect(installNodeModules).not.toHaveBeenCalled();
});
});
});
});
8 changes: 6 additions & 2 deletions packages/cli/src/commands/hydrogen/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import {
import {renderFatalError} from '@shopify/cli-kit/node/ui';
import Flags from '@oclif/core/lib/flags.js';
import {output, path} from '@shopify/cli-kit';
import {commonFlags, parseProcessFlags} from '../../utils/flags.js';
import {
commonFlags,
parseProcessFlags,
flagsToCamelObject,
} from '../../utils/flags.js';
import {transpileProject} from '../../utils/transpile-ts.js';
import {getLatestTemplates} from '../../utils/template-downloader.js';
import {checkHydrogenVersion} from '../../utils/check-version.js';
Expand Down Expand Up @@ -46,7 +50,7 @@ export default class Init extends Command {
// @ts-ignore
const {flags} = await this.parse(Init);

await runInit({...flags});
await runInit(flagsToCamelObject(flags));
}
}

Expand Down

0 comments on commit 3344b79

Please sign in to comment.