-
Notifications
You must be signed in to change notification settings - Fork 8
feat(functions): add functions pull command
#382
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
yardend-wix
merged 15 commits into
feature/functions-commands
from
feature/functions-pull-command
Mar 12, 2026
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
77758f0
feat(functions): add `functions list` command
yardend-wix 5c77e60
fix: lint formatting and remove unused FunctionInfo type
yardend-wix 4d7fdde
feat(functions): add `functions pull` command
yardend-wix 01f45fa
fix: lint formatting and remove unused WriteFunctionsResult export
yardend-wix 76bedc3
Merge branch 'main' into feature/functions-list-command
kfirstri 521c006
Merge branch 'main' into feature/functions-pull-command
kfirstri cc7f935
fix: transform snake_case fields to camelCase in function schemas
yardend-wix 0777843
fix: camelCase transforms, runTask spinner, rename auto→automation vars
yardend-wix 5406f17
merge: resolve merge conflict with main (TestAPIServer migration)
yardend-wix 31e9be2
merge: resolve merge conflict with main (TestAPIServer migration)
yardend-wix 6318969
fix: remove unused mockSingleFunctionDelete methods from TestAPIServer
yardend-wix 3d3b75d
refactor: use isDeepStrictEqual for pull comparison, add pull tests
yardend-wix e8fa31e
fix: use full AutomationSchema for list endpoint response parsing
yardend-wix 0adc760
merge feature/functions-list-command into pull branch
yardend-wix b61086e
merge: resolve conflicts with feature/functions-commands branch
yardend-wix 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
Some comments aren't visible on the classic Files Changed page.
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,79 @@ | ||
| import { dirname, join } from "node:path"; | ||
| import { log } from "@clack/prompts"; | ||
| import { Command } from "commander"; | ||
| import type { CLIContext } from "@/cli/types.js"; | ||
| import { runCommand, runTask } from "@/cli/utils/index.js"; | ||
| import type { RunCommandResult } from "@/cli/utils/runCommand.js"; | ||
| import { readProjectConfig } from "@/core/index.js"; | ||
| import { listDeployedFunctions } from "@/core/resources/function/api.js"; | ||
| import { writeFunctions } from "@/core/resources/function/pull.js"; | ||
|
|
||
| async function pullFunctionsAction( | ||
| name: string | undefined, | ||
| ): Promise<RunCommandResult> { | ||
| const { project } = await readProjectConfig(); | ||
|
|
||
| const configDir = dirname(project.configPath); | ||
| const functionsDir = join(configDir, project.functionsDir); | ||
|
|
||
| const remoteFunctions = await runTask( | ||
| "Fetching functions from Base44", | ||
| async () => { | ||
| const { functions } = await listDeployedFunctions(); | ||
| return functions; | ||
| }, | ||
| { | ||
| successMessage: "Functions fetched successfully", | ||
| errorMessage: "Failed to fetch functions", | ||
| }, | ||
| ); | ||
|
|
||
| const toPull = name | ||
| ? remoteFunctions.filter((f) => f.name === name) | ||
| : remoteFunctions; | ||
|
|
||
| if (name && toPull.length === 0) { | ||
| return { | ||
| outroMessage: `Function "${name}" not found on remote`, | ||
| }; | ||
| } | ||
|
|
||
| if (toPull.length === 0) { | ||
| return { outroMessage: "No functions found on remote" }; | ||
| } | ||
|
|
||
| const { written, skipped } = await runTask( | ||
| "Writing function files", | ||
| async () => { | ||
| return await writeFunctions(functionsDir, toPull); | ||
| }, | ||
| { | ||
| successMessage: "Function files written successfully", | ||
| errorMessage: "Failed to write function files", | ||
| }, | ||
| ); | ||
|
|
||
| for (const name of written) { | ||
| log.success(`${name.padEnd(25)} written`); | ||
| } | ||
| for (const name of skipped) { | ||
| log.info(`${name.padEnd(25)} unchanged`); | ||
| } | ||
|
|
||
| return { | ||
| outroMessage: `Pulled ${toPull.length} function${toPull.length !== 1 ? "s" : ""} to ${functionsDir}`, | ||
| }; | ||
| } | ||
|
|
||
| export function getPullCommand(context: CLIContext): Command { | ||
| return new Command("pull") | ||
| .description("Pull deployed functions from Base44") | ||
| .argument("[name]", "Pull a single function by name") | ||
| .action(async (name: string | undefined) => { | ||
| await runCommand( | ||
| () => pullFunctionsAction(name), | ||
| { requireAuth: true }, | ||
| context, | ||
| ); | ||
| }); | ||
| } | ||
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 |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| export * from "./api.js"; | ||
| export * from "./config.js"; | ||
| export * from "./deploy.js"; | ||
| export * from "./pull.js"; | ||
| export * from "./resource.js"; | ||
| export * from "./schema.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,102 @@ | ||
| import { join } from "node:path"; | ||
| import { isDeepStrictEqual } from "node:util"; | ||
| import type { FunctionInfo } from "@/core/resources/function/schema.js"; | ||
| import { | ||
| pathExists, | ||
| readJsonFile, | ||
| readTextFile, | ||
| writeFile, | ||
| writeJsonFile, | ||
| } from "@/core/utils/fs.js"; | ||
|
|
||
| interface WriteFunctionsResult { | ||
| written: string[]; | ||
| skipped: string[]; | ||
| } | ||
|
|
||
| /** | ||
| * Writes remote function data to local function directories. | ||
| * Creates function.jsonc config and all source files for each function. | ||
| * Skips functions whose local files already match remote content. | ||
| */ | ||
| export async function writeFunctions( | ||
| functionsDir: string, | ||
| functions: FunctionInfo[], | ||
| ): Promise<WriteFunctionsResult> { | ||
| const written: string[] = []; | ||
| const skipped: string[] = []; | ||
|
|
||
| for (const fn of functions) { | ||
| const functionDir = join(functionsDir, fn.name); | ||
| const configPath = join(functionDir, "function.jsonc"); | ||
|
|
||
| // Check if all files already match remote content | ||
| if (await isFunctionUnchanged(functionDir, fn)) { | ||
| skipped.push(fn.name); | ||
| continue; | ||
| } | ||
|
|
||
| // Write function config | ||
| const config: Record<string, unknown> = { | ||
| name: fn.name, | ||
| entry: fn.entry, | ||
| }; | ||
| if (fn.automations.length > 0) { | ||
| config.automations = fn.automations; | ||
| } | ||
| await writeJsonFile(configPath, config); | ||
|
|
||
| // Write all source files | ||
| for (const file of fn.files) { | ||
| await writeFile(join(functionDir, file.path), file.content); | ||
| } | ||
|
|
||
| written.push(fn.name); | ||
| } | ||
|
|
||
| return { written, skipped }; | ||
| } | ||
|
|
||
| async function isFunctionUnchanged( | ||
kfirstri marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| functionDir: string, | ||
| fn: FunctionInfo, | ||
| ): Promise<boolean> { | ||
| if (!(await pathExists(functionDir))) { | ||
| return false; | ||
| } | ||
|
|
||
| // Compare function config (entry, automations) | ||
| const configPath = join(functionDir, "function.jsonc"); | ||
| try { | ||
| const localConfig = (await readJsonFile(configPath)) as Record< | ||
| string, | ||
| unknown | ||
| >; | ||
| if (localConfig.entry !== fn.entry) { | ||
| return false; | ||
| } | ||
| if (!isDeepStrictEqual(localConfig.automations ?? [], fn.automations)) { | ||
| return false; | ||
| } | ||
| } catch { | ||
| return false; | ||
| } | ||
|
|
||
| // Compare source files | ||
| for (const file of fn.files) { | ||
| const filePath = join(functionDir, file.path); | ||
| if (!(await pathExists(filePath))) { | ||
| return false; | ||
| } | ||
| try { | ||
| const localContent = await readTextFile(filePath); | ||
| if (localContent !== file.content) { | ||
| return false; | ||
| } | ||
| } catch { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
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.