1
1
import { OptionValues } from 'commander'
2
2
import inquirer from 'inquirer'
3
3
import BaseCommand from '../base-command.js'
4
- import { getExtension , getExtensionInstallations , installExtension } from './utils.js'
4
+ import { getExtension , getExtensionInstallations , getSiteConfiguration , installExtension } from './utils.js'
5
5
import { initDrizzle } from './drizzle.js'
6
6
7
7
const NETLIFY_DATABASE_EXTENSION_SLUG = '-94w9m6w-netlify-database-extension'
@@ -37,7 +37,8 @@ const init = async (_options: OptionValues, command: BaseCommand) => {
37
37
if ( ! command . netlify . api . accessToken ) {
38
38
throw new Error ( `No access token found, please login with netlify login` )
39
39
}
40
-
40
+ console . log ( `Initializing a new database for site: ${ command . siteId }
41
+ Please wait...` )
41
42
let site : Awaited < ReturnType < typeof command . netlify . api . getSite > >
42
43
try {
43
44
// @ts -expect-error -- feature_flags is not in the types
@@ -76,16 +77,27 @@ const init = async (_options: OptionValues, command: BaseCommand) => {
76
77
if ( ! dbExtensionInstallation ) {
77
78
console . log ( `Netlify Database extension not installed on team ${ site . account_id } , attempting to install now...` )
78
79
79
- const installed = await installExtension ( {
80
- accountId : site . account_id ,
81
- token : netlifyToken ,
82
- slug : NETLIFY_DATABASE_EXTENSION_SLUG ,
83
- hostSiteUrl : extension . hostSiteUrl ?? '' ,
84
- } )
85
- if ( ! installed ) {
86
- throw new Error ( `Failed to install extension on team ${ site . account_id } : ${ NETLIFY_DATABASE_EXTENSION_SLUG } ` )
80
+ const answers = await inquirer . prompt ( [
81
+ {
82
+ type : 'confirm' ,
83
+ name : 'installExtension' ,
84
+ message : `Netlify Database extension is not installed on team ${ site . account_id } , would you like to install it now?` ,
85
+ } ,
86
+ ] )
87
+ if ( answers . installExtension ) {
88
+ const installed = await installExtension ( {
89
+ accountId : site . account_id ,
90
+ token : netlifyToken ,
91
+ slug : NETLIFY_DATABASE_EXTENSION_SLUG ,
92
+ hostSiteUrl : extension . hostSiteUrl ?? '' ,
93
+ } )
94
+ if ( ! installed ) {
95
+ throw new Error ( `Failed to install extension on team ${ site . account_id } : ${ NETLIFY_DATABASE_EXTENSION_SLUG } ` )
96
+ }
97
+ console . log ( `Netlify Database extension installed on team ${ site . account_id } ` )
98
+ } else {
99
+ return
87
100
}
88
- console . log ( `Netlify Database extension installed on team ${ site . account_id } ` )
89
101
}
90
102
91
103
try {
@@ -125,6 +137,60 @@ const init = async (_options: OptionValues, command: BaseCommand) => {
125
137
return
126
138
}
127
139
140
+ const status = async ( _options : OptionValues , command : BaseCommand ) => {
141
+ if ( ! command . siteId ) {
142
+ console . error ( `The project must be linked with netlify link before initializing a database.` )
143
+ return
144
+ }
145
+ // check if this site has a db initialized
146
+ const site = await command . netlify . api . getSite ( { siteId : command . siteId } )
147
+ if ( ! site . account_id ) {
148
+ throw new Error ( `No account id found for site ${ command . siteId } ` )
149
+ }
150
+ if ( ! command . netlify . api . accessToken ) {
151
+ throw new Error ( `You must be logged in with netlify login to check the status of the database` )
152
+ }
153
+ const netlifyToken = command . netlify . api . accessToken . replace ( 'Bearer ' , '' )
154
+ const extensionInstallation = await getExtensionInstallations ( {
155
+ accountId : site . account_id ,
156
+ siteId : command . siteId ,
157
+ token : netlifyToken ,
158
+ } )
159
+
160
+ if ( ! extensionInstallation ) {
161
+ console . log ( `Netlify Database extension not installed on team ${ site . account_id } ` )
162
+ return
163
+ }
164
+
165
+ const siteConfig = await getSiteConfiguration ( {
166
+ siteId : command . siteId ,
167
+ accountId : site . account_id ,
168
+ slug : NETLIFY_DATABASE_EXTENSION_SLUG ,
169
+ token : netlifyToken ,
170
+ } )
171
+
172
+ if ( ! siteConfig ) {
173
+ throw new Error ( `Failed to get site configuration for site ${ command . siteId } ` )
174
+ }
175
+ try {
176
+ const siteEnv = await command . netlify . api . getEnvVar ( {
177
+ accountId : site . account_id ,
178
+ siteId : command . siteId ,
179
+ key : 'NETLIFY_DATABASE_URL' ,
180
+ } )
181
+
182
+ if ( siteEnv . key === 'NETLIFY_DATABASE_URL' ) {
183
+ console . log ( `Database initialized for site: ${ command . siteId } ` )
184
+ return
185
+ }
186
+ } catch {
187
+ throw new Error (
188
+ `Database not initialized for site: ${ command . siteId } .
189
+ Run 'netlify db init' to initialize a database` ,
190
+ )
191
+ }
192
+ }
193
+
128
194
export const createDatabaseCommand = ( program : BaseCommand ) => {
129
195
const dbCommand = program . command ( 'db' ) . alias ( 'database' ) . description ( `TODO: write description for database command` )
130
196
@@ -134,5 +200,7 @@ export const createDatabaseCommand = (program: BaseCommand) => {
134
200
. option ( '--drizzle' , 'Sets up drizzle-kit and drizzle-orm in your project' )
135
201
. action ( init )
136
202
203
+ dbCommand . command ( 'status' ) . description ( 'Check the status of the database' ) . action ( status )
204
+
137
205
return dbCommand
138
206
}
0 commit comments