Skip to content

Commit af70905

Browse files
Improve Turso setup with group selection
1 parent 07cc955 commit af70905

File tree

4 files changed

+91
-15
lines changed

4 files changed

+91
-15
lines changed

.changeset/crazy-bananas-know.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"create-better-t-stack": minor
3+
---
4+
5+
Fix auth template setup, todo example, turso setup with setup selection

apps/cli/src/constants.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,14 @@ export const DEFAULT_CONFIG: ProjectConfig = {
1818
};
1919

2020
export const dependencyVersionMap = {
21-
// Authentication
2221
"better-auth": "^1.2.4",
2322

24-
// Database - Drizzle
2523
"drizzle-orm": "^0.38.4",
2624
"drizzle-kit": "^0.30.5",
2725

28-
// Database - SQLite/PostgreSQL
2926
"@libsql/client": "^0.14.0",
3027
postgres: "^3.4.5",
3128

32-
// Database - Prisma
3329
"@prisma/client": "^5.7.1",
3430
prisma: "^5.7.1",
3531
} as const;

apps/cli/src/helpers/addons-setup.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ EXPOSE 3000
5656
CMD ["node", "packages/server/dist/index.js"]
5757
`;
5858

59-
// Create docker-compose.yml
6059
const dockerComposeContent = `version: '3'
6160
6261
services:

apps/cli/src/helpers/turso-setup.ts

Lines changed: 86 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,35 @@
11
import os from "node:os";
22
import path from "node:path";
3-
import { cancel, confirm, isCancel, log, spinner, text } from "@clack/prompts";
3+
import {
4+
cancel,
5+
confirm,
6+
isCancel,
7+
log,
8+
select,
9+
spinner,
10+
text,
11+
} from "@clack/prompts";
412
import { $ } from "execa";
513
import fs from "fs-extra";
614
import pc from "picocolors";
715

8-
interface TursoConfig {
16+
type TursoConfig = {
917
dbUrl: string;
1018
authToken: string;
11-
}
19+
};
20+
21+
type TursoGroup = {
22+
name: string;
23+
locations: string;
24+
version: string;
25+
status: string;
26+
};
1227

1328
async function isTursoInstalled() {
1429
try {
15-
await $`turso --version`;
16-
return true;
17-
} catch {
30+
const result = await $`turso --version`;
31+
return result.exitCode === 0;
32+
} catch (error) {
1833
return false;
1934
}
2035
}
@@ -67,9 +82,66 @@ async function installTursoCLI(isMac: boolean) {
6782
}
6883
}
6984

70-
async function createTursoDatabase(dbName: string): Promise<TursoConfig> {
85+
async function getTursoGroups(): Promise<TursoGroup[]> {
86+
try {
87+
const { stdout } = await $`turso group list`;
88+
const lines = stdout.trim().split("\n");
89+
90+
if (lines.length <= 1) {
91+
return [];
92+
}
93+
94+
const groups = lines.slice(1).map((line) => {
95+
const [name, locations, version, status] = line.trim().split(/\s{2,}/);
96+
return { name, locations, version, status };
97+
});
98+
99+
return groups;
100+
} catch (error) {
101+
console.error("Error fetching Turso groups:", error);
102+
return [];
103+
}
104+
}
105+
106+
async function selectTursoGroup(): Promise<string | null> {
107+
const groups = await getTursoGroups();
108+
109+
if (groups.length === 0) {
110+
return null;
111+
}
112+
113+
if (groups.length === 1) {
114+
return groups[0].name;
115+
}
116+
117+
const groupOptions = groups.map((group) => ({
118+
value: group.name,
119+
label: `${group.name} (${group.locations})`,
120+
}));
121+
122+
const selectedGroup = await select({
123+
message: "Select a Turso database group:",
124+
options: groupOptions,
125+
});
126+
127+
if (isCancel(selectedGroup)) {
128+
cancel(pc.red("Operation cancelled"));
129+
process.exit(0);
130+
}
131+
132+
return selectedGroup as string;
133+
}
134+
135+
async function createTursoDatabase(
136+
dbName: string,
137+
groupName: string | null,
138+
): Promise<TursoConfig> {
71139
try {
72-
await $`turso db create ${dbName}`;
140+
if (groupName) {
141+
await $`turso db create ${dbName} --group ${groupName}`;
142+
} else {
143+
await $`turso db create ${dbName}`;
144+
}
73145
} catch (error) {
74146
if (error instanceof Error && error.message.includes("already exists")) {
75147
throw new Error("DATABASE_EXISTS");
@@ -159,6 +231,8 @@ export async function setupTurso(
159231
await loginToTurso();
160232
}
161233

234+
const selectedGroup = await selectTursoGroup();
235+
162236
let success = false;
163237
let dbName = "";
164238
let suggestedName = path.basename(projectDir);
@@ -180,8 +254,10 @@ export async function setupTurso(
180254
const s = spinner();
181255

182256
try {
183-
s.start(`Creating Turso database "${dbName}"...`);
184-
const config = await createTursoDatabase(dbName);
257+
s.start(
258+
`Creating Turso database "${dbName}"${selectedGroup ? ` in group "${selectedGroup}"` : ""}...`,
259+
);
260+
const config = await createTursoDatabase(dbName, selectedGroup);
185261
await writeEnvFile(projectDir, config);
186262
s.stop("Turso database configured successfully!");
187263
success = true;

0 commit comments

Comments
 (0)