Skip to content

Commit

Permalink
refactor: separate supabase functions
Browse files Browse the repository at this point in the history
  • Loading branch information
maneike committed Nov 25, 2024
1 parent 5e35685 commit 48d477d
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 94 deletions.
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.');
});
};
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.');
});
};
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 packages/core/installMachine/installSteps/supabase/install.ts
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('..');
};
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.');
});
};
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);
}
}
});
};
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);
}
});
};

0 comments on commit 48d477d

Please sign in to comment.