11import os from "node:os" ;
22import path from "node:path" ;
3- import * as p from "@clack/prompts" ;
3+ import {
4+ cancel ,
5+ confirm ,
6+ group ,
7+ intro ,
8+ isCancel ,
9+ log ,
10+ multiselect ,
11+ outro ,
12+ select ,
13+ spinner ,
14+ tasks ,
15+ text ,
16+ } from "@clack/prompts" ;
417import { $ } from "execa" ;
518import fs from "fs-extra" ;
619import { isTursoInstalled , isTursoLoggedIn } from "../utils/turso-cli" ;
@@ -11,21 +24,21 @@ interface TursoConfig {
1124}
1225
1326async function loginToTurso ( ) {
14- const spinner = p . spinner ( ) ;
27+ const s = spinner ( ) ;
1528 try {
16- spinner . start ( "Logging in to Turso..." ) ;
29+ s . start ( "Logging in to Turso..." ) ;
1730 await $ `turso auth login` ;
18- spinner . stop ( "Logged in to Turso successfully!" ) ;
31+ s . stop ( "Logged in to Turso successfully!" ) ;
1932 } catch ( error ) {
20- spinner . stop ( "Failed to log in to Turso" ) ;
33+ s . stop ( "Failed to log in to Turso" ) ;
2134 throw error ;
2235 }
2336}
2437
2538async function installTursoCLI ( isMac : boolean ) {
26- const spinner = p . spinner ( ) ;
39+ const s = spinner ( ) ;
2740 try {
28- spinner . start ( "Installing Turso CLI..." ) ;
41+ s . start ( "Installing Turso CLI..." ) ;
2942
3043 if ( isMac ) {
3144 await $ `brew install tursodatabase/tap/turso` ;
@@ -35,14 +48,14 @@ async function installTursoCLI(isMac: boolean) {
3548 await $ `bash -c '${ installScript } '` ;
3649 }
3750
38- spinner . stop ( "Turso CLI installed successfully!" ) ;
51+ s . stop ( "Turso CLI installed successfully!" ) ;
3952 } catch ( error ) {
4053 if ( error instanceof Error && error . message . includes ( "User force closed" ) ) {
41- spinner . stop ( ) ;
42- p . log . warn ( "Turso CLI installation cancelled by user" ) ;
54+ s . stop ( ) ;
55+ log . warn ( "Turso CLI installation cancelled by user" ) ;
4356 throw new Error ( "Installation cancelled" ) ;
4457 }
45- spinner . stop ( "Failed to install Turso CLI" ) ;
58+ s . stop ( "Failed to install Turso CLI" ) ;
4659 throw error ;
4760 }
4861}
@@ -78,42 +91,38 @@ TURSO_AUTH_TOKEN=`;
7891}
7992
8093function displayManualSetupInstructions ( ) {
81- p . log . info ( "📝 Manual Turso Setup Instructions:" ) ;
82- p . log . info ( "1. Visit https://turso.tech and create an account" ) ;
83- p . log . info ( "2. Create a new database from the dashboard" ) ;
84- p . log . info ( "3. Get your database URL and authentication token" ) ;
85- p . log . info (
86- "4. Add these credentials to the .env file in packages/server/.env" ,
87- ) ;
88- p . log . info ( "\nThe .env file has been created with placeholder variables:" ) ;
89- p . log . info ( "TURSO_DATABASE_URL=your_database_url" ) ;
90- p . log . info ( "TURSO_AUTH_TOKEN=your_auth_token" ) ;
94+ log . info ( "📝 Manual Turso Setup Instructions:" ) ;
95+ log . info ( "1. Visit https://turso.tech and create an account" ) ;
96+ log . info ( "2. Create a new database from the dashboard" ) ;
97+ log . info ( "3. Get your database URL and authentication token" ) ;
98+ log . info ( "4. Add these credentials to the .env file in packages/server/.env" ) ;
99+ log . info ( "\nThe .env file has been created with placeholder variables:" ) ;
100+ log . info ( "TURSO_DATABASE_URL=your_database_url" ) ;
101+ log . info ( "TURSO_AUTH_TOKEN=your_auth_token" ) ;
91102}
92103
93104export async function setupTurso ( projectDir : string ) {
94- p . intro ( "Setting up Turso..." ) ;
95-
96105 const platform = os . platform ( ) ;
97106 const isMac = platform === "darwin" ;
98107 const canInstallCLI = platform !== "win32" ;
99108
100- try {
101- if ( ! canInstallCLI ) {
102- p . log . warn ( "Automatic Turso setup is not supported on Windows." ) ;
103- await writeEnvFile ( projectDir ) ;
104- displayManualSetupInstructions ( ) ;
105- return ;
106- }
109+ if ( ! canInstallCLI ) {
110+ log . warn ( "Automatic Turso setup is not supported on Windows." ) ;
111+ await writeEnvFile ( projectDir ) ;
112+ displayManualSetupInstructions ( ) ;
113+ return ;
114+ }
107115
116+ try {
108117 const isCliInstalled = await isTursoInstalled ( ) ;
109118
110119 if ( ! isCliInstalled ) {
111- const shouldInstall = await p . confirm ( {
120+ const shouldInstall = await confirm ( {
112121 message : "Would you like to install Turso CLI?" ,
113122 } ) ;
114123
115- if ( p . isCancel ( shouldInstall ) ) {
116- p . cancel ( "Operation cancelled" ) ;
124+ if ( isCancel ( shouldInstall ) ) {
125+ cancel ( "Operation cancelled" ) ;
117126 process . exit ( 0 ) ;
118127 }
119128
@@ -123,53 +132,73 @@ export async function setupTurso(projectDir: string) {
123132 return ;
124133 }
125134
126- await installTursoCLI ( isMac ) ;
135+ const s = spinner ( ) ;
136+ s . start ( "Installing Turso CLI..." ) ;
137+ try {
138+ if ( isMac ) {
139+ await $ `brew install tursodatabase/tap/turso` ;
140+ } else {
141+ const { stdout : installScript } =
142+ await $ `curl -sSfL https://get.tur.so/install.sh` ;
143+ await $ `bash -c '${ installScript } '` ;
144+ }
145+ s . stop ( "Turso CLI installed successfully!" ) ;
146+ } catch ( error ) {
147+ s . stop ( "Failed to install Turso CLI" ) ;
148+ throw error ;
149+ }
127150 }
128151
129152 const isLoggedIn = await isTursoLoggedIn ( ) ;
130153 if ( ! isLoggedIn ) {
131- await loginToTurso ( ) ;
154+ const s = spinner ( ) ;
155+ s . start ( "Logging in to Turso..." ) ;
156+ try {
157+ await $ `turso auth login` ;
158+ s . stop ( "Logged in to Turso successfully!" ) ;
159+ } catch ( error ) {
160+ s . stop ( "Failed to log in to Turso" ) ;
161+ throw error ;
162+ }
132163 }
133164
134165 let success = false ;
135166 let dbName = "" ;
136167 let suggestedName = path . basename ( projectDir ) ;
137168
138169 while ( ! success ) {
139- const dbNameResponse = await p . text ( {
170+ const dbNameResponse = await text ( {
140171 message : "Enter database name:" ,
141172 defaultValue : suggestedName ,
142173 } ) ;
143174
144- if ( p . isCancel ( dbNameResponse ) ) {
145- p . cancel ( "Operation cancelled" ) ;
175+ if ( isCancel ( dbNameResponse ) ) {
176+ cancel ( "Operation cancelled" ) ;
146177 process . exit ( 0 ) ;
147178 }
148179
149180 dbName = dbNameResponse as string ;
150- const spinner = p . spinner ( ) ;
181+ const s = spinner ( ) ;
151182
152183 try {
153- spinner . start ( `Creating Turso database "${ dbName } "...` ) ;
184+ s . start ( `Creating Turso database "${ dbName } "...` ) ;
154185 const config = await createTursoDatabase ( dbName ) ;
155186 await writeEnvFile ( projectDir , config ) ;
156- spinner . stop ( "Turso database configured successfully!" ) ;
187+ s . stop ( "Turso database configured successfully!" ) ;
157188 success = true ;
158189 } catch ( error ) {
159190 if ( error instanceof Error && error . message === "DATABASE_EXISTS" ) {
160- spinner . stop ( `Database "${ dbName } " already exists` ) ;
191+ s . stop ( `Database "${ dbName } " already exists` ) ;
161192 suggestedName = `${ dbName } -${ Math . floor ( Math . random ( ) * 1000 ) } ` ;
162193 } else {
163194 throw error ;
164195 }
165196 }
166197 }
167-
168- p . outro ( "Turso setup completed successfully!" ) ;
169198 } catch ( error ) {
170- p . log . error ( `Error during Turso setup: ${ error } ` ) ;
199+ log . error ( `Error during Turso setup: ${ error } ` ) ;
171200 await writeEnvFile ( projectDir ) ;
172201 displayManualSetupInstructions ( ) ;
173- p . outro ( "Setup completed with manual configuration required." ) ;
202+ outro ( "Setup completed with manual configuration required." ) ;
174203 }
175204}
0 commit comments