From f43a4dd8b5b93396dbf643b4da812a52b88d5e0f Mon Sep 17 00:00:00 2001 From: bowtiedswan Date: Tue, 30 Dec 2025 14:47:04 +0200 Subject: [PATCH] Refactor environment variable generation for workflows - Updated `generateEnvExample` to accept workflow nodes and extract action IDs. - Replaced `getAllEnvVars` with `getEnvVarsForActions` to fetch environment variables specific to used integrations. - Added `getEnvVarsForActions` function to retrieve environment variables based on action IDs from the plugin registry. --- .../workflows/[workflowId]/download/route.ts | 18 ++++++++---- plugins/index.ts | 1 + plugins/registry.ts | 28 +++++++++++++++++++ 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/app/api/workflows/[workflowId]/download/route.ts b/app/api/workflows/[workflowId]/download/route.ts index 0f2f7d860..fa1f563eb 100644 --- a/app/api/workflows/[workflowId]/download/route.ts +++ b/app/api/workflows/[workflowId]/download/route.ts @@ -7,7 +7,7 @@ import { db } from "@/lib/db"; import { workflows } from "@/lib/db/schema"; import { generateWorkflowModule } from "@/lib/workflow-codegen"; import type { WorkflowEdge, WorkflowNode } from "@/lib/workflow-store"; -import { getAllEnvVars, getDependenciesForActions } from "@/plugins"; +import { getDependenciesForActions, getEnvVarsForActions } from "@/plugins"; // Path to the Next.js boilerplate directory const BOILERPLATE_PATH = join(process.cwd(), "lib", "next-boilerplate"); @@ -154,9 +154,9 @@ function getIntegrationDependencies( } /** - * Generate .env.example content based on registered integrations + * Generate .env.example content based on workflow nodes */ -function generateEnvExample(): string { +function generateEnvExample(nodes: WorkflowNode[]): string { const lines = ["# Add your environment variables here"]; // Add system integration env vars @@ -164,8 +164,14 @@ function generateEnvExample(): string { lines.push("# For database integrations"); lines.push("DATABASE_URL=your_database_url"); - // Add plugin env vars from registry - const envVars = getAllEnvVars(); + // Extract action IDs from workflow nodes + const actionIds = nodes + .filter((node) => node.data.type === "action") + .map((node) => node.data.config?.actionType as string) + .filter(Boolean); + + // Get only env vars for used integrations + const envVars = getEnvVarsForActions(actionIds); const groupedByPrefix: Record< string, Array<{ name: string; description: string }> @@ -328,7 +334,7 @@ For more information, visit the [Workflow documentation](https://workflow.is). `; // Add .env.example file (dynamically generated from plugin registry) - allFiles[".env.example"] = generateEnvExample(); + allFiles[".env.example"] = generateEnvExample(workflow.nodes as WorkflowNode[]); return NextResponse.json({ success: true, diff --git a/plugins/index.ts b/plugins/index.ts index 495c6e339..1c6936c55 100644 --- a/plugins/index.ts +++ b/plugins/index.ts @@ -49,6 +49,7 @@ export { getAllDependencies, getAllEnvVars, getAllIntegrations, + getEnvVarsForActions, getCredentialMapping, getDependenciesForActions, getIntegration, diff --git a/plugins/registry.ts b/plugins/registry.ts index ef16bea60..0c1a4f9b3 100644 --- a/plugins/registry.ts +++ b/plugins/registry.ts @@ -433,6 +433,34 @@ export function getDependenciesForActions( return deps; } +/** + * Get environment variables for specific action IDs + */ +export function getEnvVarsForActions( + actionIds: string[] +): Array<{ name: string; description: string }> { + const envVars: Array<{ name: string; description: string }> = []; + const integrations = new Set(); + + // Find which integrations are used + for (const actionId of actionIds) { + const action = findActionById(actionId); + if (action) { + integrations.add(action.integration); + } + } + + // Get env vars for those integrations only + for (const integrationType of integrations) { + const plugin = integrationRegistry.get(integrationType); + if (plugin) { + envVars.push(...getPluginEnvVars(plugin)); + } + } + + return envVars; +} + /** * Get environment variables for a single plugin (from formFields) */