Skip to content

Commit

Permalink
cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
henryfauna committed Dec 12, 2024
1 parent d0d9f20 commit 578a456
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/commands/schema/push.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import path from "path";

import { container } from "../../cli.mjs";
import { ValidationError } from "../../lib/errors.mjs";
import { yargsWithCommonQueryOptions } from "../../lib/command-helpers.mjs";
import { ValidationError } from "../../lib/errors.mjs";
import { getSecret } from "../../lib/fauna-client.mjs";
import { reformatFSL } from "../../lib/schema.mjs";
import { localSchemaOptions } from "./schema.mjs";
Expand Down
51 changes: 23 additions & 28 deletions src/commands/schema/status.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,21 @@ import { reformatFSL } from "../../lib/schema.mjs";
import { localSchemaOptions } from "./schema.mjs";

const tab = " ";

// Helper functions to reduce repetition
const logLineWithTab = (
line,
{ numTabs = 1, logger = container.resolve("logger").stdout } = {},
) => logger(tab.repeat(numTabs) + line);

const formatDatabaseName = (database) => (database ? ` for '${database}'` : "");

const logDiff = (diff, numTabs = 3) => {
for (const line of diff.trim().split("\n")) {
logLineWithTab(line, { numTabs });
}
};

async function doStatus(argv) {

Check warning on line 29 in src/commands/schema/status.mjs

View workflow job for this annotation

GitHub Actions / lint

Async function 'doStatus' has a complexity of 14. Maximum allowed is 10
const logger = container.resolve("logger");
const makeFaunaRequest = container.resolve("makeFaunaRequest");
Expand Down Expand Up @@ -54,47 +64,35 @@ async function doStatus(argv) {
}

// Output the status response

switch (statusResponse.status) {
case "none":
logger.stdout(
`No changes staged ${argv.database ? `for '${chalk.bold(argv.database)}'` : ""}.`,
);
logger.stdout(`No changes staged${formatDatabaseName(argv.database)}.`);
break;
case "pending":
logger.stdout(
`Staged changes ${argv.database ? `for '${chalk.bold(argv.database)}'` : ""} are ${chalk.bold(statusResponse.status)}:`,
);
if (statusResponse.pending_summary) {
logLineWithTab(statusResponse.pending_summary.trim());
}
break;
case "ready":
logger.stdout(
`Staged changes ${argv.database ? `for '${chalk.bold(argv.database)}'` : ""} are ${chalk.bold(statusResponse.status)}:`,
`Staged changes${formatDatabaseName(argv.database)} are ${chalk.bold(statusResponse.status)}:`,
);
if (statusResponse.diff) {
if (statusResponse.status === "ready" && statusResponse.diff) {
logLineWithTab("(use `fauna schema commit` to commit staged changes)");
for (const line of statusResponse.diff.trim().split("\n")) {
logLineWithTab(line, { numTabs: 3 });
}
logDiff(statusResponse.diff);
} else if (statusResponse.pending_summary) {
logLineWithTab(statusResponse.pending_summary.trim());
}
break;
case "failed":
logger.stdout(
`Staged changes ${argv.database ? `for '${chalk.bold(argv.database)}'` : ""} have ${chalk.bold(statusResponse.status)}. `,
`Staged changes${formatDatabaseName(argv.database)} have ${chalk.bold(statusResponse.status)}.`,
);
break;
default:
logLineWithTab(`Staged changes: ${statusResponse.status}`);
break;
}

// Output the diff response

// Handle local changes
if (!hasLocalSchema) {
logger.stdout(
`\nNo local changes. No schema files found in '${chalk.bold(absoluteDirPath)}'.\n`,
`\nNo local changes. No schema files found in '${absoluteDirPath}'.\n`,
);
return;
}
Expand All @@ -105,19 +103,16 @@ async function doStatus(argv) {

if (diffResponse.diff === "") {
logger.stdout(
`\nNo local changes${argv.dir !== "." ? ` in '${chalk.bold(argv.dir)}'` : ""}.\n`,
`\nNo local changes${argv.dir !== "." ? ` in '${argv.dir}'` : ""}.\n`,
);
return;
}

logger.stdout(
`\nLocal changes${argv.dir !== "." ? ` in '${chalk.bold(argv.dir)}'` : ""}:`,
);
const dirInfo = argv.dir !== "." ? ` in '${argv.dir}'` : "";
logger.stdout(`\nLocal changes${dirInfo}:`);
logLineWithTab("(use `fauna schema diff` to display local changes)");
logLineWithTab("(use `fauna schema push` to stage local changes)");
for (const line of diffResponse.diff.trim().split("\n")) {
logLineWithTab(line, { numTabs: 3 });
}
logDiff(diffResponse.diff);
logger.stdout("");
}

Expand Down

0 comments on commit 578a456

Please sign in to comment.