Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eslint proposal #407

Merged
merged 2 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import globals from "globals";
import { config as defaultConfig } from "@fauna/typescript/config/js/eslint.config.js";
import * as espree from "espree";
import { config as defaultConfig } from "@fauna/typescript/config/eslint.config.js";
import globals from "globals";

export default [
...defaultConfig,
Expand Down
39 changes: 34 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
},
"bugs": "https://github.com/fauna/fauna-shell/issues",
"dependencies": {
"@fauna/typescript": "^0.0.7",
"@inquirer/prompts": "^7.0.0",
"awilix": "^12.0.2",
"chalk": "^5.3.0",
Expand All @@ -23,6 +22,7 @@
"devDependencies": {
"@eslint/eslintrc": "^3.1.0",
"@eslint/js": "^9.11.1",
"@fauna/typescript": "^0.0.9",
"@inquirer/testing": "^2.1.7",
"@types/chai": "^5.0.0",
"@types/mocha": "^10.0.1",
Expand Down
9 changes: 5 additions & 4 deletions src/cli.mjs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// @ts-check

import yargs from "yargs";
import chalk from "chalk";
import yargs from "yargs";

import databaseCommand from "./commands/database.mjs";
import evalCommand from "./commands/eval.mjs";
import keyCommand from "./commands/key.mjs";
import loginCommand from "./commands/login.mjs";
import schemaCommand from "./commands/schema/schema.mjs";
import databaseCommand from "./commands/database.mjs";
import keyCommand from "./commands/key.mjs";
import { logArgv, fixPaths, checkForUpdates } from "./lib/middleware.mjs";
import { authNZMiddleware } from "./lib/auth/authNZ.mjs";
import { checkForUpdates, fixPaths, logArgv } from "./lib/middleware.mjs";

/** @typedef {import('awilix').AwilixContainer<import('./config/setup-container.mjs').modifiedInjectables>} cliContainer */

Expand All @@ -28,6 +28,7 @@ export async function run(argvInput, _container) {
const parseYargs = container.resolve("parseYargs");

try {

builtYargs = buildYargs(argvInput);
await parseYargs(builtYargs);
} catch (e) {
Expand Down
9 changes: 5 additions & 4 deletions src/commands/database.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ async function listDatabases(profile) {
const logger = container.resolve("logger");
const accountClient = container.resolve("accountClient");
const accountCreds = container.resolve("accountCreds");
const account_key = accountCreds.get({ key: profile }).account_key;
const accountKey = accountCreds.get({ key: profile }).accountKey;
logger.stdout("Listing Databases...");
const databases = await accountClient.listDatabases(account_key);
const databases = await accountClient.listDatabases(accountKey);
logger.stdout(databases);
}

Expand All @@ -29,13 +29,14 @@ function buildDatabaseCommand(yargs) {
}

function databaseHandler(argv) {
const logger = container.resolve("logger");
const method = argv.method;
switch (method) {
case "create":
console.log("Creating database...");
logger.stdout("Creating database...");
break;
case "delete":
console.log("Deleting database...");
logger.stdout("Deleting database...");
break;
case "list":
listDatabases(argv.profile);
Expand Down
100 changes: 53 additions & 47 deletions src/commands/eval.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

const EVAL_OUTPUT_FORMATS = ["json", "json-tagged", "shell"];

import util from "util";
import { existsSync } from "fs";
import * as misc from "../lib/misc.mjs";
import util from "util";

import { container } from "../cli.mjs";
import {
// ensureDbScopeClient,
commonQueryOptions,
} from "../lib/command-helpers.mjs";
import { container } from "../cli.mjs";
import * as misc from "../lib/misc.mjs";

const { runQuery } = misc;

Expand All @@ -25,6 +26,7 @@ async function writeFormattedJson(file, data) {
return str;
} else {
// await writeFile(file, str);
return undefined;
}
}

Expand All @@ -39,6 +41,7 @@ async function writeFormattedShell(file, str) {
return str;
} else {
// await writeFile(file, str);
return undefined;
}
}

Expand All @@ -54,26 +57,41 @@ async function writeFormattedOutput(file, data, format) {
return writeFormattedJson(file, data);
} else if (format === "shell") {
return writeFormattedShell(file, util.inspect(data, { depth: null }));
} else {
throw new Error(`Unrecognized format ${format}.`);
}
}

/**
* Perform a v4 or v10 query, depending on the FQL version
*
* @param {Object} client - An instance of the client used to execute the query.
* @param {string} fqlQuery - The FQL v4 query to be executed.
* @param {string} outputFile - Target filename
* @param {Object} flags - Options for the query execution.
* @param {("4" | "10")} flags.version - FQL version number
* @param {("json" | "json-tagged" | "shell")} flags.format - Result format
* @param {boolean} [flags.typecheck] - (Optional) Flag to enable typechecking
*/
export async function performQuery(client, fqlQuery, outputFile, flags) {
if (flags.version === "4") {
const res = performV4Query(client, fqlQuery, outputFile, flags);
return res;
async function writeFormattedOutputV10(file, res, format) {
const isOk = res.status >= 200 && res.status <= 299;

if (format === "json" || format === "json-tagged") {
if (isOk) {
return writeFormattedJson(file, res.body.data);
} else {
return writeFormattedJson(file, {
error: res.body.error,
summary: res.body.summary,
});
}
} else if (format === "shell") {
let output = "";
if (isOk) {
output += res.body.summary ?? "";
if (output) {
output += "\n\n";
}
output += res.body.data ?? "";
} else {
output = `${res.body.error?.code ?? ""}: ${res.body.error?.message ?? ""}`;
if (res.body.summary) {
output += "\n\n";
output += res.body.summary ?? "";
}
}
return writeFormattedShell(file, output);
} else {
return performV10Query(client, fqlQuery, outputFile, flags);
throw new Error("Unsupported output format");
}
}

Expand Down Expand Up @@ -127,36 +145,24 @@ async function performV4Query(client, fqlQuery, outputFile, flags) {
throw error;
}
}
async function writeFormattedOutputV10(file, res, format) {
const isOk = res.status >= 200 && res.status <= 299;

if (format === "json" || format === "json-tagged") {
if (isOk) {
return writeFormattedJson(file, res.body.data);
} else {
return writeFormattedJson(file, {
error: res.body.error,
summary: res.body.summary,
});
}
} else if (format === "shell") {
let output = "";
if (isOk) {
output += res.body.summary ?? "";
if (output) {
output += "\n\n";
}
output += res.body.data ?? "";
} else {
output = `${res.body.error?.code ?? ""}: ${res.body.error?.message ?? ""}`;
if (res.body.summary) {
output += "\n\n";
output += res.body.summary ?? "";
}
}
return writeFormattedShell(file, output);
/**
* Perform a v4 or v10 query, depending on the FQL version
*
* @param {Object} client - An instance of the client used to execute the query.
* @param {string} fqlQuery - The FQL v4 query to be executed.
* @param {string} outputFile - Target filename
* @param {Object} flags - Options for the query execution.
* @param {("4" | "10")} flags.version - FQL version number
* @param {("json" | "json-tagged" | "shell")} flags.format - Result format
* @param {boolean} [flags.typecheck] - (Optional) Flag to enable typechecking
*/
export async function performQuery(client, fqlQuery, outputFile, flags) {
if (flags.version === "4") {
const res = performV4Query(client, fqlQuery, outputFile, flags);
return res;
} else {
throw new Error("Unsupported output format");
return performV10Query(client, fqlQuery, outputFile, flags);
}
}

Expand Down
Loading