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

C3: Detect production branch when creating pages project #3644

Merged
merged 3 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions .changeset/nine-apes-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-cloudflare": patch
---

Detect production branch when creating pages project
18 changes: 18 additions & 0 deletions packages/create-cloudflare/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,3 +315,21 @@ export async function initializeGit(cwd: string) {
await runCommand(`git init`, { useSpinner: false, silent: true, cwd });
}
}

export async function getProductionBranch(cwd: string) {
try {
const producitonBranch = await runCommand(
jculvey marked this conversation as resolved.
Show resolved Hide resolved
"git rev-parse --abbrev-ref HEAD",
jculvey marked this conversation as resolved.
Show resolved Hide resolved
{
silent: true,
cwd,
useSpinner: false,
captureOutput: true,
}
);

return producitonBranch.trim();
jculvey marked this conversation as resolved.
Show resolved Hide resolved
} catch (err) {}

return "main";
Comment on lines +333 to +335
Copy link
Contributor

Choose a reason for hiding this comment

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

What happens if the user chose to not use git? Will the default main still work or should we throw a better error than the potential crash would?

Copy link
Contributor

Choose a reason for hiding this comment

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

Pages needs a "production branch" even if git isn't used (e.g. for direct uploads). It is used as a tag for the production environment. So I think this is OK.
We should move this return statement inside the catch block, though, rather than having an empty catch block.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It'll default to the current behavior. We need to supply a --productionBranch argument to the pages create command, otherwise the call to wrangler pages project create will fail when it demands interactive input.

}
2 changes: 1 addition & 1 deletion packages/create-cloudflare/src/helpers/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const runCommand = async (

return printAsyncStatus({
useSpinner: opts.useSpinner ?? opts.silent,
startText: opts.startText || command.join(" "),
startText: opts.startText || command.join(" ").trim(),
doneText: opts.doneText,
promise() {
const [executable, ...args] = command;
Expand Down
7 changes: 5 additions & 2 deletions packages/create-cloudflare/src/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
import { detectPackageManager } from "helpers/packages";
import { C3_DEFAULTS } from "./cli";
import {
getProductionBranch,
gitCommit,
isInsideGitRepo,

Check failure on line 15 in packages/create-cloudflare/src/pages.ts

View workflow job for this annotation

GitHub Actions / Checks (ubuntu-latest)

'isInsideGitRepo' is defined but never used
offerGit,
offerToDeploy,
printSummary,
Expand Down Expand Up @@ -135,7 +137,8 @@
? `--compatibility-flags ${compatFlags}`
: "";

const cmd = `${npx} wrangler pages project create ${ctx.project.name} --production-branch main ${compatFlagsArg}`;
const productionBranch = await getProductionBranch(ctx.project.path);
const cmd = `${npx} wrangler pages project create ${ctx.project.name} --production-branch ${productionBranch} ${compatFlagsArg}`;

try {
await retry(CREATE_PROJECT_RETRIES, async () =>
Expand All @@ -144,7 +147,7 @@
cwd: ctx.project.path,
env: { CLOUDFLARE_ACCOUNT_ID },
startText: "Creating Pages project",
doneText: `${brandColor("created")} ${dim(`via \`${cmd}\``)}`,
doneText: `${brandColor("created")} ${dim(`via \`${cmd.trim()}\``)}`,
})
);
} catch (error) {
Expand Down
Loading