|
1 | 1 | import { exec, execSync } from 'child_process';
|
2 |
| -import inquirer from 'inquirer'; |
3 | 2 | import { promisify } from 'util';
|
4 | 3 | import chalk from 'chalk';
|
5 |
| -import { continueOnAnyKeypress } from '../../../utils/continueOnKeypress'; |
| 4 | +import boxen from 'boxen'; |
6 | 5 | import { getSupabaseKeys, parseProjectsList } from './utils';
|
7 |
| -import { logWithColoredPrefix } from '../../../utils/logWithColoredPrefix'; |
| 6 | +import { logger } from '../../../utils/logWithColoredPrefix'; |
| 7 | +import { getVercelTokenFromAuthFile } from '../../../utils/getVercelTokenFromAuthFile'; |
| 8 | +import { getProjectIdFromVercelConfig } from '../../../utils/getProjectIdFromVercelConfig'; |
8 | 9 |
|
9 | 10 | const execAsync = promisify(exec);
|
| 11 | +const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); |
10 | 12 |
|
11 | 13 | export const connectSupabaseProject = async (projectName: string, currentDir: string) => {
|
12 | 14 | try {
|
13 |
| - logWithColoredPrefix('supabase', 'Getting information about newly created project...'); |
14 |
| - const { stdout: projectsList } = await execAsync('npx supabase projects list'); |
15 |
| - const projects = parseProjectsList(projectsList); |
16 |
| - const newProject = projects.find((project) => project.name === projectName); |
17 |
| - |
18 |
| - if (!newProject || !newProject.refId) { |
19 |
| - throw new Error( |
20 |
| - `Could not find Supabase project "${projectName}". Please ensure the project exists and you have the correct permissions.`, |
21 |
| - ); |
22 |
| - } |
| 15 | + // Get project information |
| 16 | + const newProject = await logger.withSpinner('supabase', 'Getting project information...', async (spinner) => { |
| 17 | + const { stdout: projectsList } = await execAsync('npx supabase projects list'); |
| 18 | + const projects = parseProjectsList(projectsList); |
| 19 | + const project = projects.find((p) => p.name === projectName); |
| 20 | + |
| 21 | + if (!project || !project.refId) { |
| 22 | + spinner.fail('Project not found'); |
| 23 | + throw new Error( |
| 24 | + `Could not find Supabase project "${projectName}". Please ensure the project exists and you have the correct permissions.`, |
| 25 | + ); |
| 26 | + } |
| 27 | + |
| 28 | + spinner.succeed('Project found!'); |
| 29 | + return project; |
| 30 | + }); |
| 31 | + |
| 32 | + // Get API keys |
| 33 | + const { anonKey, serviceRoleKey } = await logger.withSpinner( |
| 34 | + 'supabase', |
| 35 | + 'Getting project API keys...', |
| 36 | + async (spinner) => { |
| 37 | + const { stdout: projectAPIKeys } = await execAsync( |
| 38 | + `npx supabase projects api-keys --project-ref ${newProject.refId}`, |
| 39 | + ); |
| 40 | + |
| 41 | + const keys = getSupabaseKeys(projectAPIKeys); |
| 42 | + if (!keys.anonKey || !keys.serviceRoleKey) { |
| 43 | + spinner.fail('Failed to retrieve API keys'); |
| 44 | + throw new Error('Failed to retrieve Supabase API keys. Please check your project configuration.'); |
| 45 | + } |
23 | 46 |
|
24 |
| - logWithColoredPrefix('supabase', 'Getting project keys...'); |
25 |
| - const { stdout: projectAPIKeys } = await execAsync( |
26 |
| - `npx supabase projects api-keys --project-ref ${newProject.refId}`, |
| 47 | + spinner.succeed('API keys retrieved!'); |
| 48 | + return keys; |
| 49 | + }, |
27 | 50 | );
|
28 | 51 |
|
29 |
| - const { anonKey, serviceRoleKey } = getSupabaseKeys(projectAPIKeys); |
| 52 | + // Link project |
| 53 | + logger.log('supabase', 'Linking project...'); |
| 54 | + execSync(`npx supabase link --project-ref ${newProject.refId}`, { |
| 55 | + stdio: 'inherit', |
| 56 | + }); |
30 | 57 |
|
31 |
| - if (!anonKey || !serviceRoleKey) { |
32 |
| - throw new Error('Failed to retrieve Supabase API keys. Please check your project configuration.'); |
33 |
| - } |
| 58 | + // Display integration instructions |
| 59 | + console.log( |
| 60 | + boxen( |
| 61 | + chalk.bold('Supabase Integration Setup\n\n') + |
| 62 | + chalk.hex('#3ABC82')('1.') + |
| 63 | + ' You will be redirected to your project dashboard\n' + |
| 64 | + chalk.hex('#3ABC82')('2.') + |
| 65 | + ' Connect Vercel: "Add new project connection"\n' + |
| 66 | + chalk.hex('#3ABC82')('3.') + |
| 67 | + ' (Optional) Connect GitHub: "Add new project connection"\n\n' + |
| 68 | + chalk.dim('Tip: Keep this terminal open to track the integration status'), |
| 69 | + { |
| 70 | + padding: 1, |
| 71 | + margin: 1, |
| 72 | + borderStyle: 'round', |
| 73 | + borderColor: '#3ABC82', |
| 74 | + }, |
| 75 | + ), |
| 76 | + ); |
34 | 77 |
|
35 |
| - logWithColoredPrefix('supabase', 'Linking project...'); |
36 |
| - execSync(`npx supabase link --project-ref ${newProject.refId}`, { stdio: 'inherit' }); |
| 78 | + // Countdown and open dashboard |
| 79 | + const spinner = logger.createSpinner('supabase', 'Preparing to open dashboard'); |
| 80 | + spinner.start(); |
37 | 81 |
|
38 |
| - logWithColoredPrefix('supabase', [ |
39 |
| - chalk.bold('=== Instructions for integration with GitHub and Vercel ==='), |
40 |
| - '\n1. You will be redirected to your project dashboard', |
41 |
| - '\n2. Find the "GitHub" section and click "Connect".', |
42 |
| - '\n - Follow the prompts to connect with your GitHub repository.', |
43 |
| - '\n3. Then, find the "Vercel" section and click "Connect".', |
44 |
| - '\n - Follow the prompts to connect with your Vercel project.', |
45 |
| - chalk.italic('\nNOTE: These steps require manual configuration in the Supabase interface.'), |
46 |
| - ]); |
| 82 | + for (let i = 3; i > 0; i--) { |
| 83 | + spinner.text = `Opening dashboard in ${chalk.hex('#3ABC82')(i)}...`; |
| 84 | + await delay(1000); |
| 85 | + } |
47 | 86 |
|
48 |
| - await continueOnAnyKeypress('When you are ready to be redirected to the Supabase page press any key'); |
| 87 | + spinner.text = 'Opening dashboard in your browser...'; |
49 | 88 | await execAsync(`open https://supabase.com/dashboard/project/${newProject.refId}/settings/integrations`);
|
| 89 | + spinner.succeed('Dashboard opened.'); |
50 | 90 |
|
51 |
| - const { isIntegrationReady } = await inquirer.prompt([ |
52 |
| - { |
53 |
| - type: 'confirm', |
54 |
| - name: 'isIntegrationReady', |
55 |
| - message: 'Have you completed the GitHub and Vercel integration setup?', |
56 |
| - default: false, |
57 |
| - }, |
58 |
| - ]); |
| 91 | + // Check Vercel integration |
| 92 | + await logger.withSpinner('vercel', 'Checking integration...', async (spinner) => { |
| 93 | + const token = await getVercelTokenFromAuthFile(); |
| 94 | + const vercelProjectId = await getProjectIdFromVercelConfig(); |
59 | 95 |
|
60 |
| - if (!isIntegrationReady) { |
61 |
| - logWithColoredPrefix( |
62 |
| - 'supabase', |
63 |
| - `You can access your project dashboard at: https://supabase.com/dashboard/project/${newProject.refId}/settings/integrations`, |
64 |
| - ), |
65 |
| - process.exit(1); |
66 |
| - } |
| 96 | + let attempts = 0; |
| 97 | + const maxAttempts = 30; |
| 98 | + const interval = 5000; |
| 99 | + |
| 100 | + while (attempts < maxAttempts) { |
| 101 | + try { |
| 102 | + const response = await fetch(`https://api.vercel.com/v9/projects/${vercelProjectId}/env`, { |
| 103 | + headers: { Authorization: `Bearer ${token}` }, |
| 104 | + method: 'get', |
| 105 | + }); |
| 106 | + |
| 107 | + const envVarsSet = await response.json(); |
| 108 | + const supabaseUrl = envVarsSet.envs.find((env: { key: string }) => env.key === 'SUPABASE_URL')?.value; |
| 109 | + |
| 110 | + if (supabaseUrl) { |
| 111 | + spinner.succeed('Integration complete!'); |
| 112 | + return true; |
| 113 | + } |
| 114 | + |
| 115 | + attempts++; |
| 116 | + spinner.text = `Checking integration status ${chalk.hex('#3ABC82')(`[${attempts}/${maxAttempts}]`)}`; |
| 117 | + await delay(interval); |
| 118 | + } catch (error) { |
| 119 | + spinner.fail('Failed to check Vercel integration status'); |
| 120 | + throw error; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + // Timeout reached |
| 125 | + spinner.warn('Integration check timed out'); |
| 126 | + console.log( |
| 127 | + boxen( |
| 128 | + chalk.yellow('Integration Status Unknown\n\n') + |
| 129 | + 'You can manually verify the integration at:\n' + |
| 130 | + chalk.hex('#3ABC82')(`https://supabase.com/dashboard/project/${newProject.refId}/settings/integrations`), |
| 131 | + { |
| 132 | + padding: 1, |
| 133 | + margin: 1, |
| 134 | + borderStyle: 'round', |
| 135 | + borderColor: 'yellow', |
| 136 | + }, |
| 137 | + ), |
| 138 | + ); |
| 139 | + |
| 140 | + return false; |
| 141 | + }); |
67 | 142 | } catch (error) {
|
68 |
| - const errorMessage = error instanceof Error ? error.message : 'An unknown error occurred'; |
69 |
| - console.error('Error connecting Supabase project:', errorMessage); |
| 143 | + logger.log('error', error instanceof Error ? error.message : 'An unknown error occurred'); |
70 | 144 | throw error;
|
71 | 145 | }
|
72 | 146 | };
|
0 commit comments