@sordo/deploy-cloudflare 0.0.0-stage → 0.4.0

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/CHANGELOG.md ADDED
@@ -0,0 +1,13 @@
1
+ # @sordo/deploy-cloudflare
2
+
3
+ ## 0.4.0
4
+
5
+ ### Minor Changes
6
+
7
+ - b266211: New `@sordo/deploy-cloudflare` deploy plugin: `sordo deploy --target edge` runs `wrangler deploy` against the edge build, generating a minimal Wrangler config when the project has none. The README now documents the plugin and the manual Wrangler path (`.sordo/worker.js`, `bun run build`, external assets served under `/_sordo/assets/`), and the `sordo deploy` hint no longer calls the plugin unpublished.
8
+
9
+ ### Patch Changes
10
+
11
+ - Updated dependencies [b266211]
12
+ - Updated dependencies [5ebcc79]
13
+ - sordo@0.4.0
package/README.md CHANGED
@@ -1,3 +1,45 @@
1
- # Temporary Holding Version
1
+ # @sordo/deploy-cloudflare
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Deploy a Sordo `--target edge` build to Cloudflare Workers.
4
+
5
+ ```sh
6
+ bun add -d @sordo/deploy-cloudflare
7
+ bunx wrangler login
8
+ ```
9
+
10
+ ```ts
11
+ // sordo.config.ts
12
+ import { defineConfig } from "sordo/config";
13
+ import { cloudflare } from "@sordo/deploy-cloudflare";
14
+
15
+ export default defineConfig({
16
+ target: "edge",
17
+ plugins: [cloudflare({ name: "my-site" })],
18
+ });
19
+ ```
20
+
21
+ ```sh
22
+ sordo deploy --target edge
23
+ ```
24
+
25
+ `sordo deploy` builds, then the plugin runs `bunx wrangler deploy`:
26
+
27
+ - A `wrangler.jsonc` / `wrangler.json` / `wrangler.toml` in the project root (or
28
+ `options.config`) is used as-is.
29
+ - Otherwise `<outDir>/wrangler.generated.jsonc` is written with `name`,
30
+ `main: "worker.js"`, `compatibility_date`, and `assets` when the build used
31
+ `adapter.edge.assets: "external"`.
32
+
33
+ ## Options
34
+
35
+ | Option | Default | |
36
+ |---|---|---|
37
+ | `name` | package.json `name` | Worker name in the generated config |
38
+ | `config` | auto-detect | Path to your own Wrangler config |
39
+ | `compatibilityDate` | today (UTC) | Generated config only |
40
+ | `wranglerEnv` | — | `wrangler deploy --env <name>` |
41
+ | `dryRun` | `false` | `--dry-run`; also `SORDO_DEPLOY_DRY_RUN=1` |
42
+ | `args` | — | Extra `wrangler deploy` arguments |
43
+
44
+ `sordo deploy --env` selects a deploy plugin by name; it is not forwarded to
45
+ Wrangler. Use `wranglerEnv`. Secrets stay in Wrangler (`wrangler secret put`).
package/package.json CHANGED
@@ -1,6 +1,34 @@
1
1
  {
2
- "name": "@sordo/deploy-cloudflare",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
2
+ "name": "@sordo/deploy-cloudflare",
3
+ "version": "0.4.0",
4
+ "description": "Deploy a Sordo edge build to Cloudflare Workers with Wrangler.",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/bihaviour/sordo.git",
8
+ "directory": "packages/deploy-cloudflare"
9
+ },
10
+ "type": "module",
11
+ "license": "MIT",
12
+ "exports": {
13
+ ".": "./src/index.ts"
14
+ },
15
+ "files": [
16
+ "src",
17
+ "README.md",
18
+ "CHANGELOG.md"
19
+ ],
20
+ "scripts": {
21
+ "test": "bun test",
22
+ "typecheck": "tsc --noEmit",
23
+ "lint": "biome check ."
24
+ },
25
+ "peerDependencies": {
26
+ "sordo": ">=0.3.0"
27
+ },
28
+ "devDependencies": {
29
+ "sordo": "workspace:*"
30
+ },
31
+ "engines": {
32
+ "bun": ">=1.3.0"
33
+ }
34
+ }
package/src/index.ts ADDED
@@ -0,0 +1,134 @@
1
+ import { basename, join, resolve } from "node:path";
2
+ import type {
3
+ DeployArtifacts,
4
+ DeployContext,
5
+ DeployPlugin,
6
+ } from "sordo/plugin";
7
+
8
+ export interface RunResult {
9
+ code: number;
10
+ output: string;
11
+ }
12
+
13
+ export type Runner = (cmd: string[], cwd: string) => Promise<RunResult>;
14
+
15
+ export interface CloudflareDeployOptions {
16
+ /** Worker name for the generated config. Defaults to package.json `name`. */
17
+ name?: string;
18
+ /** Path to your own wrangler config. Skips config generation. */
19
+ config?: string;
20
+ /** `compatibility_date` for the generated config. Defaults to today (UTC). */
21
+ compatibilityDate?: string;
22
+ /** Wrangler environment (`wrangler deploy --env <name>`). */
23
+ wranglerEnv?: string;
24
+ /** Run `wrangler deploy --dry-run`; also enabled by SORDO_DEPLOY_DRY_RUN=1. */
25
+ dryRun?: boolean;
26
+ /** Extra arguments appended to `wrangler deploy`. */
27
+ args?: string[];
28
+ /** Replace the process runner (tests). */
29
+ run?: Runner;
30
+ }
31
+
32
+ const PLUGIN_NAME = "@sordo/deploy-cloudflare";
33
+ const WRANGLER_CONFIGS = ["wrangler.jsonc", "wrangler.json", "wrangler.toml"];
34
+ const WORKERS_DEV_URL = /https:\/\/[\w.-]+\.workers\.dev\S*/;
35
+
36
+ const spawnRunner: Runner = async (cmd, cwd) => {
37
+ const proc = Bun.spawn(cmd, { cwd, stdout: "pipe", stderr: "pipe" });
38
+ let output = "";
39
+ const pump = async (
40
+ stream: ReadableStream<Uint8Array>,
41
+ sink: "out" | "err",
42
+ ) => {
43
+ const decoder = new TextDecoder();
44
+ const reader = stream.getReader();
45
+ for (;;) {
46
+ const { done, value: chunk } = await reader.read();
47
+ if (done) break;
48
+ const text = decoder.decode(chunk, { stream: true });
49
+ output += text;
50
+ (sink === "out" ? process.stdout : process.stderr).write(text);
51
+ }
52
+ };
53
+ await Promise.all([pump(proc.stdout, "out"), pump(proc.stderr, "err")]);
54
+ return { code: await proc.exited, output };
55
+ };
56
+
57
+ async function findUserConfig(cwd: string): Promise<string | undefined> {
58
+ for (const name of WRANGLER_CONFIGS) {
59
+ const path = join(cwd, name);
60
+ if (await Bun.file(path).exists()) return path;
61
+ }
62
+ return undefined;
63
+ }
64
+
65
+ async function workerName(cwd: string, explicit?: string): Promise<string> {
66
+ if (explicit) return explicit;
67
+ const pkg = Bun.file(join(cwd, "package.json"));
68
+ if (await pkg.exists()) {
69
+ const { name } = (await pkg.json()) as { name?: string };
70
+ if (name) return name.replace(/^@[^/]+\//, "").replace(/[^a-z0-9-]/gi, "-");
71
+ }
72
+ return basename(cwd).replace(/[^a-z0-9-]/gi, "-");
73
+ }
74
+
75
+ async function generateConfig(
76
+ outDir: string,
77
+ cwd: string,
78
+ options: CloudflareDeployOptions,
79
+ ): Promise<string> {
80
+ const hasAssets = await Bun.file(
81
+ join(outDir, "worker-assets", "manifest.json"),
82
+ ).exists();
83
+ const config: Record<string, unknown> = {
84
+ name: await workerName(cwd, options.name),
85
+ main: "worker.js",
86
+ compatibility_date:
87
+ options.compatibilityDate ?? new Date().toISOString().slice(0, 10),
88
+ };
89
+ if (hasAssets) config.assets = { directory: "worker-assets" };
90
+ const path = join(outDir, "wrangler.generated.jsonc");
91
+ await Bun.write(path, `${JSON.stringify(config, null, 2)}\n`);
92
+ return path;
93
+ }
94
+
95
+ export function cloudflare(
96
+ options: CloudflareDeployOptions = {},
97
+ ): DeployPlugin {
98
+ const run = options.run ?? spawnRunner;
99
+ return {
100
+ name: PLUGIN_NAME,
101
+ deployTarget: "edge",
102
+ async deploy(
103
+ artifacts: DeployArtifacts,
104
+ ctx: DeployContext,
105
+ ): Promise<string | undefined> {
106
+ const outDir = resolve(ctx.cwd, artifacts.outDir);
107
+ if (!(await Bun.file(join(outDir, "worker.js")).exists())) {
108
+ throw new Error(
109
+ `no worker.js in ${outDir}. Build with \`sordo build --target edge\` first.`,
110
+ );
111
+ }
112
+
113
+ let configPath: string | undefined = options.config
114
+ ? resolve(ctx.cwd, options.config)
115
+ : await findUserConfig(ctx.cwd);
116
+ if (configPath) {
117
+ ctx.logger.info(`using ${configPath}`);
118
+ } else {
119
+ configPath = await generateConfig(outDir, ctx.cwd, options);
120
+ ctx.logger.info(`no wrangler config found; generated ${configPath}`);
121
+ }
122
+
123
+ const dryRun = options.dryRun ?? process.env.SORDO_DEPLOY_DRY_RUN === "1";
124
+ const cmd = ["bunx", "wrangler", "deploy", "--config", configPath];
125
+ if (options.wranglerEnv) cmd.push("--env", options.wranglerEnv);
126
+ if (dryRun) cmd.push("--dry-run");
127
+ if (options.args) cmd.push(...options.args);
128
+
129
+ const { code, output } = await run(cmd, ctx.cwd);
130
+ if (code !== 0) throw new Error(`wrangler deploy exited with ${code}`);
131
+ return dryRun ? undefined : output.match(WORKERS_DEV_URL)?.[0];
132
+ },
133
+ };
134
+ }