-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: separate supabase functions
- Loading branch information
Showing
7 changed files
with
118 additions
and
94 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
packages/core/installMachine/installSteps/supabase/addTemplateFiles.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.'); | ||
}); | ||
}; |
16 changes: 16 additions & 0 deletions
16
packages/core/installMachine/installSteps/supabase/createEnvFile.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.'); | ||
}); | ||
}; |
22 changes: 22 additions & 0 deletions
22
packages/core/installMachine/installSteps/supabase/initializeSupabaseProject.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
}); | ||
}; |
106 changes: 12 additions & 94 deletions
106
packages/core/installMachine/installSteps/supabase/install.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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('..'); | ||
}; |
9 changes: 9 additions & 0 deletions
9
packages/core/installMachine/installSteps/supabase/installDependencies.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.'); | ||
}); | ||
}; |
20 changes: 20 additions & 0 deletions
20
packages/core/installMachine/installSteps/supabase/loginToSupabase.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
}); | ||
}; |
16 changes: 16 additions & 0 deletions
16
packages/core/installMachine/installSteps/supabase/startLocalDatabase.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
}); | ||
}; |