-
Notifications
You must be signed in to change notification settings - Fork 7
feat: add base44 exec command
#357
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
Merged
Merged
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
69f9806
feat: add `base44 exec` command for running scripts with pre-authenti…
netanelgilad 1adcbe3
fix: only consider stdin mode when no file or eval is provided
netanelgilad 495e1c1
fix: add --node-modules-dir=auto for Deno 2.x npm: specifier support
netanelgilad 7d50dc2
fix: copy exec wrapper out of node_modules before running with Deno
netanelgilad 9247e25
fix: call cleanup() after script completes so Deno can exit
netanelgilad d1eac96
fix: format spawn args array and add exec command tests
netanelgilad cf7deb9
fix: validate input args before checking for Deno
netanelgilad 8d045dd
Merge branch 'main' into feat/exec-command
netanelgilad 8d65f98
fix: route function calls through app subdomain in exec
netanelgilad 10dc541
fix: address PR review comments on exec command
github-actions[bot] eda2b29
Merge remote-tracking branch 'origin/main' into feat/exec-command
netanelgilad a35af35
refactor(exec): address PR review feedback
netanelgilad 669fcec
stdin
netanelgilad 7bf3b1a
Merge main into feat/exec-command
github-actions[bot] 9763325
fix: resolve lint, knip, and test failures in exec command
netanelgilad 17d9467
fix: include dist/deno-runtime in npm package files
netanelgilad 02648fb
fix: add Deno to CI and move exec wrapper into assets system
netanelgilad 03703f4
Merge remote-tracking branch 'origin/main' into feat/exec-command
netanelgilad a48b41b
fix: migrate exec command to Base44Command after main merge
netanelgilad cd7698d
no rename
netanelgilad 15d7c84
cleanups
netanelgilad 185b23e
remove extra args
netanelgilad 2efd491
cleanup
netanelgilad 658445f
cleaner
netanelgilad cce9fc0
test
netanelgilad a32ead4
lint fixes
netanelgilad 5409d70
move to project/api
netanelgilad dee89b6
move getAppConfig
netanelgilad d85d1e8
non interactive
netanelgilad 99e33ef
better example
netanelgilad 38d62d5
better help
netanelgilad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| /** | ||
| * Deno Exec Wrapper | ||
| * | ||
| * This script is executed by Deno to run user scripts with the Base44 SDK | ||
| * pre-authenticated and available as a global `base44` variable. | ||
| * | ||
| * Environment variables: | ||
| * - SCRIPT_PATH: Absolute path (or file:// URL) to the user's script | ||
| * - BASE44_APP_ID: App identifier from .app.jsonc | ||
| * - BASE44_ACCESS_TOKEN: User's access token | ||
| * - BASE44_APP_BASE_URL: App's published URL / subdomain (used for function calls) | ||
| */ | ||
|
|
||
| export {}; | ||
|
|
||
| const scriptPath = Deno.env.get("SCRIPT_PATH"); | ||
| const appId = Deno.env.get("BASE44_APP_ID"); | ||
| const accessToken = Deno.env.get("BASE44_ACCESS_TOKEN"); | ||
| const appBaseUrl = Deno.env.get("BASE44_APP_BASE_URL"); | ||
|
|
||
| if (!scriptPath) { | ||
| console.error("SCRIPT_PATH environment variable is required"); | ||
| Deno.exit(1); | ||
| } | ||
|
|
||
| if (!appId || !accessToken) { | ||
| console.error("BASE44_APP_ID and BASE44_ACCESS_TOKEN are required"); | ||
| Deno.exit(1); | ||
| } | ||
|
|
||
| if (!appBaseUrl) { | ||
| console.error("BASE44_APP_BASE_URL environment variable is required"); | ||
| Deno.exit(1); | ||
| } | ||
|
|
||
| import { createClient } from "npm:@base44/sdk"; | ||
|
|
||
| const base44 = createClient({ | ||
| appId, | ||
| token: accessToken, | ||
| serverUrl: appBaseUrl, | ||
| }); | ||
|
|
||
| (globalThis as any).base44 = base44; | ||
|
|
||
| try { | ||
| await import(scriptPath); | ||
| } catch (error) { | ||
| console.error("Failed to execute script:", error); | ||
| Deno.exit(1); | ||
| } finally { | ||
| // Clean up the SDK client (clears analytics heartbeat interval, | ||
| // disconnects socket) so the process can exit naturally. | ||
| base44.cleanup(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| import type { Command } from "commander"; | ||
| import type { RunCommandResult } from "@/cli/types.js"; | ||
| import { Base44Command } from "@/cli/utils/index.js"; | ||
| import { InvalidInputError } from "@/core/errors.js"; | ||
| import { runScript } from "@/core/exec/index.js"; | ||
| import { getAppConfig } from "@/core/project/index.js"; | ||
|
|
||
| function readStdin(): Promise<string> { | ||
| return new Promise((resolve, reject) => { | ||
| let data = ""; | ||
| process.stdin.setEncoding("utf-8"); | ||
| process.stdin.on("data", (chunk: string) => { | ||
| data += chunk; | ||
| }); | ||
| process.stdin.on("end", () => resolve(data)); | ||
| process.stdin.on("error", reject); | ||
| }); | ||
| } | ||
|
|
||
| async function execAction( | ||
| isNonInteractive: boolean, | ||
| ): Promise<RunCommandResult> { | ||
| const noInputError = new InvalidInputError( | ||
| "No input provided. Pipe a script to stdin.", | ||
| { | ||
| hints: [ | ||
| { message: "File: cat ./script.ts | base44 exec" }, | ||
| { | ||
| message: | ||
| 'Eval: echo "const users = await base44.entities.User.list(); console.log(users)" | base44 exec', | ||
| }, | ||
| ], | ||
| }, | ||
| ); | ||
|
|
||
| if (!isNonInteractive) { | ||
| throw noInputError; | ||
| } | ||
|
|
||
| const code = await readStdin(); | ||
|
|
||
| if (!code.trim()) { | ||
| throw noInputError; | ||
| } | ||
|
|
||
| const { exitCode } = await runScript({ appId: getAppConfig().id, code }); | ||
|
|
||
| if (exitCode !== 0) { | ||
| process.exitCode = exitCode; | ||
| } | ||
|
|
||
| return {}; | ||
| } | ||
|
|
||
| export function getExecCommand(): Command { | ||
| return new Base44Command("exec") | ||
| .description( | ||
| "Run a script with the Base44 SDK pre-authenticated as the current user", | ||
| ) | ||
kfirstri marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| .addHelpText( | ||
| "after", | ||
| ` | ||
| Examples: | ||
| Run a script file: | ||
| $ cat ./script.ts | base44 exec | ||
|
|
||
| Inline script: | ||
| $ echo "const users = await base44.entities.User.list()" | base44 exec`, | ||
| ) | ||
| .action(async (_options: unknown, command: Base44Command) => { | ||
| return await execAction(command.isNonInteractive); | ||
| }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export * from "./run-script.js"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| import { spawn } from "node:child_process"; | ||
| import { copyFileSync, writeFileSync } from "node:fs"; | ||
| import { file } from "tmp-promise"; | ||
| import { getExecWrapperPath } from "@/core/assets.js"; | ||
| import { getAppUserToken, getSiteUrl } from "@/core/project/api.js"; | ||
| import { verifyDenoInstalled } from "@/core/utils/index.js"; | ||
|
|
||
| interface RunScriptOptions { | ||
| appId: string; | ||
| code: string; | ||
| } | ||
|
|
||
| interface RunScriptResult { | ||
| exitCode: number; | ||
| } | ||
|
|
||
| export async function runScript( | ||
| options: RunScriptOptions, | ||
| ): Promise<RunScriptResult> { | ||
| const { appId, code } = options; | ||
|
|
||
| verifyDenoInstalled("to run scripts with exec"); | ||
|
|
||
| const cleanupFns: (() => void)[] = []; | ||
|
|
||
| const tempScript = await file({ postfix: ".ts" }); | ||
| cleanupFns.push(tempScript.cleanup); | ||
| writeFileSync(tempScript.path, code, "utf-8"); | ||
| const scriptPath = `file://${tempScript.path}`; | ||
|
|
||
| const [appUserToken, appBaseUrl] = await Promise.all([ | ||
| getAppUserToken(), | ||
| getSiteUrl(), | ||
| ]); | ||
|
|
||
| // Copy the exec wrapper to a temp location outside node_modules. | ||
| // This works with both Deno 1.x and 2.x, but is required for Deno 2.x | ||
| // which treats files inside node_modules as Node modules and blocks | ||
| // npm: specifiers in them. | ||
| const tempWrapper = await file({ postfix: ".ts" }); | ||
| cleanupFns.push(tempWrapper.cleanup); | ||
| copyFileSync(getExecWrapperPath(), tempWrapper.path); | ||
|
|
||
| try { | ||
| const exitCode = await new Promise<number>((resolvePromise) => { | ||
| const child = spawn( | ||
| "deno", | ||
| ["run", "--allow-all", "--node-modules-dir=auto", tempWrapper.path], | ||
| { | ||
| env: { | ||
| ...process.env, | ||
| SCRIPT_PATH: scriptPath, | ||
| BASE44_APP_ID: appId, | ||
| BASE44_ACCESS_TOKEN: appUserToken, | ||
| BASE44_APP_BASE_URL: appBaseUrl, | ||
| }, | ||
| stdio: "inherit", | ||
| }, | ||
| ); | ||
|
|
||
| child.on("close", (code) => { | ||
| resolvePromise(code ?? 1); | ||
| }); | ||
| }); | ||
|
|
||
| return { exitCode }; | ||
| } finally { | ||
| for (const cleanup of cleanupFns) { | ||
| cleanup(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.