-
Notifications
You must be signed in to change notification settings - Fork 3
CLI local dev full proxy #226
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
Changes from all commits
3f5e4bc
b5adb6d
f146a07
da401f7
2d01152
a45bdf3
a0fa231
480a23b
3eca575
a8081af
52ee7bd
010c8d1
2ddd8fd
f96811a
8e085f7
6bc6400
a452cc1
61ee7da
1d40c86
6cf1543
faa4189
293b64a
91e5cb9
e302423
62071ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| root = true | ||
|
|
||
| [*] | ||
| indent_style = space | ||
| indent_size = 2 | ||
| charset = utf-8 | ||
| trim_trailing_whitespace = true | ||
| insert_final_newline = true | ||
| end_of_line = lf | ||
|
|
||
| [*.{md,ts.snap}] | ||
| trim_trailing_whitespace = false |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| { | ||
| "recommendations": ["biomejs.biome"] | ||
| } | ||
|
Comment on lines
+1
to
+3
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. plugin needed for correct work. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,23 @@ | ||
| { | ||
| "editor.defaultFormatter": "biomejs.biome", | ||
| "editor.formatOnSave": true, | ||
| "editor.codeActionsOnSave": { | ||
| "source.fixAll.biome": "explicit", | ||
| "source.organizeImports.biome": "explicit" | ||
| }, | ||
| "[typescript]": { | ||
| "editor.defaultFormatter": "biomejs.biome" | ||
| }, | ||
| "[typescriptreact]": { | ||
| "editor.defaultFormatter": "biomejs.biome" | ||
| }, | ||
| "[javascript]": { | ||
| "editor.defaultFormatter": "biomejs.biome" | ||
| }, | ||
| "[json]": { | ||
| "editor.defaultFormatter": "biomejs.biome" | ||
| }, | ||
| "[jsonc]": { | ||
| "editor.defaultFormatter": "biomejs.biome" | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missed it last time, should be explicit |
||
| } | ||
| } | ||
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,19 @@ | ||
| import { watch } from "node:fs"; | ||
| import { copyFile } from "node:fs/promises"; | ||
| import { join } from "node:path"; | ||
| import chalk from "chalk"; | ||
| import { BuildConfig } from "bun"; | ||
|
|
||
| const runBuild = async () => { | ||
| const result = await Bun.build({ | ||
| entrypoints: ["./src/cli/index.ts"], | ||
| outdir: "./dist/cli", | ||
| const runBuild = async (config: BuildConfig) => { | ||
| const defaultBuildOptions: Partial<BuildConfig> = { | ||
| target: "node", | ||
| format: "esm", | ||
| sourcemap: "external", | ||
| }; | ||
|
|
||
| const result = await Bun.build({ | ||
| ...defaultBuildOptions, | ||
| ...config, | ||
| }); | ||
|
|
||
| if (!result.success) { | ||
|
|
@@ -21,26 +27,43 @@ const runBuild = async () => { | |
| return result; | ||
| }; | ||
|
|
||
| const runAllBuilds = async () => { | ||
| const cli = await runBuild({ | ||
| entrypoints: ["./src/cli/index.ts"], | ||
| outdir: "./dist/cli", | ||
| }); | ||
| return { | ||
| cli, | ||
| }; | ||
| }; | ||
|
|
||
| const formatOutput = (outputs: { path: string }[]) => { | ||
| return outputs.map((o) => chalk.cyan(o.path)).join("\n "); | ||
| }; | ||
|
|
||
| if (process.argv.includes("--watch")) { | ||
| console.log(chalk.yellow("Watching for changes...")); | ||
|
|
||
| const changeHandler = async (event: "rename" | "change", filename: string | null) => { | ||
| const changeHandler = async ( | ||
| event: "rename" | "change", | ||
| filename: string | null | ||
| ) => { | ||
| const time = new Date().toLocaleTimeString(); | ||
| console.log(chalk.dim(`[${time}]`), chalk.gray(`${filename} ${event}d`)); | ||
|
|
||
| const result = await runBuild(); | ||
| console.log( | ||
| chalk.green(` ✓ Rebuilt`), | ||
| chalk.dim(`→`), | ||
| formatOutput(result.outputs) | ||
| ); | ||
| const { cli } = await runAllBuilds(); | ||
| for (const result of [cli]) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a preparation for additional entry |
||
| if (result.success && result.outputs.length > 0) { | ||
| console.log( | ||
| chalk.green(` ✓ Rebuilt`), | ||
| chalk.dim(`→`), | ||
| formatOutput(result.outputs) | ||
| ); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| await runBuild(); | ||
| await runAllBuilds(); | ||
|
|
||
| for (const dir of ["./src"]) { | ||
| watch(dir, { recursive: true }, changeHandler); | ||
|
|
@@ -49,8 +72,8 @@ if (process.argv.includes("--watch")) { | |
| // Keep process alive | ||
| await new Promise(() => {}); | ||
| } else { | ||
| const result = await runBuild(); | ||
| const { cli } = await runAllBuilds(); | ||
| console.log(chalk.green.bold(`\n✓ Build complete\n`)); | ||
| console.log(chalk.dim(" Output:")); | ||
| console.log(` ${formatOutput(result.outputs)}\n`); | ||
| console.log(` ${formatOutput(cli.outputs)}`); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import { Command } from "commander"; | ||
| import { createDevServer } from "@/cli/dev/dev-server/main"; | ||
| import { runCommand, theme } from "@/cli/utils/index.js"; | ||
| import type { RunCommandResult } from "@/cli/utils/runCommand.js"; | ||
| import type { CLIContext } from "../types.js"; | ||
|
|
||
| interface DevOptions { | ||
| port?: string; | ||
| } | ||
|
|
||
| async function devAction(options: DevOptions): Promise<RunCommandResult> { | ||
| const port = options.port ? Number(options.port) : undefined; | ||
| const { port: resolvedPort } = await createDevServer({ port }); | ||
|
|
||
| return { | ||
| outroMessage: `Dev server is available at ${theme.colors.links(`http://localhost:${resolvedPort}`)}`, | ||
| }; | ||
| } | ||
|
|
||
| export function getDevCommand(context: CLIContext): Command { | ||
| return new Command("dev") | ||
| .description("Start the development server") | ||
| .option("-p, --port <number>", "Port for the development server") | ||
| .action(async (options: DevOptions) => { | ||
| await runCommand( | ||
| () => devAction(options), | ||
| { requireAuth: true }, | ||
| context | ||
| ); | ||
| }); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| import type { Server } from "node:http"; | ||
| import cors from "cors"; | ||
| import express from "express"; | ||
| import getPort from "get-port"; | ||
| import { createProxyMiddleware } from "http-proxy-middleware"; | ||
|
|
||
| const DEFAULT_PORT = 4400; | ||
| const BASE44_APP_URL = "https://base44.app"; | ||
|
|
||
| interface DevServerOptions { | ||
| port?: number; | ||
| } | ||
|
|
||
| interface DevServerResult { | ||
| port: number; | ||
| server: Server; | ||
| } | ||
|
|
||
| export async function createDevServer( | ||
| options: DevServerOptions = {} | ||
| ): Promise<DevServerResult> { | ||
| const port = options.port ?? (await getPort({ port: DEFAULT_PORT })); | ||
|
|
||
| const app = express(); | ||
|
|
||
| const remoteProxy = createProxyMiddleware({ | ||
| target: BASE44_APP_URL, | ||
| changeOrigin: true, | ||
| }); | ||
|
|
||
| app.use( | ||
| cors({ | ||
| origin: /^http:\/\/localhost(:\d+)?$/, | ||
| credentials: true, | ||
| }) | ||
| ); | ||
|
|
||
| // Redirect OAuth routes to base44.app directly — proxying breaks the | ||
| // redirect flow and session cookies set by the provider. | ||
| const AUTH_ROUTE_PATTERN = /^\/api\/apps\/auth(\/|$)/; | ||
| app.use((req, res, next) => { | ||
| if (AUTH_ROUTE_PATTERN.test(req.path)) { | ||
| const targetUrl = new URL(req.originalUrl, BASE44_APP_URL); | ||
| return res.redirect(targetUrl.toString()); | ||
| } | ||
| next(); | ||
| }); | ||
|
|
||
| app.use((req, res, next) => { | ||
| return remoteProxy(req, res, next); | ||
| }); | ||
|
|
||
| return new Promise((resolve, reject) => { | ||
| const server = app.listen(port, "127.0.0.1", (err) => { | ||
| if (err) { | ||
| if ("code" in err && err.code === "EADDRINUSE") { | ||
| reject( | ||
| new Error( | ||
| `Port ${port} is already in use. Stop the other process and try again.` | ||
| ) | ||
| ); | ||
| } else { | ||
| reject(err); | ||
| } | ||
| } else { | ||
| resolve({ | ||
| port, | ||
| server, | ||
| }); | ||
| } | ||
| }); | ||
| }); | ||
|
Comment on lines
53
to
72
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this will help automatically to deal with
linterrors regarding "non empty last line in file"