From ccf7cfb2355fa17da9678a09090fabc16e4fa4d2 Mon Sep 17 00:00:00 2001 From: Ashton Eby Date: Mon, 9 Dec 2024 15:15:47 -0800 Subject: [PATCH] bugfix --- src/commands/database/list.mjs | 42 ++++++++++++++++++---------------- src/commands/init.mjs | 10 +++++--- 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/src/commands/database/list.mjs b/src/commands/database/list.mjs index 7c9e9960..71d9efa1 100644 --- a/src/commands/database/list.mjs +++ b/src/commands/database/list.mjs @@ -35,22 +35,36 @@ export async function listDatabasesWithAccountAPI(argv) { pageSize, path: database, }); - return pickOutputFields(response.results, argv); } -export async function listDatabasesWithSecret(argv) { - const { url, secret, pageSize } = argv; - const { runQueryFromString } = container.resolve("faunaClientV10"); +async function listAndFormatDatabasesWithAccountAPI(argv) { + const { json, color } = argv; + const output = await listDatabasesWithAccountAPI(argv); + + if (json) { + container.resolve("logger").stdout(JSON.stringify(output)); + } else { + container.resolve("logger").stdout(formatObjectForShell(output, { color })); + } +} + +async function listDatabasesWithSecret(argv) { + const { url, secret, pageSize, json, color } = argv; + const { runQueryFromString, formatQueryResponse } = + container.resolve("faunaClientV10"); try { - return await runQueryFromString({ + const result = await runQueryFromString({ url, secret, // This gives us back an array of database names. If we want to // provide the after token at some point this query will need to be updated. expression: `Database.all().paginate(${pageSize}).data { ${getOutputFields(argv)} }`, }); + container + .resolve("logger") + .stdout(formatQueryResponse(result, { json, color })); } catch (e) { if (e instanceof FaunaError) { throwForError(e); @@ -60,23 +74,11 @@ export async function listDatabasesWithSecret(argv) { } async function listDatabases(argv) { - let databases; if (argv.secret) { - databases = listDatabasesWithSecret(argv); + return listDatabasesWithSecret(argv); } else { - databases = listDatabasesWithAccountAPI(argv); + return listAndFormatDatabasesWithAccountAPI(argv); } - - if (argv.json) { - return JSON.stringify(databases); - } else { - return formatObjectForShell(databases, { color: argv.color }); - } -} - -async function doListDatabases(argv) { - const logger = container.resolve("logger"); - logger.stdout(await listDatabases(argv)); } function buildListCommand(yargs) { @@ -113,5 +115,5 @@ export default { command: "list", description: "List databases.", builder: buildListCommand, - handler: doListDatabases, + handler: listDatabases, }; diff --git a/src/commands/init.mjs b/src/commands/init.mjs index 7e48197c..4c77d52f 100644 --- a/src/commands/init.mjs +++ b/src/commands/init.mjs @@ -1,6 +1,9 @@ //@ts-check /* eslint-disable require-atomic-updates */ -const __dirname = import.meta.dirname; +import path from "node:path"; +import { fileURLToPath } from "node:url"; +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); import * as inquirer from "@inquirer/prompts"; import fileSelector from "inquirer-file-selector"; @@ -124,6 +127,7 @@ async function getDatabaseRunnable(argv /*, priorChoices*/) { buildCredentials({ ...argv, + user: "default", database: runnable.choices.regionGroup, }); runnable.fql = `Database.create({ @@ -289,11 +293,11 @@ async function getProjectRunnable(argv, priorChoices) { // new db with demo data? create the demo data, then upload the schema await Promise.all([ fsp.cp( - path.join(__dirname, "../lib/schema/demo-collection-schema.fsl"), + path.join(__dirname, "../src/lib/schema/demo-collection-schema.fsl"), path.join(dirPath, dirName, "collections.fsl"), ), fsp.cp( - path.join(__dirname, "../lib/schema/demo-function-schema.fsl"), + path.join(__dirname, "../src/lib/schema/demo-function-schema.fsl"), path.join(dirPath, dirName, "functions.fsl"), ), ]);