@@ -6,8 +6,6 @@ const request = require('request')
66const { Client, query : Q } = require ( 'faunadb' )
77const streamToPromise = require ( 'stream-to-promise' )
88
9- const FAUNA_ADMIN_KEY = process . env . FAUNA_ADMIN_KEY
10-
119const MakeLatestEntriesIndex = ( ) =>
1210 Q . CreateIndex ( {
1311 name : 'latestEntries' ,
@@ -88,15 +86,29 @@ const MakeGuestbookKey = () =>
8886 role : Q . Role ( 'GuestbookRole' ) ,
8987 } )
9088
91- const askAdminKey = ( ) => {
89+ const isDatabasePrepared = ( { client } ) =>
90+ client . query ( Q . Exists ( Q . Index ( 'latestEntries' ) ) )
91+
92+ const resolveAdminKey = ( ) => {
93+ if ( process . env . FAUNA_ADMIN_KEY ) {
94+ return Promise . resolve ( process . env . FAUNA_ADMIN_KEY )
95+ }
96+
9297 const rl = readline . createInterface ( {
9398 input : process . stdin ,
9499 output : process . stdout ,
95100 } )
96101
97- return new Promise ( ( resolve ) => {
102+ return new Promise ( ( resolve , reject ) => {
98103 rl . question ( 'Please provide the Fauna admin key:\n' , ( res ) => {
99104 rl . close ( )
105+
106+ if ( ! res ) {
107+ return reject (
108+ new Error ( 'You need to provide a key, closing. Try again' )
109+ )
110+ }
111+
100112 resolve ( res )
101113 } )
102114 } )
@@ -117,8 +129,6 @@ const importSchema = (adminKey) =>
117129
118130const findImportError = ( msg ) => {
119131 switch ( true ) {
120- case msg . startsWith ( 'Invalid authorization header' ) :
121- return 'You need to provide a secret, closing. Try again'
122132 case msg . startsWith ( 'Invalid database secret' ) :
123133 return 'The secret you have provided is not valid, closing. Try again'
124134 case ! msg . includes ( 'success' ) :
@@ -129,17 +139,24 @@ const findImportError = (msg) => {
129139}
130140
131141const main = async ( ) => {
132- const adminKey = FAUNA_ADMIN_KEY || ( await askAdminKey ( ) )
142+ const adminKey = await resolveAdminKey ( )
143+ const client = new Client ( { secret : adminKey } )
144+
145+ if ( await isDatabasePrepared ( { client } ) ) {
146+ return console . info (
147+ 'Fauna resources have already been prepared. ' +
148+ 'If you want to install it once again, please, create a fresh database and re-run the script with the other key'
149+ )
150+ }
151+
133152 const importMsg = await importSchema ( adminKey )
134153 const importErrorMsg = findImportError ( importMsg )
135154
136155 if ( importErrorMsg ) {
137156 return Promise . reject ( new Error ( importErrorMsg ) )
138157 }
139158
140- console . log ( '1. Successfully imported schema' )
141-
142- const client = new Client ( { secret : adminKey } )
159+ console . log ( '- Successfully imported schema' )
143160
144161 for ( const Make of [
145162 MakeLatestEntriesIndex ,
@@ -149,13 +166,18 @@ const main = async () => {
149166 await client . query ( Make ( ) )
150167 }
151168
152- const { secret } = await client . query ( MakeGuestbookKey ( ) )
169+ console . log ( '- Created Fauna resources' )
170+
171+ if ( process . env . FAUNA_ADMIN_KEY ) {
172+ // Assume it's a Vercel environment, no need for .env.local file
173+ return
174+ }
153175
154- console . log ( '2. Created Fauna resources' )
176+ const { secret } = await client . query ( MakeGuestbookKey ( ) )
155177
156- await fs . promises . writeFile ( '.env.local' , `FAUNADB_CLIENT_SECRET =${ secret } \n` )
178+ await fs . promises . writeFile ( '.env.local' , `FAUNA_CLIENT_SECRET =${ secret } \n` )
157179
158- console . log ( '3. Created .env.local file with secret' )
180+ console . log ( '- Created .env.local file with secret' )
159181}
160182
161183main ( ) . catch ( ( err ) => {
0 commit comments