@@ -15,8 +15,6 @@ import { getSecret } from "../lib/fauna-client.mjs";
1515import { reformatFSL } from "../lib/schema.mjs" ;
1616import { listDatabasesWithAccountAPI } from "./database/list.mjs" ;
1717
18- // TODO: handle error/exit case cleanly
19-
2018async function doInit ( argv ) {
2119 const logger = container . resolve ( "logger" ) ;
2220 const getters = [ getDatabaseRunnable , getProjectRunnable , getKeyRunnable ] ;
@@ -65,11 +63,11 @@ async function getDatabaseRunnable(argv /*, priorChoices*/) {
6563
6664 buildCredentials ( { ...argv , user : "default" , database : "us-std" } ) ;
6765 const dbs = ( await listDatabasesWithAccountAPI ( argv ) )
68- . map ( ( db ) => `${ db . region_group } /${ db . name } ` )
66+ . map ( ( db ) => `${ db . regionGroup } /${ db . name } ` )
6967 . map ( ( dbName ) => ( { value : dbName } ) ) ;
7068
7169 runnable . choices . createNewDb = await inquirer . select ( {
72- message : "Select a database to create a project for" ,
70+ message : "Select a database to create an FSL directory for" ,
7371 choices : [
7472 ...dbs ,
7573 {
@@ -81,6 +79,7 @@ async function getDatabaseRunnable(argv /*, priorChoices*/) {
8179
8280 if ( runnable . choices . createNewDb !== "new" ) {
8381 runnable . choices . dbName = runnable . choices . createNewDb . split ( "/" ) [ 1 ] ;
82+ runnable . choices . regionGroup = runnable . choices . createNewDb . split ( "/" ) [ 0 ] ;
8483 return runnable ;
8584 }
8685
@@ -249,26 +248,29 @@ async function getProjectRunnable(argv, priorChoices) {
249248 if ( ! shouldCreateProjectDirectory ) return runnable ;
250249
251250 runnable . choices . dirName = await inquirer . input ( {
252- message : `FSL files are stored in a project directory and are specific to the database "${ priorChoices . dbName } ". What would you like to name this project directory? In the next step, you will choose where to put the directory.` ,
251+ message : `FSL files are stored in a directory and are specific to the database "${ priorChoices . dbName } ". What would you like to name this directory? In the next step, you will choose where to put the directory.` ,
253252 default : priorChoices . dbName ,
254253 } ) ;
255254
256255 runnable . choices . dirPath = await fileSelector ( {
257- message : "Where would you like the project directory to be created?" ,
256+ message : "Where would you like the FSL directory to be created?" ,
258257 type : "directory" ,
259258 } ) ;
260259
261- runnable . runner = async ( {
262- choices : { createNewDb, demoData, dirPath, dirName, dbName, regionGroup } ,
263- } ) => {
260+ runnable . runner = async ( { choices } ) => {
261+ const { createNewDb, demoData, dirPath, dirName, dbName, regionGroup } =
262+ choices ;
264263 const fs = container . resolve ( "fs" ) ;
265264 const fsp = container . resolve ( "fsp" ) ;
266265 const path = await import ( "path" ) ;
267- logger . stdout (
268- `Creating project directory at ${ path . join ( dirPath , dirName ) } ` ,
269- ) ;
266+ logger . stdout ( `Creating FSL directory at ${ path . join ( dirPath , dirName ) } ` ) ;
270267
271- if ( ! createNewDb ) {
268+ buildCredentials ( {
269+ ...argv ,
270+ user : "default" ,
271+ database : `${ regionGroup } /${ dbName } ` ,
272+ } ) ;
273+ if ( createNewDb !== "new" ) {
272274 // existing db? fetch the schema
273275 // TODO: this has huge overlap with schema pull - should refactor so it's only in one place
274276 fs . mkdirSync ( path . join ( dirPath , dirName ) , { recursive : true } ) ;
@@ -281,6 +283,13 @@ async function getProjectRunnable(argv, priorChoices) {
281283 secret,
282284 } ) ;
283285
286+ const { version } = await makeFaunaRequest ( {
287+ argv,
288+ path : "/schema/1/staged/status" ,
289+ method : "GET" ,
290+ secret,
291+ } ) ;
292+
284293 // sort for consistent order (it's nice for tests)
285294 const filenames = filesResponse . files
286295 . map ( ( file ) => file . filename )
@@ -290,10 +299,12 @@ async function getProjectRunnable(argv, priorChoices) {
290299 const getAllSchemaFileContents = container . resolve (
291300 "getAllSchemaFileContents" ,
292301 ) ;
293- const contents = await getAllSchemaFileContents ( filenames , {
294- ...argv ,
295- secret,
296- } ) ;
302+ const contents = await getAllSchemaFileContents (
303+ filenames ,
304+ "active" ,
305+ version ,
306+ argv ,
307+ ) ;
297308
298309 // don't start writing files until we've successfully fetched all the remote schema files
299310 const promises = [ ] ;
@@ -321,11 +332,6 @@ async function getProjectRunnable(argv, priorChoices) {
321332 staged : "false" ,
322333 } ) ;
323334
324- buildCredentials ( {
325- ...argv ,
326- user : "default" ,
327- database : `${ regionGroup } /${ dbName } ` ,
328- } ) ;
329335 await makeFaunaRequest ( {
330336 argv,
331337 path : "/schema/1/update" ,
@@ -347,9 +353,7 @@ async function getProjectRunnable(argv, priorChoices) {
347353 ) ,
348354 ] ) ;
349355 }
350- logger . stdout (
351- `Created project directory at ${ path . join ( dirPath , dirName ) } ` ,
352- ) ;
356+ logger . stdout ( `Created FSL directory at ${ path . join ( dirPath , dirName ) } ` ) ;
353357 } ;
354358
355359 return runnable ;
0 commit comments