Skip to content

feat(cli): support CF when running dev command #895

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
74 changes: 56 additions & 18 deletions packages/actor-core-cli/src/commands/dev.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import * as path from "node:path";
import { Argument, Command, Option } from "commander";
import { workflow } from "../workflow";

import { validateConfigTask } from "../workflows/validate-config";
import chokidar from "chokidar";
import { Text } from "ink";
import open from "open";
import { withResolvers } from "../utils/mod";
import { spawn } from "node:child_process";
import { mkdir, writeFile } from "node:fs/promises";
import dedent from "dedent";
import * as pkg from "../../package.json";

export const dev = new Command()
.name("dev")
Expand All @@ -30,6 +31,11 @@ export const dev = new Command()
true,
),
)
.addOption(
new Option("--runtime [runtime]", "Specify which platform to use").choices([
"cloudflare",
]),
)
.option("--no-open", "Do not open the browser with ActorCore Studio")
.action(action);

Expand All @@ -39,10 +45,42 @@ export async function action(
root: string;
port?: string;
open: boolean;
runtime?: string;
},
) {
const cwd = path.join(process.cwd(), opts.root);

if (opts.runtime === "cloudflare") {
console.log("");
console.log(` 🎭 ActorCore v${pkg.version}`);
console.log(" Handing over to Wrangler... 👋🏻");
console.log("");
await mkdir(path.join(cwd, ".actor-core"), { recursive: true });

const entry = path.join(cwd, ".actor-core", "cf-entry.js");
await writeFile(
entry,
dedent`
import { createHandler } from "@actor-core/cloudflare-workers";
import { app } from "${path.join(cwd, appPath)}";
app.config.inspector = {
enabled: true,
};
app.config.cors = {
origin: (origin) => origin,
};
const { handler, ActorHandler } = createHandler(app);
export { handler as default, ActorHandler };
`,
);

spawn("npx", ["wrangler@latest", "dev", "--port", "6240", entry], {
cwd,
stdio: "inherit",
});
return;
}

await workflow(
`Run locally your ActorCore project (${appPath})`,
async function* (ctx) {
Expand All @@ -60,21 +98,22 @@ export async function action(
ignored: (path) => path.includes("node_modules"),
});

function createServer() {
return spawn(
process.execPath,
[
path.join(
path.dirname(require.resolve("@actor-core/cli")),
"server-entry.js",
),
],
{
env: { ...process.env, PORT: opts.port, APP_PATH: appPath },
cwd,
stdio: "overlapped",
},
async function createServer() {
const serverEntry = path.join(
path.dirname(require.resolve("@actor-core/cli")),
"server-entry.js",
);

return spawn(process.execPath, [serverEntry], {
env: {
...process.env,
PORT: opts.port,
APP_PATH: appPath,
RUNTIME: opts.runtime,
},
cwd,
stdio: "overlapped",
});
}

let server: ReturnType<typeof spawn> | undefined = undefined;
Expand Down Expand Up @@ -103,11 +142,10 @@ export async function action(
createLock();

while (true) {
yield* validateConfigTask(ctx, cwd, appPath);
yield* ctx.task(
"Server started. Watching for changes",
async function* (ctx) {
server = createServer();
server = await createServer();
if (server?.stdout) {
yield ctx.attach(server.stdout, server.stderr);
}
Expand Down
9 changes: 8 additions & 1 deletion packages/platforms/cloudflare-workers/src/manager_driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type {
} from "actor-core/driver-helpers";
import { Bindings } from "./mod";
import { logger } from "./log";
import { ManagerInspector } from "actor-core/inspector";

// Define metadata type for CloudflareKV
interface KVMetadata {
Expand Down Expand Up @@ -40,6 +41,11 @@ const KEYS = {
};

export class CloudflareWorkersManagerDriver implements ManagerDriver {
inspector: ManagerInspector = new ManagerInspector(this, {
getAllActors: async () => [],
getAllTypesOfActors: async () => [],
});

async getForId({
c,
baseUrl,
Expand Down Expand Up @@ -186,6 +192,8 @@ export class CloudflareWorkersManagerDriver implements ManagerDriver {
tags,
});

this.inspector.onActorsChange([]);

// Store combined actor metadata (name and tags)
const actorData: ActorData = { name, tags };
await c.env.ACTOR_KV.put(
Expand Down Expand Up @@ -240,4 +248,3 @@ export class CloudflareWorkersManagerDriver implements ManagerDriver {
function buildActorEndpoint(baseUrl: string, actorId: string) {
return `${baseUrl}/actors/${actorId}`;
}

Loading