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

Gussy Up fauna status #521

Open
wants to merge 12 commits into
base: v3
Choose a base branch
from
63 changes: 49 additions & 14 deletions src/commands/schema/status.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,22 @@
import { reformatFSL } from "../../lib/schema.mjs";
import { localSchemaOptions } from "./schema.mjs";

const tab = " ";

const logLineWithTab = (
line,
{ numTabs = 1, logger = container.resolve("logger").stdout } = {},
) => logger(tab.repeat(numTabs) + line);

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

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

async function doStatus(argv) {

Check warning on line 28 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 @@ -48,19 +63,35 @@
}

// Output the status response
logger.stdout(`Staged changes: ${chalk.bold(statusResponse.status)}`);
if (statusResponse.pending_summary !== "") {
logger.stdout(statusResponse.pending_summary);
}
if (statusResponse.diff) {
logger.stdout("Staged changes:\n");
logger.stdout(statusResponse.diff.split("\n").join("\n "));
switch (statusResponse.status) {
case "none":
logger.stdout(`No staged changes${formatDatabaseName(argv.database)}.`);
break;
case "pending":
case "ready":
logger.stdout(
`Staged changes${formatDatabaseName(argv.database)} are ${chalk.bold(statusResponse.status)}:`,
);
if (statusResponse.status === "ready" && statusResponse.diff) {
logLineWithTab("(use `fauna schema commit` to commit staged changes)");
logBlockWithTab(statusResponse.diff);
} else if (statusResponse.pending_summary) {
logBlockWithTab(statusResponse.pending_summary, 1);
}
Comment on lines +75 to +80
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like how the diff is displayed for local changes and ready staged changes. Do you think there is a clear way to include the diff when reporting pending changes?

break;
case "failed":
logger.stdout(
`Staged changes${formatDatabaseName(argv.database)} have ${chalk.bold(statusResponse.status)}.`,
);
break;
default:
logLineWithTab(`Staged changes: ${statusResponse.status}`);
}

// Output the diff response
// Handle local changes
if (!hasLocalSchema) {
logger.stdout(
`Local changes: ${chalk.bold(`no schema files found in '${absoluteDirPath}'`)}\n`,
`\nNo local changes. No schema files found in '${absoluteDirPath}'.\n`,
);
return;
}
Expand All @@ -70,14 +101,18 @@
}

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

logger.stdout(`Local changes:\n`);
logger.stdout(` ${diffResponse.diff.split("\n").join("\n ")}`);
logger.stdout("(use `fauna schema diff` to display local changes)");
logger.stdout("(use `fauna schema push` to stage local changes)");
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)");
logBlockWithTab(diffResponse.diff);
logger.stdout("");
}

function buildStatusCommand(yargs) {
Expand Down
71 changes: 44 additions & 27 deletions test/schema/status.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { expect } from "chai";
import chalk from "chalk";
import sinon from "sinon";
import path from "path";

import { run } from "../../src/cli.mjs";
import { setupTestContainer as setupContainer } from "../../src/config/setup-test-container.mjs";
Expand Down Expand Up @@ -110,11 +110,11 @@ describe("schema status", function () {
}),
{ ...commonFetchParams, method: "POST", body: new FormData() },
);
expect(logger.stdout).to.have.been.calledWith(`No staged changes.`);

const absoluteDirPath = path.resolve(".");
expect(logger.stdout).to.have.been.calledWith(
`Staged changes: ${chalk.bold("none")}`,
);
expect(logger.stdout).to.have.been.calledWith(
sinon.match(/^Local changes: .*no schema files found in.*\n$/),
`\nNo local changes. No schema files found in '${absoluteDirPath}'.\n`,
);
});

Expand Down Expand Up @@ -151,12 +151,8 @@ describe("schema status", function () {
}),
{ ...commonFetchParams, method: "POST", body: reformatFSL(fsl) },
);
expect(logger.stdout).to.have.been.calledWith(
`Staged changes: ${chalk.bold("none")}`,
);
expect(logger.stdout).to.have.been.calledWith(
`Local changes: ${chalk.bold("none")}\n`,
);
expect(logger.stdout).to.have.been.calledWith("No staged changes.");
expect(logger.stdout).to.have.been.calledWith("\nNo local changes.\n");
});

it("fetches the current status when there are only local changes", async function () {
Expand Down Expand Up @@ -195,18 +191,23 @@ describe("schema status", function () {
}),
{ ...commonFetchParams, method: "POST", body: reformatFSL(fsl) },
);
expect(logger.stdout).to.have.been.calledWith("No staged changes.");

expect(logger.stdout).to.have.been.calledWith("\nLocal changes:");
expect(logger.stdout).to.have.been.calledWith(
`Staged changes: ${chalk.bold("none")}`,
" (use `fauna schema diff` to display local changes)",
);
expect(logger.stdout).to.have.been.calledWith(`Local changes:\n`);
expect(logger.stdout).to.have.been.calledWith(
` * Adding collection \`NewCollection\` to collections.fsl:2:1\n * Modifying collection \`OrderItem\` at collections.fsl:125:1\n * Modifying function \`createOrUpdateCartItem\` at functions.fsl:2:1\n `,
" (use `fauna schema push` to stage local changes)",
);
expect(logger.stdout).to.have.been.calledWith(
"(use `fauna schema diff` to display local changes)",
` * Adding collection \`NewCollection\` to collections.fsl:2:1`,
);
expect(logger.stdout).to.have.been.calledWith(
"(use `fauna schema push` to stage local changes)",
" * Modifying collection `OrderItem` at collections.fsl:125:1",
);
expect(logger.stdout).to.have.been.calledWith(
" * Modifying function `createOrUpdateCartItem` at functions.fsl:2:1",
);
expect(logger.stderr).not.to.have.been.called;
});
Expand Down Expand Up @@ -246,13 +247,19 @@ describe("schema status", function () {
{ ...commonFetchParams, method: "POST", body: reformatFSL(fsl) },
);
expect(logger.stdout).to.have.been.calledWith(
`Staged changes: ${chalk.bold("ready")}`,
`Staged changes are ${chalk.bold("ready")}:`,
);
expect(logger.stdout).to.have.been.calledWith(
" (use `fauna schema commit` to commit staged changes)",
);
expect(logger.stdout).to.have.been.calledWith(
` ${summaryDiff.split("\n")[0]}`,
);
expect(logger.stdout).to.have.been.calledWith(
`Local changes: ${chalk.bold("none")}\n`,
` ${summaryDiff.split("\n")[1]}`,
);
expect(logger.stdout).to.have.been.calledWith(
summaryDiff.split("\n").join("\n "),
` ${summaryDiff.split("\n")[2]}`,
);
expect(logger.stderr).not.to.have.been.called;
});
Expand Down Expand Up @@ -293,22 +300,32 @@ describe("schema status", function () {
{ ...commonFetchParams, method: "POST", body: reformatFSL(fsl) },
);
expect(logger.stdout).to.have.been.calledWith(
`Staged changes: ${chalk.bold("ready")}`,
`Staged changes are ${chalk.bold("ready")}:`,
);
expect(logger.stdout).to.have.been.calledWith(
" (use `fauna schema commit` to commit staged changes)",
);
expect(logger.stdout).to.have.been.calledWith(
` ${summaryDiff.split("\n")[0]}`,
);
expect(logger.stdout).to.have.been.calledWith(
` ${summaryDiff.split("\n")[1]}`,
);
expect(logger.stdout).to.have.been.calledWith(
` ${summaryDiff.split("\n")[2]}`,
);
expect(logger.stdout).to.have.been.calledWith(`Staged changes:\n`);
expect(logger.stdout).to.have.been.calledWith(`Local changes:\n`);
expect(logger.stdout).to.have.been.calledWith(`\nLocal changes:`);
expect(logger.stdout).to.have.been.calledWith(
summaryDiff.split("\n").join("\n "),
" (use `fauna schema diff` to display local changes)",
);
expect(logger.stdout).to.have.been.calledWith(
" * Adding function `newFunction` to functions.fsl:1:1\n" +
" * Modifying function `createOrUpdateCartItem` at functions.fsl:5:1\n ",
" (use `fauna schema push` to stage local changes)",
);
expect(logger.stdout).to.have.been.calledWith(
"(use `fauna schema diff` to display local changes)",
" * Adding function `newFunction` to functions.fsl:1:1",
);
expect(logger.stdout).to.have.been.calledWith(
"(use `fauna schema push` to stage local changes)",
" * Modifying function `createOrUpdateCartItem` at functions.fsl:5:1",
);
expect(logger.stderr).not.to.have.been.called;
});
Expand Down
Loading