|
| 1 | +import { confirm, isCancel, log } from "@clack/prompts"; |
| 2 | +import chalk from "chalk"; |
| 3 | +import { Command } from "commander"; |
| 4 | +import type { CLIContext } from "@/cli/types.js"; |
| 5 | +import { runCommand, runTask } from "@/cli/utils/index.js"; |
| 6 | +import type { RunCommandResult } from "@/cli/utils/runCommand.js"; |
| 7 | +import { readProjectConfig } from "@/core/index.js"; |
| 8 | +import { |
| 9 | + type ConnectorOAuthStatus, |
| 10 | + type ConnectorSyncResult, |
| 11 | + type IntegrationType, |
| 12 | + pushConnectors, |
| 13 | + runOAuthFlow, |
| 14 | +} from "@/core/resources/connector/index.js"; |
| 15 | + |
| 16 | +type PendingOAuthResult = ConnectorSyncResult & { |
| 17 | + redirectUrl: string; |
| 18 | + connectionId: string; |
| 19 | +}; |
| 20 | + |
| 21 | +function isPendingOAuth(r: ConnectorSyncResult): r is PendingOAuthResult { |
| 22 | + return r.action === "needs_oauth" && !!r.redirectUrl && !!r.connectionId; |
| 23 | +} |
| 24 | + |
| 25 | +function printSummary( |
| 26 | + results: ConnectorSyncResult[], |
| 27 | + oauthOutcomes: Map<IntegrationType, ConnectorOAuthStatus> |
| 28 | +): void { |
| 29 | + const synced: IntegrationType[] = []; |
| 30 | + const added: IntegrationType[] = []; |
| 31 | + const removed: IntegrationType[] = []; |
| 32 | + const failed: { type: IntegrationType; error?: string }[] = []; |
| 33 | + |
| 34 | + for (const r of results) { |
| 35 | + const oauthStatus = oauthOutcomes.get(r.type); |
| 36 | + |
| 37 | + if (r.action === "synced") { |
| 38 | + synced.push(r.type); |
| 39 | + } else if (r.action === "removed") { |
| 40 | + removed.push(r.type); |
| 41 | + } else if (r.action === "error") { |
| 42 | + failed.push({ type: r.type, error: r.error }); |
| 43 | + } else if (r.action === "needs_oauth") { |
| 44 | + if (oauthStatus === "ACTIVE") { |
| 45 | + added.push(r.type); |
| 46 | + } else if (oauthStatus === "PENDING") { |
| 47 | + failed.push({ type: r.type, error: "authorization timed out" }); |
| 48 | + } else if (oauthStatus === "FAILED") { |
| 49 | + failed.push({ type: r.type, error: "authorization failed" }); |
| 50 | + } else { |
| 51 | + failed.push({ type: r.type, error: "needs authorization" }); |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + log.info(""); |
| 57 | + log.info(chalk.bold("Summary:")); |
| 58 | + |
| 59 | + if (synced.length > 0) { |
| 60 | + log.info(chalk.green(` Synced: ${synced.join(", ")}`)); |
| 61 | + } |
| 62 | + if (added.length > 0) { |
| 63 | + log.info(chalk.green(` Added: ${added.join(", ")}`)); |
| 64 | + } |
| 65 | + if (removed.length > 0) { |
| 66 | + log.info(chalk.dim(` Removed: ${removed.join(", ")}`)); |
| 67 | + } |
| 68 | + for (const r of failed) { |
| 69 | + log.info(chalk.red(` Failed: ${r.type}${r.error ? ` - ${r.error}` : ""}`)); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +async function pushConnectorsAction(): Promise<RunCommandResult> { |
| 74 | + const { connectors } = await readProjectConfig(); |
| 75 | + |
| 76 | + if (connectors.length === 0) { |
| 77 | + log.info( |
| 78 | + "No local connectors found - checking for remote connectors to remove" |
| 79 | + ); |
| 80 | + } else { |
| 81 | + const connectorNames = connectors.map((c) => c.type).join(", "); |
| 82 | + log.info( |
| 83 | + `Found ${connectors.length} connectors to push: ${connectorNames}` |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + const { results } = await runTask( |
| 88 | + "Pushing connectors to Base44", |
| 89 | + async () => { |
| 90 | + return await pushConnectors(connectors); |
| 91 | + }, |
| 92 | + { |
| 93 | + successMessage: "Connectors pushed", |
| 94 | + errorMessage: "Failed to push connectors", |
| 95 | + } |
| 96 | + ); |
| 97 | + |
| 98 | + const oauthOutcomes = new Map<IntegrationType, ConnectorOAuthStatus>(); |
| 99 | + const needsOAuth = results.filter(isPendingOAuth); |
| 100 | + let outroMessage = "Connectors pushed to Base44"; |
| 101 | + |
| 102 | + if (needsOAuth.length > 0) { |
| 103 | + log.info(""); |
| 104 | + log.info( |
| 105 | + chalk.yellow( |
| 106 | + `${needsOAuth.length} connector(s) require authorization in your browser:` |
| 107 | + ) |
| 108 | + ); |
| 109 | + for (const connector of needsOAuth) { |
| 110 | + log.info(` ${connector.type}: ${chalk.dim(connector.redirectUrl)}`); |
| 111 | + } |
| 112 | + |
| 113 | + const pending = needsOAuth.map((c) => c.type).join(", "); |
| 114 | + |
| 115 | + if (process.env.CI) { |
| 116 | + outroMessage = `Skipped OAuth in CI. Pending: ${pending}. Run 'base44 connectors push' locally to authorize.`; |
| 117 | + } else { |
| 118 | + const shouldAuth = await confirm({ |
| 119 | + message: "Open browser to authorize now?", |
| 120 | + }); |
| 121 | + |
| 122 | + if (isCancel(shouldAuth) || !shouldAuth) { |
| 123 | + outroMessage = `Authorization skipped. Pending: ${pending}. Run 'base44 connectors push' again to complete.`; |
| 124 | + } else { |
| 125 | + for (const connector of needsOAuth) { |
| 126 | + log.info(`\nOpening browser for ${connector.type}...`); |
| 127 | + |
| 128 | + const oauthResult = await runTask( |
| 129 | + `Waiting for ${connector.type} authorization...`, |
| 130 | + async () => { |
| 131 | + return await runOAuthFlow({ |
| 132 | + type: connector.type, |
| 133 | + redirectUrl: connector.redirectUrl, |
| 134 | + connectionId: connector.connectionId, |
| 135 | + }); |
| 136 | + }, |
| 137 | + { |
| 138 | + successMessage: `${connector.type} authorization complete`, |
| 139 | + errorMessage: `${connector.type} authorization failed`, |
| 140 | + } |
| 141 | + ); |
| 142 | + |
| 143 | + oauthOutcomes.set(connector.type, oauthResult.status); |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + printSummary(results, oauthOutcomes); |
| 150 | + return { outroMessage }; |
| 151 | +} |
| 152 | + |
| 153 | +export function getConnectorsPushCommand(context: CLIContext): Command { |
| 154 | + return new Command("push") |
| 155 | + .description( |
| 156 | + "Push local connectors to Base44 (syncs scopes and removes unlisted)" |
| 157 | + ) |
| 158 | + .action(async () => { |
| 159 | + await runCommand(pushConnectorsAction, { requireAuth: true }, context); |
| 160 | + }); |
| 161 | +} |
0 commit comments