From 48d477dee6d08b948e0c93ef4339bf1a6f98b42d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Che=C5=82miniak?= Date: Mon, 25 Nov 2024 13:04:08 +0100 Subject: [PATCH] refactor: separate supabase functions --- .../installSteps/supabase/addTemplateFiles.ts | 23 ++++ .../installSteps/supabase/createEnvFile.ts | 16 +++ .../supabase/initializeSupabaseProject.ts | 22 ++++ .../installSteps/supabase/install.ts | 106 ++---------------- .../supabase/installDependencies.ts | 9 ++ .../installSteps/supabase/loginToSupabase.ts | 20 ++++ .../supabase/startLocalDatabase.ts | 16 +++ 7 files changed, 118 insertions(+), 94 deletions(-) create mode 100644 packages/core/installMachine/installSteps/supabase/addTemplateFiles.ts create mode 100644 packages/core/installMachine/installSteps/supabase/createEnvFile.ts create mode 100644 packages/core/installMachine/installSteps/supabase/initializeSupabaseProject.ts create mode 100644 packages/core/installMachine/installSteps/supabase/installDependencies.ts create mode 100644 packages/core/installMachine/installSteps/supabase/loginToSupabase.ts create mode 100644 packages/core/installMachine/installSteps/supabase/startLocalDatabase.ts diff --git a/packages/core/installMachine/installSteps/supabase/addTemplateFiles.ts b/packages/core/installMachine/installSteps/supabase/addTemplateFiles.ts new file mode 100644 index 0000000..a7302db --- /dev/null +++ b/packages/core/installMachine/installSteps/supabase/addTemplateFiles.ts @@ -0,0 +1,23 @@ +import fs from 'fs'; +import path from 'path'; +import { supabaseFiles } from '../../../templates/supabase/installConfig'; +import { templateGenerator } from '../../../utils/generator/generator'; +import { getTemplateDirectory } from '../../../utils/getTemplateDirectory'; +import { logger } from '../../../utils/logger'; + +export const addTemplateFiles = async (destinationDirectory: string) => { + await logger.withSpinner('supabase', 'Adding files from template...', async (spinner) => { + const templateDirectory = getTemplateDirectory(`/templates/supabase/files/`); + templateGenerator(supabaseFiles, templateDirectory, destinationDirectory); + + // Add "supabase/**" to pnpm-workspace.yaml + const workspacePath = path.join(destinationDirectory, 'pnpm-workspace.yaml'); + const addSupabaseToWorkspace = ` - "supabase/**"`; + const fileContents = fs.readFileSync(workspacePath, 'utf-8'); + if (!fileContents.includes(addSupabaseToWorkspace)) { + fs.appendFileSync(workspacePath, `${addSupabaseToWorkspace}\n`); + } + + spinner.succeed('Files added.'); + }); +}; diff --git a/packages/core/installMachine/installSteps/supabase/createEnvFile.ts b/packages/core/installMachine/installSteps/supabase/createEnvFile.ts new file mode 100644 index 0000000..b910baf --- /dev/null +++ b/packages/core/installMachine/installSteps/supabase/createEnvFile.ts @@ -0,0 +1,16 @@ +import fs from 'fs'; +import { execAsync } from '../../../utils/execAsync'; +import { logger } from '../../../utils/logger'; + +export const createEnvFile = async () => { + await logger.withSpinner('supabase', 'Writing local variables to .env file...', async (spinner) => { + const output = await execAsync('npx supabase status --output json'); + const jsonData = JSON.parse(output.stdout); + const envData = Object.entries(jsonData) + .map(([key, value]) => `${key}=${value}`) + .join('\n'); + + fs.writeFileSync('.env', envData, 'utf8'); + spinner.succeed('Local variables written to .env file.'); + }); +}; diff --git a/packages/core/installMachine/installSteps/supabase/initializeSupabaseProject.ts b/packages/core/installMachine/installSteps/supabase/initializeSupabaseProject.ts new file mode 100644 index 0000000..376e137 --- /dev/null +++ b/packages/core/installMachine/installSteps/supabase/initializeSupabaseProject.ts @@ -0,0 +1,22 @@ +import { execAsync } from '../../../utils/execAsync'; +import { logger } from '../../../utils/logger'; + +export const initializeSupabaseProject = async () => { + await logger.withSpinner('supabase', 'Initializing project...', async (spinner) => { + try { + await execAsync(`npx supabase init`); + spinner.succeed('Project initialized.'); + } catch (error: any) { + const errorMessage = error.stderr; + if (errorMessage.includes('file exists')) { + spinner.succeed('Configuration file already exists.'); + } else { + spinner.fail('Failed to initialize project.'); + console.error( + 'Please review the error message below, follow the initialization instructions, and try running "stplr" again.', + ); + process.exit(1); + } + } + }); +}; diff --git a/packages/core/installMachine/installSteps/supabase/install.ts b/packages/core/installMachine/installSteps/supabase/install.ts index cd0282f..496a310 100644 --- a/packages/core/installMachine/installSteps/supabase/install.ts +++ b/packages/core/installMachine/installSteps/supabase/install.ts @@ -1,102 +1,20 @@ -import fs from 'fs'; -import path from 'path'; -import chalk from 'chalk'; -import { supabaseFiles } from '../../../templates/supabase/installConfig'; -import { templateGenerator } from '../../../utils/generator/generator'; -import { getTemplateDirectory } from '../../../utils/getTemplateDirectory'; -import { logger } from '../../../utils/logger'; -import { execAsync } from '../../../utils/execAsync'; - -const supabaseLogin = async () => { - await logger.withSpinner('supabase', 'Logging in...', async (spinner) => { - try { - await execAsync('npx supabase projects list'); - spinner.succeed('Already logged in.'); - } catch (error) { - try { - await execAsync('npx supabase login'); - spinner.succeed('Logged in successfully.'); - } catch { - spinner.fail('Failed to log in to Supabase.'); - console.error('Please log in manually with "supabase login" and re-run "stplr".'); - process.exit(1); - } - } - }); -}; - -const initializeSupabaseProject = async () => { - await logger.withSpinner('supabase', 'Initializing project...', async (spinner) => { - try { - await execAsync(`npx supabase init`); - spinner.succeed('Project initialized.'); - } catch (error: any) { - const errorMessage = error.stderr; - if (errorMessage.includes('file exists')) { - spinner.succeed('Configuration file already exists.'); - } else { - spinner.fail('Failed to initialize project.'); - console.error( - 'Please review the error message below, follow the initialization instructions, and try running "stplr" again.', - ); - process.exit(1); - } - } - }); -}; +import { loginToSupabase } from './loginToSupabase'; +import { initializeSupabaseProject } from './initializeSupabaseProject'; +import { addTemplateFiles } from './addTemplateFiles'; +import { installDependencies } from './installDependencies'; +import { startLocalDatabase } from './startLocalDatabase'; +import { createEnvFile } from './createEnvFile'; export const installSupabase = async (destinationDirectory: string) => { - try { - await supabaseLogin(); - await initializeSupabaseProject(); - } catch (error) { - console.error('Failed to init project.', `\nError: ${error}`); - process.exit(1); - } - - await logger.withSpinner('supabase', 'Adding files from template...', async (spinner) => { - const templateDirectory = getTemplateDirectory(`/templates/supabase/files/`); - templateGenerator(supabaseFiles, templateDirectory, destinationDirectory); - - // Add "supabase/**" to pnpm-workspace.yaml - const workspacePath = path.join(destinationDirectory, 'pnpm-workspace.yaml'); - const addSupabaseToWorkspace = ` - "supabase/**"`; - const fileContents = fs.readFileSync(workspacePath, 'utf-8'); - if (!fileContents.includes(addSupabaseToWorkspace)) { - fs.appendFileSync(workspacePath, `${addSupabaseToWorkspace}\n`); - } - - spinner.succeed('Files added.'); - }); + await loginToSupabase(); + await initializeSupabaseProject(); + await addTemplateFiles(destinationDirectory); process.chdir('supabase'); - await logger.withSpinner('supabase', 'Installing dependencies...', async (spinner) => { - await execAsync('pnpm i --reporter silent'); - spinner.succeed('Dependencies installed.'); - }); - - await logger.withSpinner('supabase', 'Starting local database...', async (spinner) => { - try { - await execAsync('npx supabase start'); - spinner.succeed('Local database started.'); - } catch (error) { - spinner.fail(`Failed to start local database. Is your ${chalk.hex('#0db7ed')('Docker')} daemon running?`); - console.error(`\n${error}`); - process.exit(1); - } - }); - - await logger.withSpinner('supabase', 'Writing local variables to .env file...', async (spinner) => { - const output = await execAsync('npx supabase status --output json'); - const jsonData = JSON.parse(output.stdout); - const envData = Object.entries(jsonData) - .map(([key, value]) => `${key}=${value}`) - .join('\n'); - - fs.writeFileSync('.env', envData, 'utf8'); - spinner.succeed('Local variables written to .env file.'); - }); + await installDependencies(); + await startLocalDatabase(); + await createEnvFile(); process.chdir('..'); }; diff --git a/packages/core/installMachine/installSteps/supabase/installDependencies.ts b/packages/core/installMachine/installSteps/supabase/installDependencies.ts new file mode 100644 index 0000000..c563527 --- /dev/null +++ b/packages/core/installMachine/installSteps/supabase/installDependencies.ts @@ -0,0 +1,9 @@ +import { execAsync } from '../../../utils/execAsync'; +import { logger } from '../../../utils/logger'; + +export const installDependencies = async () => { + await logger.withSpinner('supabase', 'Installing dependencies...', async (spinner) => { + await execAsync('pnpm i --reporter silent'); + spinner.succeed('Dependencies installed.'); + }); +}; diff --git a/packages/core/installMachine/installSteps/supabase/loginToSupabase.ts b/packages/core/installMachine/installSteps/supabase/loginToSupabase.ts new file mode 100644 index 0000000..5ad4a9d --- /dev/null +++ b/packages/core/installMachine/installSteps/supabase/loginToSupabase.ts @@ -0,0 +1,20 @@ +import { execAsync } from "../../../utils/execAsync"; +import { logger } from "../../../utils/logger"; + +export const loginToSupabase = async () => { + await logger.withSpinner('supabase', 'Logging in...', async (spinner) => { + try { + await execAsync('npx supabase projects list'); + spinner.succeed('Already logged in.'); + } catch (error) { + try { + await execAsync('npx supabase login'); + spinner.succeed('Logged in successfully.'); + } catch { + spinner.fail('Failed to log in to Supabase.'); + console.error('Please log in manually with "supabase login" and re-run "stplr".'); + process.exit(1); + } + } + }); +}; diff --git a/packages/core/installMachine/installSteps/supabase/startLocalDatabase.ts b/packages/core/installMachine/installSteps/supabase/startLocalDatabase.ts new file mode 100644 index 0000000..4e8ff70 --- /dev/null +++ b/packages/core/installMachine/installSteps/supabase/startLocalDatabase.ts @@ -0,0 +1,16 @@ +import chalk from 'chalk'; +import { execAsync } from '../../../utils/execAsync'; +import { logger } from '../../../utils/logger'; + +export const startLocalDatabase = async () => { + await logger.withSpinner('supabase', 'Starting local database...', async (spinner) => { + try { + await execAsync('npx supabase start'); + spinner.succeed('Local database started.'); + } catch (error) { + spinner.fail(`Failed to start local database. Is your ${chalk.hex('#0db7ed')('Docker')} daemon running?`); + console.error(`\n${error}`); + process.exit(1); + } + }); +};