@@ -4,7 +4,7 @@ import { OptionValues } from 'commander'
4
4
import BaseCommand from '../base-command.js'
5
5
6
6
import openBrowser from '../../utils/open-browser.js'
7
- import { getExtension , getExtensionInstallations , installExtension } from './utils.js'
7
+ import { carefullyWriteFile , getExtension , getExtensionInstallations , installExtension } from './utils.js'
8
8
import { getToken } from '../../utils/command-helpers.js'
9
9
import inquirer from 'inquirer'
10
10
import { NetlifyAPI } from 'netlify'
@@ -69,25 +69,24 @@ const init = async (_options: OptionValues, command: BaseCommand) => {
69
69
} )
70
70
}
71
71
72
+ if ( ! command . netlify . api . accessToken ) {
73
+ throw new Error ( `No access token found, please login with netlify login` )
74
+ }
75
+
72
76
let site : Awaited < ReturnType < typeof command . netlify . api . getSite > >
73
77
try {
74
78
// @ts -expect-error -- feature_flags is not in the types
75
79
site = await command . netlify . api . getSite ( { siteId : command . siteId , feature_flags : 'cli' } )
76
80
} catch ( e ) {
77
- console . error ( `Error getting site, make sure you are logged in with netlify login` , e )
78
- return
81
+ throw new Error ( `Error getting site, make sure you are logged in with netlify login` , {
82
+ cause : e ,
83
+ } )
79
84
}
80
85
if ( ! site . account_id ) {
81
- console . error ( `Error getting site, make sure you are logged in with netlify login` )
82
- return
83
- }
84
- if ( ! command . netlify . api . accessToken ) {
85
- console . error ( `You must be logged in with netlify login to initialize a database.` )
86
- return
86
+ throw new Error ( `No account id found for site ${ command . siteId } ` )
87
87
}
88
88
89
89
const netlifyToken = command . netlify . api . accessToken . replace ( 'Bearer ' , '' )
90
-
91
90
const extension = await getExtension ( {
92
91
accountId : site . account_id ,
93
92
token : netlifyToken ,
@@ -132,14 +131,13 @@ const init = async (_options: OptionValues, command: BaseCommand) => {
132
131
} )
133
132
134
133
if ( siteEnv . key === 'NETLIFY_DATABASE_URL' ) {
135
- console . error ( `Database already initialized for site: ${ command . siteId } , skipping.` )
136
- return
134
+ throw new Error ( `Database already initialized for site: ${ command . siteId } ` )
137
135
}
138
136
} catch {
139
137
// no op, env var does not exist, so we just continue
140
138
}
141
139
142
- console . log ( 'Initializing a new database for site:' , command . siteId )
140
+ console . log ( 'Initializing a new database for site: ' , command . siteId )
143
141
144
142
const initEndpoint = new URL (
145
143
'/cli-db-init' ,
@@ -276,20 +274,3 @@ export const db = drizzle({
276
274
schema
277
275
});
278
276
`
279
-
280
- const carefullyWriteFile = async ( filePath : string , data : string ) => {
281
- if ( fs . existsSync ( filePath ) ) {
282
- const answers = await inquirer . prompt ( [
283
- {
284
- type : 'confirm' ,
285
- name : 'overwrite' ,
286
- message : `Overwrite existing ${ path . basename ( filePath ) } ?` ,
287
- } ,
288
- ] )
289
- if ( answers . overwrite ) {
290
- fs . writeFileSync ( filePath , data )
291
- }
292
- } else {
293
- fs . writeFileSync ( filePath , data )
294
- }
295
- }
0 commit comments