create-cloudflare 0.0.0-fd0fb230 → 0.0.0-fd2f1530

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-cloudflare",
3
- "version": "0.0.0-fd0fb230",
3
+ "version": "0.0.0-fd2f1530",
4
4
  "description": "A CLI for creating and deploying new applications to Cloudflare.",
5
5
  "keywords": [
6
6
  "cloudflare",
@@ -50,6 +50,7 @@
50
50
  "deepmerge": "^4.3.1",
51
51
  "degit": "^2.8.4",
52
52
  "dns2": "^2.1.0",
53
+ "dotenv": "^16.0.0",
53
54
  "esbuild": "^0.17.12",
54
55
  "execa": "^7.1.1",
55
56
  "glob": "^10.3.3",
@@ -60,14 +61,14 @@
60
61
  "recast": "^0.22.0",
61
62
  "semver": "^7.5.1",
62
63
  "typescript": "^5.0.2",
63
- "undici": "5.28.2",
64
+ "undici": "5.28.3",
64
65
  "vite-tsconfig-paths": "^4.0.8",
65
66
  "which-pm-runs": "^1.1.0",
66
67
  "yargs": "^17.7.1",
67
68
  "yarn": "^1.22.19",
68
- "@cloudflare/cli": "1.1.1",
69
69
  "@cloudflare/workers-tsconfig": "0.0.0",
70
- "wrangler": "3.28.3"
70
+ "wrangler": "3.30.1",
71
+ "@cloudflare/cli": "1.1.1"
71
72
  },
72
73
  "engines": {
73
74
  "node": ">=18.14.1"
@@ -77,6 +78,7 @@
77
78
  },
78
79
  "scripts": {
79
80
  "build": "node -r esbuild-register scripts/build.ts",
81
+ "dev:codemod": "node -r esbuild-register scripts/codemodDev.ts",
80
82
  "check:lint": "eslint .",
81
83
  "check:type": "tsc",
82
84
  "lint": "eslint",
@@ -1,10 +1,12 @@
1
- import { logRaw } from "@cloudflare/cli";
2
- import { brandColor, dim } from "@cloudflare/cli/colors";
1
+ import { logRaw, updateStatus } from "@cloudflare/cli";
2
+ import { blue, brandColor, dim } from "@cloudflare/cli/colors";
3
+ import { loadTemplateSnippets, transformFile } from "helpers/codemod";
3
4
  import { runCommand, runFrameworkGenerator } from "helpers/command";
4
- import { compatDateFlag } from "helpers/files";
5
+ import { usesTypescript } from "helpers/files";
5
6
  import { detectPackageManager } from "helpers/packages";
7
+ import * as recast from "recast";
6
8
  import type { TemplateConfig } from "../../src/templates";
7
- import type { C3Context } from "types";
9
+ import type { C3Context, PackageJson } from "types";
8
10
 
9
11
  const { npx } = detectPackageManager();
10
12
 
@@ -14,7 +16,7 @@ const generate = async (ctx: C3Context) => {
14
16
  logRaw(""); // newline
15
17
  };
16
18
 
17
- const configure = async () => {
19
+ const configure = async (ctx: C3Context) => {
18
20
  await runCommand([npx, "astro", "add", "cloudflare", "-y"], {
19
21
  silent: true,
20
22
  startText: "Installing adapter",
@@ -22,6 +24,67 @@ const configure = async () => {
22
24
  `via \`${npx} astro add cloudflare\``
23
25
  )}`,
24
26
  });
27
+
28
+ updateAstroConfig();
29
+ updateEnvDeclaration(ctx);
30
+ };
31
+
32
+ const updateAstroConfig = () => {
33
+ const filePath = "astro.config.mjs";
34
+
35
+ updateStatus(`Updating configuration in ${blue(filePath)}`);
36
+
37
+ transformFile(filePath, {
38
+ visitCallExpression: function (n) {
39
+ const callee = n.node.callee as recast.types.namedTypes.Identifier;
40
+ if (callee.name !== "cloudflare") {
41
+ return this.traverse(n);
42
+ }
43
+
44
+ const b = recast.types.builders;
45
+ n.node.arguments = [
46
+ b.objectExpression([
47
+ b.objectProperty(
48
+ b.identifier("runtime"),
49
+ b.objectExpression([
50
+ b.objectProperty(b.identifier("mode"), b.stringLiteral("local")),
51
+ ])
52
+ ),
53
+ ]),
54
+ ];
55
+
56
+ return false;
57
+ },
58
+ });
59
+ };
60
+
61
+ const updateEnvDeclaration = (ctx: C3Context) => {
62
+ if (!usesTypescript(ctx)) {
63
+ return;
64
+ }
65
+
66
+ const filePath = "src/env.d.ts";
67
+
68
+ updateStatus(`Adding type declarations in ${blue(filePath)}`);
69
+
70
+ transformFile(filePath, {
71
+ visitProgram: function (n) {
72
+ const snippets = loadTemplateSnippets(ctx);
73
+ const patch = snippets.runtimeDeclarationTs;
74
+ const b = recast.types.builders;
75
+
76
+ // Preserve comments with the new body
77
+ const comments = n.get("comments").value;
78
+ n.node.comments = comments.map((c: recast.types.namedTypes.CommentLine) =>
79
+ b.commentLine(c.value)
80
+ );
81
+
82
+ // Add the patch
83
+ n.get("body").push(...patch);
84
+
85
+ return false;
86
+ },
87
+ });
25
88
  };
26
89
 
27
90
  const config: TemplateConfig = {
@@ -29,12 +92,19 @@ const config: TemplateConfig = {
29
92
  id: "astro",
30
93
  platform: "pages",
31
94
  displayName: "Astro",
95
+ copyFiles: {
96
+ path: "./templates",
97
+ },
98
+ devScript: "dev",
99
+ deployScript: "deploy",
100
+ previewScript: "preview",
32
101
  generate,
33
102
  configure,
34
- transformPackageJson: async () => ({
103
+ transformPackageJson: async (pkgJson: PackageJson, ctx: C3Context) => ({
35
104
  scripts: {
36
- "pages:dev": `wrangler pages dev ${await compatDateFlag()} -- astro dev`,
37
- "pages:deploy": `astro build && wrangler pages deploy ./dist`,
105
+ deploy: `astro build && wrangler pages deploy ./dist`,
106
+ preview: `astro build && wrangler pages dev ./dist`,
107
+ ...(usesTypescript(ctx) && { "build-cf-types": `wrangler types` }),
38
108
  },
39
109
  }),
40
110
  testFlags: [
@@ -0,0 +1,4 @@
1
+ type Runtime = import("@astrojs/cloudflare").DirectoryRuntime<Env>;
2
+ declare namespace App {
3
+ interface Locals extends Runtime {}
4
+ }
@@ -0,0 +1,50 @@
1
+ name = "<TBD>"
2
+ compatibility_date = "<TBD>"
3
+
4
+ # Variable bindings. These are arbitrary, plaintext strings (similar to environment variables)
5
+ # Note: Use secrets to store sensitive data.
6
+ # Docs: https://developers.cloudflare.com/workers/platform/environment-variables
7
+ # [vars]
8
+ # MY_VARIABLE = "production_value"
9
+
10
+ # Bind a KV Namespace. Use KV as persistent storage for small key-value pairs.
11
+ # Docs: https://developers.cloudflare.com/workers/runtime-apis/kv
12
+ # [[kv_namespaces]]
13
+ # binding = "MY_KV_NAMESPACE"
14
+ # id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
15
+
16
+ # Bind an R2 Bucket. Use R2 to store arbitrarily large blobs of data, such as files.
17
+ # Docs: https://developers.cloudflare.com/r2/api/workers/workers-api-usage/
18
+ # [[r2_buckets]]
19
+ # binding = "MY_BUCKET"
20
+ # bucket_name = "my-bucket"
21
+
22
+ # Bind a Queue producer. Use this binding to schedule an arbitrary task that may be processed later by a Queue consumer.
23
+ # Docs: https://developers.cloudflare.com/queues/get-started
24
+ # [[queues.producers]]
25
+ # binding = "MY_QUEUE"
26
+ # queue = "my-queue"
27
+
28
+ # Bind a Queue consumer. Queue Consumers can retrieve tasks scheduled by Producers to act on them.
29
+ # Docs: https://developers.cloudflare.com/queues/get-started
30
+ # [[queues.consumers]]
31
+ # queue = "my-queue"
32
+
33
+ # Bind another Worker service. Use this binding to call another Worker without network overhead.
34
+ # Docs: https://developers.cloudflare.com/workers/platform/services
35
+ # [[services]]
36
+ # binding = "MY_SERVICE"
37
+ # service = "my-service"
38
+
39
+ # Bind a Durable Object. Durable objects are a scale-to-zero compute primitive based on the actor model.
40
+ # Durable Objects can live for as long as needed. Use these when you need a long-running "server", such as in realtime apps.
41
+ # Docs: https://developers.cloudflare.com/workers/runtime-apis/durable-objects
42
+ # [[durable_objects.bindings]]
43
+ # name = "MY_DURABLE_OBJECT"
44
+ # class_name = "MyDurableObject"
45
+
46
+ # Durable Object migrations.
47
+ # Docs: https://developers.cloudflare.com/workers/learning/using-durable-objects#configure-durable-object-classes-with-migrations
48
+ # [[migrations]]
49
+ # tag = "v1"
50
+ # new_classes = ["MyDurableObject"]
@@ -81,10 +81,10 @@ const config: TemplateConfig = {
81
81
  configVersion: 1,
82
82
  id: "nuxt",
83
83
  platform: "pages",
84
+ displayName: "Nuxt",
84
85
  copyFiles: {
85
86
  path: "./templates",
86
87
  },
87
- displayName: "Nuxt",
88
88
  devScript: "dev",
89
89
  deployScript: "deploy",
90
90
  generate,
@@ -93,6 +93,7 @@ const config: TemplateConfig = {
93
93
  scripts: {
94
94
  deploy: `${npm} run build && wrangler pages deploy ./dist`,
95
95
  preview: `${npm} run build && wrangler pages dev ./dist`,
96
+ "build-cf-types": `wrangler types`,
96
97
  },
97
98
  }),
98
99
  };
@@ -1,13 +1,13 @@
1
- import { endSection } from "@cloudflare/cli";
1
+ import { crash, endSection } from "@cloudflare/cli";
2
2
  import { brandColor } from "@cloudflare/cli/colors";
3
3
  import { spinner } from "@cloudflare/cli/interactive";
4
- import { parseTs, transformFile } from "helpers/codemod";
4
+ import { loadTemplateSnippets, transformFile } from "helpers/codemod";
5
5
  import { runCommand, runFrameworkGenerator } from "helpers/command";
6
6
  import { usesTypescript } from "helpers/files";
7
7
  import { detectPackageManager } from "helpers/packages";
8
+ import * as recast from "recast";
8
9
  import { quoteShellArgs } from "../../src/common";
9
10
  import type { TemplateConfig } from "../../src/templates";
10
- import type * as recast from "recast";
11
11
  import type { C3Context } from "types";
12
12
 
13
13
  const { npm, npx } = detectPackageManager();
@@ -23,6 +23,7 @@ const configure = async (ctx: C3Context) => {
23
23
  await runCommand(cmd);
24
24
 
25
25
  addBindingsProxy(ctx);
26
+ populateCloudflareEnv();
26
27
  };
27
28
 
28
29
  const addBindingsProxy = (ctx: C3Context) => {
@@ -35,43 +36,90 @@ const addBindingsProxy = (ctx: C3Context) => {
35
36
  const s = spinner();
36
37
  s.start("Updating `vite.config.ts`");
37
38
 
38
- // Insert the env declaration after the last import (but before the rest of the body)
39
- const envDeclaration = `
40
- let env = {};
41
-
42
- if(process.env.NODE_ENV === 'development') {
43
- const { getPlatformProxy } = await import('wrangler');
44
- const platformProxy = await getPlatformProxy();
45
- env = platformProxy.env;
46
- }
47
- `;
39
+ const snippets = loadTemplateSnippets(ctx);
40
+ const b = recast.types.builders;
48
41
 
49
42
  transformFile("vite.config.ts", {
43
+ // Insert the env declaration after the last import (but before the rest of the body)
50
44
  visitProgram: function (n) {
51
45
  const lastImportIndex = n.node.body.findLastIndex(
52
46
  (t) => t.type === "ImportDeclaration"
53
47
  );
54
- n.get("body").insertAt(lastImportIndex + 1, envDeclaration);
48
+ const lastImport = n.get("body", lastImportIndex);
49
+ lastImport.insertAfter(...snippets.getPlatformProxyTs);
50
+
51
+ return this.traverse(n);
52
+ },
53
+ // Pass the `platform` object from the declaration to the `qwikCity` plugin
54
+ visitCallExpression: function (n) {
55
+ const callee = n.node.callee as recast.types.namedTypes.Identifier;
56
+ if (callee.name !== "qwikCity") {
57
+ return this.traverse(n);
58
+ }
59
+
60
+ // The config object passed to `qwikCity`
61
+ const configArgument = n.node.arguments[0] as
62
+ | recast.types.namedTypes.ObjectExpression
63
+ | undefined;
64
+
65
+ const platformPropery = b.objectProperty.from({
66
+ key: b.identifier("platform"),
67
+ value: b.identifier("platform"),
68
+ shorthand: true,
69
+ });
70
+
71
+ if (!configArgument) {
72
+ n.node.arguments = [b.objectExpression([platformPropery])];
73
+
74
+ return false;
75
+ }
76
+
77
+ if (configArgument.type !== "ObjectExpression") {
78
+ crash("Failed to update `vite.config.ts`");
79
+ }
80
+
81
+ // Add the `platform` object to the object
82
+ configArgument.properties.push(platformPropery);
55
83
 
56
84
  return false;
57
85
  },
58
86
  });
59
87
 
60
- // Populate the `qwikCity` plugin with the platform object containing the `env` defined above.
61
- const platformObject = parseTs(`{ platform: { env } }`);
88
+ s.stop(`${brandColor("updated")} \`vite.config.ts\``);
89
+ };
62
90
 
63
- transformFile("vite.config.ts", {
64
- visitCallExpression: function (n) {
65
- const callee = n.node.callee as recast.types.namedTypes.Identifier;
66
- if (callee.name === "qwikCity") {
67
- n.node.arguments = [platformObject];
91
+ const populateCloudflareEnv = () => {
92
+ const entrypointPath = "src/entry.cloudflare-pages.tsx";
93
+
94
+ const s = spinner();
95
+ s.start(`Updating \`${entrypointPath}\``);
96
+
97
+ transformFile(entrypointPath, {
98
+ visitTSInterfaceDeclaration: function (n) {
99
+ const b = recast.types.builders;
100
+ const id = n.node.id as recast.types.namedTypes.Identifier;
101
+ if (id.name !== "QwikCityPlatform") {
102
+ this.traverse(n);
68
103
  }
69
104
 
70
- this.traverse(n);
105
+ const newBody = [
106
+ ["env", "Env"],
107
+ // Qwik doesn't supply `cf` to the platform object. Should they do so, uncomment this
108
+ // ["cf", "CfProperties"],
109
+ ].map(([varName, type]) =>
110
+ b.tsPropertySignature(
111
+ b.identifier(varName),
112
+ b.tsTypeAnnotation(b.tsTypeReference(b.identifier(type)))
113
+ )
114
+ );
115
+
116
+ n.node.body.body = newBody;
117
+
118
+ return false;
71
119
  },
72
120
  });
73
121
 
74
- s.stop(`${brandColor("updated")} \`vite.config.ts\``);
122
+ s.stop(`${brandColor("updated")} \`${entrypointPath}\``);
75
123
  };
76
124
 
77
125
  const config: TemplateConfig = {
@@ -89,6 +137,8 @@ const config: TemplateConfig = {
89
137
  transformPackageJson: async () => ({
90
138
  scripts: {
91
139
  deploy: `${npm} run build && wrangler pages deploy ./dist`,
140
+ preview: `${npm} run build && wrangler pages dev ./dist`,
141
+ "build-cf-types": `wrangler types`,
92
142
  },
93
143
  }),
94
144
  };
@@ -0,0 +1,6 @@
1
+ let platform = {};
2
+
3
+ if(process.env.NODE_ENV === 'development') {
4
+ const { getPlatformProxy } = await import('wrangler');
5
+ platform = await getPlatformProxy();
6
+ }
@@ -0,0 +1,3 @@
1
+ // Generated by Wrangler on Fri Feb 16 2024 15:52:18 GMT-0600 (Central Standard Time)
2
+ // After adding bindings to `wrangler.toml`, regenerate this interface via `npm build-cf-types`
3
+ interface Env {}
@@ -1,4 +1,7 @@
1
1
  import { logRaw } from "@cloudflare/cli";
2
+ import { brandColor, dim } from "@cloudflare/cli/colors";
3
+ import { spinner } from "@cloudflare/cli/interactive";
4
+ import { transformFile } from "helpers/codemod";
2
5
  import { runFrameworkGenerator } from "helpers/command.js";
3
6
  import { detectPackageManager } from "helpers/packages";
4
7
  import type { TemplateConfig } from "../../src/templates";
@@ -10,24 +13,55 @@ const generate = async (ctx: C3Context) => {
10
13
  await runFrameworkGenerator(ctx, [
11
14
  ctx.project.name,
12
15
  "--template",
13
- "https://github.com/remix-run/remix/tree/main/templates/cloudflare-pages",
16
+ "https://github.com/remix-run/remix/tree/main/templates/vite-cloudflare",
14
17
  ]);
15
18
 
16
19
  logRaw(""); // newline
17
20
  };
18
21
 
22
+ const configure = async () => {
23
+ const typeDefsPath = "load-context.ts";
24
+
25
+ const s = spinner();
26
+ s.start(`Updating \`${typeDefsPath}\``);
27
+
28
+ // Remove the empty Env declaration from the template to allow the type from
29
+ // worker-configuration.d.ts to take over
30
+ transformFile(typeDefsPath, {
31
+ visitTSInterfaceDeclaration(n) {
32
+ if (n.node.id.type === "Identifier" && n.node.id.name !== "Env") {
33
+ return this.traverse(n);
34
+ }
35
+
36
+ // Removes the node
37
+ n.replace();
38
+ return false;
39
+ },
40
+ });
41
+
42
+ s.stop(`${brandColor("updated")} \`${dim(typeDefsPath)}\``);
43
+ };
44
+
19
45
  const config: TemplateConfig = {
20
46
  configVersion: 1,
21
47
  id: "remix",
22
- displayName: "Remix",
23
48
  platform: "pages",
49
+ displayName: "Remix",
50
+ copyFiles: {
51
+ path: "./templates",
52
+ },
24
53
  generate,
54
+ configure,
25
55
  transformPackageJson: async () => ({
26
56
  scripts: {
27
- "pages:deploy": `${npm} run build && wrangler pages deploy ./public`,
57
+ deploy: `${npm} run build && wrangler pages deploy ./build/client`,
58
+ preview: `${npm} run build && wrangler pages dev ./build/client`,
59
+ "build-cf-types": `wrangler types`,
28
60
  },
29
61
  }),
30
62
  devScript: "dev",
63
+ deployScript: "deploy",
64
+ previewScript: "preview",
31
65
  testFlags: ["--typescript", "--no-install", "--no-git-init"],
32
66
  };
33
67
  export default config;
@@ -0,0 +1,3 @@
1
+ // Generated by Wrangler on Fri Feb 16 2024 15:52:18 GMT-0600 (Central Standard Time)
2
+ // After adding bindings to `wrangler.toml`, regenerate this interface via `npm build-cf-types`
3
+ interface Env {}
@@ -0,0 +1,50 @@
1
+ name = "<TBD>"
2
+ compatibility_date = "<TBD>"
3
+
4
+ # Variable bindings. These are arbitrary, plaintext strings (similar to environment variables)
5
+ # Note: Use secrets to store sensitive data.
6
+ # Docs: https://developers.cloudflare.com/workers/platform/environment-variables
7
+ # [vars]
8
+ # MY_VARIABLE = "production_value"
9
+
10
+ # Bind a KV Namespace. Use KV as persistent storage for small key-value pairs.
11
+ # Docs: https://developers.cloudflare.com/workers/runtime-apis/kv
12
+ # [[kv_namespaces]]
13
+ # binding = "MY_KV_NAMESPACE"
14
+ # id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
15
+
16
+ # Bind an R2 Bucket. Use R2 to store arbitrarily large blobs of data, such as files.
17
+ # Docs: https://developers.cloudflare.com/r2/api/workers/workers-api-usage/
18
+ # [[r2_buckets]]
19
+ # binding = "MY_BUCKET"
20
+ # bucket_name = "my-bucket"
21
+
22
+ # Bind a Queue producer. Use this binding to schedule an arbitrary task that may be processed later by a Queue consumer.
23
+ # Docs: https://developers.cloudflare.com/queues/get-started
24
+ # [[queues.producers]]
25
+ # binding = "MY_QUEUE"
26
+ # queue = "my-queue"
27
+
28
+ # Bind a Queue consumer. Queue Consumers can retrieve tasks scheduled by Producers to act on them.
29
+ # Docs: https://developers.cloudflare.com/queues/get-started
30
+ # [[queues.consumers]]
31
+ # queue = "my-queue"
32
+
33
+ # Bind another Worker service. Use this binding to call another Worker without network overhead.
34
+ # Docs: https://developers.cloudflare.com/workers/platform/services
35
+ # [[services]]
36
+ # binding = "MY_SERVICE"
37
+ # service = "my-service"
38
+
39
+ # Bind a Durable Object. Durable objects are a scale-to-zero compute primitive based on the actor model.
40
+ # Durable Objects can live for as long as needed. Use these when you need a long-running "server", such as in realtime apps.
41
+ # Docs: https://developers.cloudflare.com/workers/runtime-apis/durable-objects
42
+ # [[durable_objects.bindings]]
43
+ # name = "MY_DURABLE_OBJECT"
44
+ # class_name = "MyDurableObject"
45
+
46
+ # Durable Object migrations.
47
+ # Docs: https://developers.cloudflare.com/workers/learning/using-durable-objects#configure-durable-object-classes-with-migrations
48
+ # [[migrations]]
49
+ # tag = "v1"
50
+ # new_classes = ["MyDurableObject"]