|
| 1 | +import chalk from 'chalk'; |
| 2 | +import open from 'open'; |
| 3 | +import type Client from '../../util/client'; |
| 4 | +import getScope from '../../util/get-scope'; |
| 5 | +import type { Configuration } from './types'; |
| 6 | +import { fetchMarketplaceIntegrations } from './client'; |
| 7 | + |
| 8 | +export async function openIntegration(client: Client, args: string[]) { |
| 9 | + if (args.length > 1) { |
| 10 | + client.output.error('Cannot open more than one dashboard at a time'); |
| 11 | + return 1; |
| 12 | + } |
| 13 | + |
| 14 | + const integrationSlug = args[0]; |
| 15 | + |
| 16 | + if (!integrationSlug) { |
| 17 | + client.output.error('You must pass an integration slug'); |
| 18 | + return 1; |
| 19 | + } |
| 20 | + |
| 21 | + const { team } = await getScope(client); |
| 22 | + |
| 23 | + if (!team) { |
| 24 | + client.output.error('Team not found'); |
| 25 | + return 1; |
| 26 | + } |
| 27 | + |
| 28 | + let configuration: Configuration | undefined; |
| 29 | + |
| 30 | + try { |
| 31 | + configuration = await getFirstConfiguration(client, integrationSlug); |
| 32 | + } catch (error) { |
| 33 | + client.output.error( |
| 34 | + `Failed to fetch configuration for ${chalk.bold(`"${integrationSlug}"`)}: ${(error as Error).message}` |
| 35 | + ); |
| 36 | + return 1; |
| 37 | + } |
| 38 | + |
| 39 | + if (!configuration) { |
| 40 | + client.output.error( |
| 41 | + `No configuration found for ${chalk.bold(`"${integrationSlug}"`)}.` |
| 42 | + ); |
| 43 | + return 1; |
| 44 | + } |
| 45 | + |
| 46 | + client.output.print( |
| 47 | + `Opening the ${chalk.bold(integrationSlug)} dashboard...` |
| 48 | + ); |
| 49 | + |
| 50 | + const url = new URL('/api/marketplace/sso', 'https://vercel.com'); |
| 51 | + url.searchParams.set('teamId', team.id); |
| 52 | + url.searchParams.set('integrationConfigurationId', configuration.id); |
| 53 | + open(url.href); |
| 54 | + |
| 55 | + return 0; |
| 56 | +} |
| 57 | + |
| 58 | +async function getFirstConfiguration(client: Client, integrationSlug: string) { |
| 59 | + const configurations = await fetchMarketplaceIntegrations( |
| 60 | + client, |
| 61 | + integrationSlug |
| 62 | + ); |
| 63 | + return configurations.length > 0 ? configurations[0] : undefined; |
| 64 | +} |
0 commit comments