Skip to content

Commit

Permalink
refactor(cli): change default core ref to latest release (#712)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewvolk authored Apr 2, 2024
1 parent 5af4856 commit 8ad9d15
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 33 deletions.
5 changes: 5 additions & 0 deletions .changeset/wicked-beers-bathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@bigcommerce/create-catalyst": minor
---

Change the default GitHub Ref that the CLI uses to clone `bigcommerce/catalyst-core` from `main` to latest published release (e.g., `@bigcommerce/[email protected]`)
1 change: 1 addition & 0 deletions packages/create-catalyst/jest.config.cjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
transformIgnorePatterns: [],
transform: {
'^.+\\.(t|j)s?$': '@swc/jest',
},
Expand Down
2 changes: 1 addition & 1 deletion packages/create-catalyst/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"giget": "^1.2.3",
"lodash.kebabcase": "^4.1.1",
"lodash.merge": "^4.6.2",
"msw": "^2.1.7",
"nypm": "^0.3.6",
"ora": "^8.0.1",
"semver": "^7.6.0",
Expand All @@ -46,6 +45,7 @@
"@types/semver": "^7.5.7",
"eslint": "^8.55.0",
"jest": "^29.7.0",
"msw": "^2.1.7",
"prettier": "^3.1.1",
"tsup": "^6.7.0",
"typescript": "^5.3.3"
Expand Down
10 changes: 9 additions & 1 deletion packages/create-catalyst/src/commands/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,21 @@ import { writeEnv } from '../utils/write-env';
const exec = promisify(execCallback);

export const create = async (options: CreateCommandOptions) => {
const { packageManager, ghRef } = options;
const { packageManager } = options;

const URLSchema = z.string().url();
const sampleDataApiUrl = parse(options.sampleDataApiUrl, URLSchema);
const bigcommerceApiUrl = parse(`https://api.${options.bigcommerceHostname}`, URLSchema);
const bigcommerceAuthUrl = parse(`https://login.${options.bigcommerceHostname}`, URLSchema);

let ghRef: string;

if (options.ghRef instanceof Function) {
ghRef = await options.ghRef();
} else {
ghRef = options.ghRef;
}

let projectName;
let projectDir;
let storeHash = options.storeHash;
Expand Down
8 changes: 7 additions & 1 deletion packages/create-catalyst/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import PACKAGE_INFO from '../package.json';

import { create } from './commands/create';
import { init } from './commands/init';
import { getLatestCoreTag } from './utils/get-latest-core-tag';
import { getPackageManager, packageManagerChoices } from './utils/pm';

if (!satisfies(process.version, PACKAGE_INFO.engines.node)) {
Expand Down Expand Up @@ -38,11 +39,16 @@ const createCommand = program
.description('Command to scaffold and connect a Catalyst storefront to your BigCommerce store')
.option('--project-name <name>', 'Name of your Catalyst project')
.option('--project-dir <dir>', 'Directory in which to create your project', process.cwd())
.option('--gh-ref <url>', 'Clone a specific ref from the bigcommerce/catalyst repository', 'main')
.option('--store-hash <hash>', 'BigCommerce store hash')
.option('--access-token <token>', 'BigCommerce access token')
.option('--channel-id <id>', 'BigCommerce channel ID')
.option('--customer-impersonation-token <token>', 'BigCommerce customer impersonation token')
.addOption(
new Option(
'--gh-ref <ref>',
'Clone a specific ref from the bigcommerce/catalyst repository',
).default(getLatestCoreTag),
)
.addOption(
new Option('--bigcommerce-hostname <hostname>', 'BigCommerce hostname')
.default('bigcommerce.com')
Expand Down
42 changes: 42 additions & 0 deletions packages/create-catalyst/src/utils/get-latest-core-tag.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { http } from 'msw';
import { setupServer } from 'msw/node';

import { getLatestCoreTag } from './get-latest-core-tag';

const handlers = [
http.get(
'https://raw.githubusercontent.com/bigcommerce/catalyst/main/apps/core/package.json',
() => {
return new Response('{ "name": "@bigcommerce/catalyst-core", "version": "0.1.0" }');
},
),
];

const server = setupServer(...handlers);

beforeAll(() => server.listen());
afterEach(() => server.resetHandlers());
afterAll(() => server.close());

describe('getLatestCoreTag', () => {
it('should return the latest core tag', async () => {
const latestTag = await getLatestCoreTag();

expect(latestTag).toBe('@bigcommerce/[email protected]');
});

it('should throw an error if the latest core tag is not found', async () => {
server.use(
http.get(
'https://raw.githubusercontent.com/bigcommerce/catalyst/main/apps/core/package.json',
() => {
return new Response('{ "name": "@bigcommerce/catalyst-core" }');
},
),
);

await expect(getLatestCoreTag()).rejects.toThrow(
'Unable to determine the latest valid Catalyst release',
);
});
});
16 changes: 16 additions & 0 deletions packages/create-catalyst/src/utils/get-latest-core-tag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as z from 'zod';

export const getLatestCoreTag = async () => {
const rawPackageJsonUrl =
'https://raw.githubusercontent.com/bigcommerce/catalyst/main/apps/core/package.json';

const response = await fetch(rawPackageJsonUrl);

try {
const { version } = z.object({ version: z.string() }).parse(await response.json());

return `@bigcommerce/catalyst-core@${version}`;
} catch (err) {
throw new Error('Unable to determine the latest valid Catalyst release');
}
};
Loading

0 comments on commit 8ad9d15

Please sign in to comment.