Skip to content

Commit

Permalink
Improve setup script
Browse files Browse the repository at this point in the history
  • Loading branch information
leerob committed Sep 18, 2024
1 parent a2d9edd commit 2ae775d
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 26 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
# Next.js SaaS Starter

> [!IMPORTANT]
> This repo is a work-in-progress. Please open an issue if you see something wrong!
This is a starter template for building a SaaS application using **Next.js** with support for authentication, Stripe integration for payments, and a dashboard for logged-in users.

**Demo: [https://next-saas-start.vercel.app/](https://next-saas-start.vercel.app/)**

<details>
<summary>Wait, why make this?</summary>
<summary>Why did I make this?</summary>

In 2020, I made a course called "React 2025" which showed how to build a SaaS application with Next.js, Stripe, and other tools.

Expand Down Expand Up @@ -77,6 +74,7 @@ Finally, run the Next.js development server:
```bash
pnpm dev
```

Open [http://localhost:3000](http://localhost:3000) in your browser to see the app in action.

Optionally, you can listen for Stripe webhooks locally through their CLI to handle subscription change events:
Expand Down
15 changes: 0 additions & 15 deletions docker-compose.yml

This file was deleted.

79 changes: 72 additions & 7 deletions lib/db/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { promisify } from 'node:util';
import readline from 'node:readline';
import crypto from 'node:crypto';
import path from 'node:path';
import os from 'node:os';

const execAsync = promisify(exec);

Expand Down Expand Up @@ -77,11 +78,73 @@ async function checkStripeCLI() {
}

async function getPostgresURL(): Promise<string> {
console.log('Step 2: Getting Postgres URL');
console.log(
'You can find Postgres databases at: https://vercel.com/marketplace?category=databases'
console.log('Step 2: Setting up Postgres');
const dbChoice = await question(
'Do you want to use a local Postgres instance with Docker (L) or a remote Postgres instance (R)? (L/R): '
);
return await question('Enter your POSTGRES_URL: ');

if (dbChoice.toLowerCase() === 'l') {
console.log('Setting up local Postgres instance with Docker...');
await setupLocalPostgres();
return 'postgres://postgres:postgres@localhost:54322/postgres';
} else {
console.log(
'You can find Postgres databases at: https://vercel.com/marketplace?category=databases'
);
return await question('Enter your POSTGRES_URL: ');
}
}

async function setupLocalPostgres() {
console.log('Checking if Docker is installed...');
try {
await execAsync('docker --version');
console.log('Docker is installed.');
} catch (error) {
console.error(
'Docker is not installed. Please install Docker and try again.'
);
console.log(
'To install Docker, visit: https://docs.docker.com/get-docker/'
);
process.exit(1);
}

console.log('Creating docker-compose.yml file...');
const dockerComposeContent = `
services:
postgres:
image: postgres:16.4-alpine
container_name: next_saas_starter_postgres
environment:
POSTGRES_DB: postgres
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
ports:
- "54322:5432"
volumes:
- postgres_data:/var/lib/postgresql/data
volumes:
postgres_data:
`;

await fs.writeFile(
path.join(process.cwd(), 'docker-compose.yml'),
dockerComposeContent
);
console.log('docker-compose.yml file created.');

console.log('Starting Docker container with `docker compose up -d`...');
try {
await execAsync('docker compose up -d');
console.log('Docker container started successfully.');
} catch (error) {
console.error(
'Failed to start Docker container. Please check your Docker installation and try again.'
);
process.exit(1);
}
}

async function getStripeSecretKey(): Promise<string> {
Expand All @@ -106,9 +169,11 @@ async function createStripeWebhook(): Promise<string> {
console.error(
'Failed to create Stripe webhook. Check your Stripe CLI installation and permissions.'
);
console.log(
'Note: On Windows, you may need to run this script as an administrator.'
);
if (os.platform() === 'win32') {
console.log(
'Note: On Windows, you may need to run this script as an administrator.'
);
}
throw error;
}
}
Expand Down

0 comments on commit 2ae775d

Please sign in to comment.