Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(atomic): use the initializer version from CLI #1292

Merged
merged 2 commits into from
Apr 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ exports[`atomic:component calls \`npm init @coveo/atomic-component\` properly 1`
"npm",
[
"init",
"@coveo/atomic-component",
"@coveo/atomic-component@1.2.3",
"foo",
],
]
Expand All @@ -16,7 +16,7 @@ exports[`atomic:component calls \`npm init @coveo/atomic-component\` when the us
"npm",
[
"init",
"@coveo/atomic-component",
"@coveo/atomic-component@1.2.3",
"foo",
],
]
Expand All @@ -27,7 +27,7 @@ exports[`atomic:component calls \`npm init @coveo/atomic-result-component\` prop
"npm",
[
"init",
"@coveo/atomic-result-component",
"@coveo/atomic-result-component@1.2.3",
"foo",
],
]
Expand All @@ -38,7 +38,7 @@ exports[`atomic:component calls \`npm init @coveo/atomic-result-component\` when
"npm",
[
"init",
"@coveo/atomic-result-component",
"@coveo/atomic-result-component@1.2.3",
"foo",
],
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ exports[`atomic:init calls \`createAtomicApp\` properly 1`] = `
"organization": "default-org",
"region": "us",
},
"initializerVersion": "latest",
"projectName": "foo",
},
]
Expand All @@ -22,7 +21,6 @@ exports[`atomic:init calls \`createAtomicApp\` properly 2`] = `
"organization": "default-org",
"region": "us",
},
"initializerVersion": "latest",
"projectName": "foo",
},
]
Expand All @@ -36,7 +34,6 @@ exports[`atomic:init calls \`createAtomicApp\` when the user select application
"organization": "default-org",
"region": "us",
},
"initializerVersion": "latest",
"projectName": "foo",
},
]
Expand Down
5 changes: 4 additions & 1 deletion packages/cli/core/src/commands/atomic/component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@ jest.mock('../../lib/utils/os');
import {appendCmdIfWindows} from '../../lib/utils/os';
jest.mock('../../lib/utils/process');
import {spawnProcess} from '../../lib/utils/process';
jest.mock('../../lib/utils/os');
jest.mock('../../lib/utils/misc');
import {getPackageVersion} from '../../lib/utils/misc';

describe('atomic:component', () => {
const mockedSpawnProcess = jest.mocked(spawnProcess);
const mockedInquirer = jest.mocked(inquirer);
const mockAppendCmdIfWindows = jest.mocked(appendCmdIfWindows);
const mockedGetPackageVersion = jest.mocked(getPackageVersion);

beforeEach(() => {
jest.resetAllMocks();
mockAppendCmdIfWindows.mockImplementation(
(input: TemplateStringsArray) => `${input}`
);
mockedGetPackageVersion.mockReturnValue('1.2.3');
});

test
Expand Down
17 changes: 13 additions & 4 deletions packages/cli/core/src/commands/atomic/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {appendCmdIfWindows} from '../../lib/utils/os';
import {spawnProcess} from '../../lib/utils/process';
import {startSpinner} from '@coveo/cli-commons/utils/ux';
import {Trackable} from '@coveo/cli-commons/preconditions/trackable';
import {getPackageVersion} from '../../lib/utils/misc';

export default class AtomicInit extends CLICommand {
public static description =
Expand Down Expand Up @@ -41,8 +42,16 @@ export default class AtomicInit extends CLICommand {
const {args, flags} = await this.parse(AtomicInit);
const type = flags.type || (await this.askType());

const initializer = this.getInitializerPackage(type);
return {initializer, name: args.name};
const initializerPackage = this.getInitializerPackage(type);

// TODO CDX-1340: Refactor the replace into a well named utils.
return {
initializer: `${initializerPackage.replace(
'/create-',
'/'
)}@${getPackageVersion(initializerPackage)}`,
name: args.name,
};
}

private async askType(): Promise<string> {
Expand All @@ -60,9 +69,9 @@ export default class AtomicInit extends CLICommand {
private getInitializerPackage(type: string): string {
switch (type) {
case 'page':
return '@coveo/atomic-component';
return '@coveo/create-atomic-component';
case 'result':
return '@coveo/atomic-result-component';
return '@coveo/create-atomic-result-component';
default:
throw new UnknownError();
}
Expand Down
1 change: 0 additions & 1 deletion packages/cli/core/src/commands/atomic/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ export default class AtomicInit extends CLICommand {
private createAtomicApp(projectName: string) {
const cfg = this.configuration.get();
return createAtomicApp({
initializerVersion: 'latest',
projectName,
cfg,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,31 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`createAtomicProject createAtomicApp() without options.pageId calls \`npx @coveo/create-atomic\` properly 1`] = `
exports[`createAtomicProject createAtomicApp() with options.initializerVersion set calls \`npx @coveo/create-atomic\` properly 1`] = `
[
"npx",
[
"@coveo/create-atomic@test",
"--project",
"potato",
"--org-id",
"bobbyOrg",
"--api-key",
"someToken",
"--platform-url",
"https://platformdev-au.cloud.coveo.com",
"--user",
"[email protected]",
"--page-id",
"pageId",
],
]
`;

exports[`createAtomicProject createAtomicApp() with options.pageId calls \`npx @coveo/create-atomic\` properly 1`] = `
[
"npx",
[
"@coveo/create-atomic@latest",
"@coveo/create-atomic@1.2.3",
"--project",
"potato",
"--org-id",
Expand All @@ -15,15 +36,17 @@ exports[`createAtomicProject createAtomicApp() without options.pageId calls \`np
"https://platformdev-au.cloud.coveo.com",
"--user",
"[email protected]",
"--page-id",
"pageId",
],
]
`;

exports[`createAtomicProject createAtomicApp() without options.pageId calls \`npx @coveo/create-atomic\` properly 2`] = `
exports[`createAtomicProject createAtomicApp() without options.initializerVersion calls \`npx @coveo/create-atomic\` properly and calls getPackageVersion 1`] = `
[
"npx",
[
"@coveo/create-atomic@latest",
"@coveo/create-atomic@1.2.3",
"--project",
"potato",
"--org-id",
Expand All @@ -40,6 +63,25 @@ exports[`createAtomicProject createAtomicApp() without options.pageId calls \`np
]
`;

exports[`createAtomicProject createAtomicApp() without options.pageId calls \`npx @coveo/create-atomic\` properly 1`] = `
[
"npx",
[
"@coveo/[email protected]",
"--project",
"potato",
"--org-id",
"bobbyOrg",
"--api-key",
"someToken",
"--platform-url",
"https://platformdev-au.cloud.coveo.com",
"--user",
"[email protected]",
],
]
`;

exports[`createAtomicProject createAtomicLib() calls \`npx @coveo/create-atomic-component-project\` properly 1`] = `
[
"npm",
Expand Down
33 changes: 31 additions & 2 deletions packages/cli/core/src/lib/atomic/createAtomicProject.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@ jest.mock('../utils/process');
import {spawnProcess} from '../utils/process';
jest.mock('../utils/os');
import {appendCmdIfWindows} from '../utils/os';
jest.mock('../utils/misc');
import {getPackageVersion} from '../utils/misc';

import {createAtomicApp, createAtomicLib} from './createAtomicProject';

describe('createAtomicProject', () => {
const mockedSpawnProcess = jest.mocked(spawnProcess);
const mockedAppendCmdIfWindows = jest.mocked(appendCmdIfWindows);
const mockedGetPackageVersion = jest.mocked(getPackageVersion);
beforeEach(() => {
jest.resetAllMocks();
mockedAppendCmdIfWindows.mockImplementation((input) => `${input}`);
mockedGetPackageVersion.mockReturnValue('1.2.3');
});

describe('createAtomicApp()', () => {
Expand All @@ -41,7 +46,6 @@ describe('createAtomicProject', () => {
region: Region.AU,
version: '1.0.0',
},
initializerVersion: 'latest',
projectName: 'potato',
};

Expand All @@ -57,13 +61,38 @@ describe('createAtomicProject', () => {
});
});

describe('without options.pageId', () => {
describe('with options.pageId', () => {
it('calls `npx @coveo/create-atomic` properly', async () => {
await createAtomicApp({...callOptions, pageId: 'pageId'});
expect(mockedSpawnProcess).toBeCalledTimes(1);
expect(mockedSpawnProcess.mock.lastCall).toMatchSnapshot();
});
});

describe('with options.initializerVersion set', () => {
it('calls `npx @coveo/create-atomic` properly', async () => {
await createAtomicApp({
...callOptions,
initializerVersion: 'test',
pageId: 'pageId',
});
expect(mockedSpawnProcess).toBeCalledTimes(1);
expect(mockedSpawnProcess.mock.lastCall).toMatchSnapshot();
});
});

describe('without options.initializerVersion', () => {
it('calls `npx @coveo/create-atomic` properly and calls getPackageVersion', async () => {
await createAtomicApp({
...callOptions,
initializerVersion: undefined,
pageId: 'pageId',
});
expect(mockedGetPackageVersion).toBeCalledTimes(1);
expect(mockedSpawnProcess).toBeCalledTimes(1);
expect(mockedSpawnProcess.mock.lastCall).toMatchSnapshot();
});
});
});

describe('createAtomicLib()', () => {
Expand Down
8 changes: 6 additions & 2 deletions packages/cli/core/src/lib/atomic/createAtomicProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ import {
IsNpxInstalled,
IsNodeVersionInRange,
} from '../decorators/preconditions';
import {getPackageVersion} from '../utils/misc';

interface CreateAppOptions {
initializerVersion: string;
initializerVersion?: string;
pageId?: string;
projectName: string;
cfg: Configuration;
Expand Down Expand Up @@ -52,7 +53,10 @@ export async function createAtomicApp(options: CreateAppOptions) {

const username = await authenticatedClient.getUsername();
const cliArgs: string[] = [
`${atomicAppInitializerPackage}@${options.initializerVersion}`,
`${atomicAppInitializerPackage}@${
options.initializerVersion ??
getPackageVersion(atomicAppInitializerPackage)
}`,
'--project',
options.projectName,
'--org-id',
Expand Down