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

FEAT: added reset branch to ref command #1716

Merged
merged 9 commits into from
Dec 10, 2024
8 changes: 5 additions & 3 deletions packages/create-catalyst/src/commands/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const create = new Command('create')
.option('--channel-id <id>', 'BigCommerce channel ID')
.option('--storefront-token <token>', 'BigCommerce storefront token')
.option('--gh-ref <ref>', 'Clone a specific ref from the source repository')
.option('--reset-main', 'Reset the main branch to the gh-ref')
.option('--repository <repository>', 'GitHub repository to clone from', 'bigcommerce/catalyst')
.option('--env <vars...>', 'Arbitrary environment variables to set in .env.local')
.addOption(
Expand Down Expand Up @@ -67,6 +68,7 @@ export const create = new Command('create')
const sampleDataApiUrl = parse(options.sampleDataApiUrl, URLSchema);
const bigcommerceApiUrl = parse(`https://api.${options.bigcommerceHostname}`, URLSchema);
const bigcommerceAuthUrl = parse(`https://login.${options.bigcommerceHostname}`, URLSchema);
const resetMain = options.resetMain;

let projectName;
let projectDir;
Expand Down Expand Up @@ -123,7 +125,7 @@ export const create = new Command('create')
if (storeHash && channelId && storefrontToken) {
console.log(`\nCreating '${projectName}' at '${projectDir}'\n`);

cloneCatalyst({ repository, projectName, projectDir, ghRef });
cloneCatalyst({ repository, projectName, projectDir, ghRef, resetMain });

writeEnv(projectDir, {
channelId: channelId.toString(),
Expand Down Expand Up @@ -153,7 +155,7 @@ export const create = new Command('create')
if (!storeHash || !accessToken) {
console.log(`\nCreating '${projectName}' at '${projectDir}'\n`);

cloneCatalyst({ repository, projectName, projectDir, ghRef });
cloneCatalyst({ repository, projectName, projectDir, ghRef, resetMain });

await installDependencies(projectDir);

Expand Down Expand Up @@ -254,7 +256,7 @@ export const create = new Command('create')

console.log(`\nCreating '${projectName}' at '${projectDir}'\n`);

cloneCatalyst({ repository, projectName, projectDir, ghRef });
cloneCatalyst({ repository, projectName, projectDir, ghRef, resetMain });

writeEnv(projectDir, {
channelId: channelId.toString(),
Expand Down
15 changes: 15 additions & 0 deletions packages/create-catalyst/src/utils/clone-catalyst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@ import { execSync } from 'child_process';

import { checkoutRef } from './checkout-ref';
import { hasGitHubSSH } from './has-github-ssh';
import { resetBranchToRef } from './reset-branch-to-ref';

export const cloneCatalyst = ({
repository,
projectName,
projectDir,
ghRef,
resetMain = false,
}: {
repository: string;
projectName: string;
projectDir: string;
ghRef?: string;
resetMain?: boolean;
}) => {
const useSSH = hasGitHubSSH();

Expand All @@ -29,7 +32,19 @@ export const cloneCatalyst = ({
console.log();

if (ghRef) {
if (resetMain) {
checkoutRef(projectDir, 'main');

resetBranchToRef(projectDir, ghRef);

console.log(`Reset main to ${ghRef} successfully.`);
console.log();

return;
}

checkoutRef(projectDir, ghRef);

console.log();
}
};
9 changes: 9 additions & 0 deletions packages/create-catalyst/src/utils/reset-branch-to-ref.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { execSync } from 'node:child_process';

export function resetBranchToRef(projectDir: string, ghRef: string): void {
execSync(`git reset --hard ${ghRef}`, {
cwd: projectDir,
stdio: 'inherit',
encoding: 'utf8',
});
}
Comment on lines +3 to +9
Copy link
Contributor

@matthewvolk matthewvolk Dec 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed internally, in the unlikely event that a bad ghRef is passed, this will throw and kill the CLI process. This will leave the user running the CLI in a state where the repository was cloned successfully, but dependencies wouldn't be installed nor would the environment file be written (see code reference below)

cloneCatalyst({ repository, projectName, projectDir, ghRef });
writeEnv(projectDir, {
channelId: channelId.toString(),
storeHash,
storefrontToken,
arbitraryEnv: options.env,
});
await installDependencies(projectDir);

For now, we'll accept this since it's unlikely the ghRef provided by OCC will be rejected. That said, we do need to handle errors in a follow up PR.

Loading