Skip to content

Commit 2f5932e

Browse files
dario-piotrowiczpenalosa
authored andcommitted
fix: make sure C3 works with the latest Astro versions (#7497)
make sure that both the stable and experimental C3 Astro templates work with the latest Astro versions (which no longer include an src/env.d.ts file in their starting templates)
1 parent 13079fd commit 2f5932e

File tree

13 files changed

+149
-66
lines changed

13 files changed

+149
-66
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"create-cloudflare": patch
3+
---
4+
5+
fix: make sure C3 works with the latest Astro versions

packages/create-cloudflare/templates-experimental/astro/c3.ts

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { logRaw, updateStatus } from "@cloudflare/cli";
22
import { blue, brandColor, dim } from "@cloudflare/cli/colors";
33
import { runFrameworkGenerator } from "frameworks/index";
4-
import { loadTemplateSnippets, transformFile } from "helpers/codemod";
4+
import { transformFile } from "helpers/codemod";
55
import { runCommand } from "helpers/command";
66
import { usesTypescript } from "helpers/files";
77
import { detectPackageManager } from "helpers/packageManagers";
@@ -17,7 +17,7 @@ const generate = async (ctx: C3Context) => {
1717
logRaw(""); // newline
1818
};
1919

20-
const configure = async (ctx: C3Context) => {
20+
const configure = async () => {
2121
await runCommand([npx, "astro", "add", "cloudflare", "-y"], {
2222
silent: true,
2323
startText: "Installing adapter",
@@ -27,7 +27,6 @@ const configure = async (ctx: C3Context) => {
2727
});
2828

2929
updateAstroConfig();
30-
updateEnvDeclaration(ctx);
3130
};
3231

3332
const updateAstroConfig = () => {
@@ -59,43 +58,28 @@ const updateAstroConfig = () => {
5958
});
6059
};
6160

62-
const updateEnvDeclaration = (ctx: C3Context) => {
63-
if (!usesTypescript(ctx)) {
64-
return;
65-
}
66-
67-
const filePath = "src/env.d.ts";
68-
69-
updateStatus(`Adding type declarations in ${blue(filePath)}`);
70-
71-
transformFile(filePath, {
72-
visitProgram: function (n) {
73-
const snippets = loadTemplateSnippets(ctx);
74-
const patch = snippets.runtimeDeclarationTs;
75-
const b = recast.types.builders;
76-
77-
// Preserve comments with the new body
78-
const comments = n.get("comments").value;
79-
n.node.comments = comments.map((c: recast.types.namedTypes.CommentLine) =>
80-
b.commentLine(c.value),
81-
);
82-
83-
// Add the patch
84-
n.get("body").push(...patch);
85-
86-
return false;
87-
},
88-
});
89-
};
90-
9161
const config: TemplateConfig = {
9262
configVersion: 1,
9363
id: "astro",
9464
frameworkCli: "create-astro",
9565
platform: "workers",
9666
displayName: "Astro",
9767
copyFiles: {
98-
path: "./templates",
68+
async selectVariant(ctx) {
69+
// Note: this `selectVariant` function should not be needed
70+
// this is just a quick workaround until
71+
// https://github.com/cloudflare/workers-sdk/issues/7495
72+
// is resolved
73+
return usesTypescript(ctx) ? "ts" : "js";
74+
},
75+
variants: {
76+
js: {
77+
path: "./templates/js",
78+
},
79+
ts: {
80+
path: "./templates/ts",
81+
},
82+
},
9983
},
10084
devScript: "dev",
10185
deployScript: "deploy",
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
_worker.js
2+
_routes.json
3+
_headers
4+
_redirects
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Generated by Wrangler
2+
// After adding bindings to `wrangler.toml`, regenerate this interface via `npm run cf-typegen`
3+
interface Env {
4+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#:schema node_modules/wrangler/config-schema.json
2+
name = "<TBD>"
3+
compatibility_date = "<TBD>"
4+
compatibility_flags = ["nodejs_compat_v2"]
5+
main = "./dist/_worker.js/index.js"
6+
assets = { directory = "./dist", binding = "ASSETS" }
7+
8+
# Workers Logs
9+
# Docs: https://developers.cloudflare.com/workers/observability/logs/workers-logs/
10+
# Configuration: https://developers.cloudflare.com/workers/observability/logs/workers-logs/#enable-workers-logs
11+
[observability]
12+
enabled = true

packages/create-cloudflare/templates/astro/c3.ts

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { logRaw, updateStatus } from "@cloudflare/cli";
22
import { blue, brandColor, dim } from "@cloudflare/cli/colors";
33
import { runFrameworkGenerator } from "frameworks/index";
4-
import { loadTemplateSnippets, transformFile } from "helpers/codemod";
4+
import { transformFile } from "helpers/codemod";
55
import { runCommand } from "helpers/command";
66
import { usesTypescript } from "helpers/files";
77
import { detectPackageManager } from "helpers/packageManagers";
@@ -17,7 +17,7 @@ const generate = async (ctx: C3Context) => {
1717
logRaw(""); // newline
1818
};
1919

20-
const configure = async (ctx: C3Context) => {
20+
const configure = async () => {
2121
await runCommand([npx, "astro", "add", "cloudflare", "-y"], {
2222
silent: true,
2323
startText: "Installing adapter",
@@ -27,7 +27,6 @@ const configure = async (ctx: C3Context) => {
2727
});
2828

2929
updateAstroConfig();
30-
updateEnvDeclaration(ctx);
3130
};
3231

3332
const updateAstroConfig = () => {
@@ -59,43 +58,28 @@ const updateAstroConfig = () => {
5958
});
6059
};
6160

62-
const updateEnvDeclaration = (ctx: C3Context) => {
63-
if (!usesTypescript(ctx)) {
64-
return;
65-
}
66-
67-
const filePath = "src/env.d.ts";
68-
69-
updateStatus(`Adding type declarations in ${blue(filePath)}`);
70-
71-
transformFile(filePath, {
72-
visitProgram: function (n) {
73-
const snippets = loadTemplateSnippets(ctx);
74-
const patch = snippets.runtimeDeclarationTs;
75-
const b = recast.types.builders;
76-
77-
// Preserve comments with the new body
78-
const comments = n.get("comments").value;
79-
n.node.comments = comments.map((c: recast.types.namedTypes.CommentLine) =>
80-
b.commentLine(c.value),
81-
);
82-
83-
// Add the patch
84-
n.get("body").push(...patch);
85-
86-
return false;
87-
},
88-
});
89-
};
90-
9161
const config: TemplateConfig = {
9262
configVersion: 1,
9363
id: "astro",
9464
frameworkCli: "create-astro",
9565
platform: "pages",
9666
displayName: "Astro",
9767
copyFiles: {
98-
path: "./templates",
68+
async selectVariant(ctx) {
69+
// Note: this `selectVariant` function should not be needed
70+
// this is just a quick workaround until
71+
// https://github.com/cloudflare/workers-sdk/issues/7495
72+
// is resolved
73+
return usesTypescript(ctx) ? "ts" : "js";
74+
},
75+
variants: {
76+
js: {
77+
path: "./templates/js",
78+
},
79+
ts: {
80+
path: "./templates/ts",
81+
},
82+
},
9983
},
10084
devScript: "dev",
10185
deployScript: "deploy",

0 commit comments

Comments
 (0)