Skip to content

Commit

Permalink
linting changes
Browse files Browse the repository at this point in the history
  • Loading branch information
echo-bravo-yahoo committed Oct 24, 2024
1 parent 9b9e4b8 commit 0d4ae6e
Show file tree
Hide file tree
Showing 28 changed files with 187 additions and 180 deletions.
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
20 changes: 9 additions & 11 deletions src/commands/key.mjs
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import { container } from "../cli.mjs";
import {
authNZMiddleware,
getAccountKey,
getDBKey,
} from "../lib/auth/authNZ.mjs";
import { getAccountKey, getDBKey } from "../lib/auth/authNZ.mjs";

// TODO: this function should just spit out the secret that was created.
// consider an optional flag that will save this secret to the creds file, overwriting
// the existing secret if it exists at key/path/role
async function createKey(argv) {
const { database, profile, role, url, local } = argv;
const { database, profile, role, url } = argv;
const logger = container.resolve("logger");
const accountKey = await getAccountKey(profile);
// TODO: after logging in, should we list the top level databases and create db keys for them?
Expand All @@ -21,14 +17,14 @@ async function createKey(argv) {

// TODO: when using fauna to create a key at the specified database path, we should
// getDBKey(parent path).
const dbSecret = await getDBKey({
const dbSecret = getDBKey({
accountKey,
path: database,
role,
url,
});
console.log("got account key", accountKey);
console.log("got db secret", dbSecret);
logger.stdout("got account key", accountKey);
logger.stdout("got db secret", dbSecret);
}

function buildKeyCommand(yargs) {
Expand Down Expand Up @@ -61,15 +57,17 @@ function buildKeyCommand(yargs) {

function keyHandler(argv) {
const method = argv.method;
const logger = container.resolve("logger");

switch (method) {
case "create":
createKey(argv);
break;
case "delete":
console.log("Deleting key...");
logger.stdout("Deleting key...");
break;
case "list":
console.log("Listing keys...");
logger.stdout("Listing keys...");
break;
default:
break;
Expand Down
6 changes: 3 additions & 3 deletions src/commands/login.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ async function doLogin(argv) {
try {
const tokenParams = oAuth.getTokenParams();
const accessToken = await accountClient.getToken(tokenParams);
const { account_key, refresh_token } =
const { accountKey, refreshToken } =
await accountClient.getSession(accessToken);
accountCreds.save({
creds: { account_key, refresh_token },
creds: { accountKey, refreshToken },
key: argv.profile,
});
logger.stdout(`Login Success!\n`);
} catch (err) {
console.error(err);
logger.stderr(err);
}
});
await oAuth.start();
Expand Down
2 changes: 1 addition & 1 deletion src/commands/schema/abandon.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//@ts-check

import { commonQueryOptions } from "../../lib/command-helpers.mjs";
import { container } from "../../cli.mjs";
import { commonQueryOptions } from "../../lib/command-helpers.mjs";

async function doAbandon(argv) {
const makeFaunaRequest = container.resolve("makeFaunaRequest");
Expand Down
2 changes: 1 addition & 1 deletion src/commands/schema/commit.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//@ts-check

import { commonQueryOptions } from "../../lib/command-helpers.mjs";
import { container } from "../../cli.mjs";
import { commonQueryOptions } from "../../lib/command-helpers.mjs";

async function doCommit(argv) {
const makeFaunaRequest = container.resolve("makeFaunaRequest");
Expand Down
1 change: 1 addition & 0 deletions src/commands/schema/schema.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ export default {
command: "schema",
describe: "Manipulate Fauna schema state",
builder: buildSchema,
// eslint-disable-next-line no-empty-function
handler: () => {},
};
24 changes: 12 additions & 12 deletions src/config/setup-container.mjs
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
import fs from "node:fs";
import * as fsp from "node:fs/promises";
import path from "node:path";
import os from "node:os";
import path from "node:path";
import { exit } from "node:process";

import { confirm } from "@inquirer/prompts";
import * as awilix from "awilix";
import { Lifetime } from "awilix";
import open from "open";
import updateNotifier from "update-notifier";
import { confirm } from "@inquirer/prompts";

import { parseYargs } from "../cli.mjs";
import { performQuery } from "../commands/eval.mjs";
import logger from "../lib/logger.mjs";
import OAuthClient from "../lib/auth/oauth-client.mjs";
import { getSimpleClient } from "../lib/command-helpers.mjs";
import { makeFaunaRequest } from "../lib/db.mjs";
import { FaunaAccountClient } from "../lib/fauna-account-client.mjs";
import fetchWrapper from "../lib/fetch-wrapper.mjs";
import { AccountKey, SecretKey } from "../lib/file-util.mjs";
import logger from "../lib/logger.mjs";
import {
deleteUnusedSchemaFiles,
gatherFSL,
gatherRelativeFSLFilePaths,
getAllSchemaFileContents,
getStagedSchemaStatus,
getSchemaFile,
getSchemaFiles,
deleteUnusedSchemaFiles,
getStagedSchemaStatus,
writeSchemaFiles,
} from "../lib/schema.mjs";
import { makeFaunaRequest } from "../lib/db.mjs";
import fetchWrapper from "../lib/fetch-wrapper.mjs";
import { FaunaAccountClient } from "../lib/fauna-account-client.mjs";
import open from "open";
import OAuthClient from "../lib/auth/oauth-client.mjs";
import { AccountKey, SecretKey } from "../lib/file-util.mjs";
import { parseYargs } from "../cli.mjs";

// import { findUpSync } from 'find-up'
// import fs from 'node:fs'
Expand Down
9 changes: 4 additions & 5 deletions src/config/setup-test-container.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ import fs from "node:fs";
import { normalize } from "node:path";

import * as awilix from "awilix";
import { setupCommonContainer, injectables } from "./setup-container.mjs";
import { f } from "../../test/helpers.mjs";
import { makeFaunaRequest } from "../lib/db.mjs";
import { spy, stub } from "sinon";

import { stub, spy } from "sinon";
import { f } from "../../test/helpers.mjs";
import { parseYargs } from "../cli.mjs";

import { makeFaunaRequest } from "../lib/db.mjs";
import logger from "../lib/logger.mjs";
import { injectables, setupCommonContainer } from "./setup-container.mjs";

// Mocks all _functions_ declared on the injectables export from setup-container.mjs
function automock(container) {
Expand Down
Loading

0 comments on commit 0d4ae6e

Please sign in to comment.