From d0d9f2077418789513ca5f98795b3780c4afe3af Mon Sep 17 00:00:00 2001 From: Henry Ball Date: Thu, 12 Dec 2024 13:21:01 -0800 Subject: [PATCH 1/8] gussy up status --- src/commands/schema/status.mjs | 67 +++++++++++++++++++++++++++------- 1 file changed, 54 insertions(+), 13 deletions(-) diff --git a/src/commands/schema/status.mjs b/src/commands/schema/status.mjs index 48fa1dfb..09331bf8 100644 --- a/src/commands/schema/status.mjs +++ b/src/commands/schema/status.mjs @@ -10,6 +10,12 @@ import { getSecret } from "../../lib/fauna-client.mjs"; 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); + async function doStatus(argv) { const logger = container.resolve("logger"); const makeFaunaRequest = container.resolve("makeFaunaRequest"); @@ -48,19 +54,47 @@ async function doStatus(argv) { } // 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 changes staged ${argv.database ? `for '${chalk.bold(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)}:`, + ); + if (statusResponse.diff) { + logLineWithTab("(use `fauna schema commit` to commit staged changes)"); + for (const line of statusResponse.diff.trim().split("\n")) { + logLineWithTab(line, { numTabs: 3 }); + } + } + break; + case "failed": + logger.stdout( + `Staged changes ${argv.database ? `for '${chalk.bold(argv.database)}'` : ""} have ${chalk.bold(statusResponse.status)}. `, + ); + break; + default: + logLineWithTab(`Staged changes: ${statusResponse.status}`); + break; } // Output the diff response + if (!hasLocalSchema) { logger.stdout( - `Local changes: ${chalk.bold(`no schema files found in '${absoluteDirPath}'`)}\n`, + `\nNo local changes. No schema files found in '${chalk.bold(absoluteDirPath)}'.\n`, ); return; } @@ -70,14 +104,21 @@ async function doStatus(argv) { } if (diffResponse.diff === "") { - logger.stdout(`Local changes: ${chalk.bold("none")}\n`); + logger.stdout( + `\nNo local changes${argv.dir !== "." ? ` in '${chalk.bold(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)"); + logger.stdout( + `\nLocal changes${argv.dir !== "." ? ` in '${chalk.bold(argv.dir)}'` : ""}:`, + ); + 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 }); + } + logger.stdout(""); } function buildStatusCommand(yargs) { From 578a4568c32042ab94e9264dc8564727e58fbdad Mon Sep 17 00:00:00 2001 From: Henry Ball Date: Thu, 12 Dec 2024 13:36:27 -0800 Subject: [PATCH 2/8] cleanup code --- src/commands/schema/push.mjs | 2 +- src/commands/schema/status.mjs | 51 +++++++++++++++------------------- 2 files changed, 24 insertions(+), 29 deletions(-) diff --git a/src/commands/schema/push.mjs b/src/commands/schema/push.mjs index a56e5e62..72d6d95d 100644 --- a/src/commands/schema/push.mjs +++ b/src/commands/schema/push.mjs @@ -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"; diff --git a/src/commands/schema/status.mjs b/src/commands/schema/status.mjs index 09331bf8..fa0d02b3 100644 --- a/src/commands/schema/status.mjs +++ b/src/commands/schema/status.mjs @@ -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) { const logger = container.resolve("logger"); const makeFaunaRequest = container.resolve("makeFaunaRequest"); @@ -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; } @@ -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(""); } From 364e2c8f8cfc54aa9a4dd865ee15a97b519733cf Mon Sep 17 00:00:00 2001 From: Henry Ball Date: Thu, 12 Dec 2024 13:51:00 -0800 Subject: [PATCH 3/8] fix --- src/commands/schema/status.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commands/schema/status.mjs b/src/commands/schema/status.mjs index fa0d02b3..17e3065d 100644 --- a/src/commands/schema/status.mjs +++ b/src/commands/schema/status.mjs @@ -77,7 +77,7 @@ async function doStatus(argv) { logLineWithTab("(use `fauna schema commit` to commit staged changes)"); logDiff(statusResponse.diff); } else if (statusResponse.pending_summary) { - logLineWithTab(statusResponse.pending_summary.trim()); + logDiff(statusResponse.pending_summary, 1); } break; case "failed": From 63a34c096cc85b4845080994b84c8b518d56abd3 Mon Sep 17 00:00:00 2001 From: Henry Ball Date: Thu, 12 Dec 2024 13:51:59 -0800 Subject: [PATCH 4/8] fix summary --- src/commands/schema/status.mjs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/commands/schema/status.mjs b/src/commands/schema/status.mjs index 17e3065d..a7978f37 100644 --- a/src/commands/schema/status.mjs +++ b/src/commands/schema/status.mjs @@ -12,20 +12,19 @@ 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) => { +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) { const logger = container.resolve("logger"); const makeFaunaRequest = container.resolve("makeFaunaRequest"); @@ -75,9 +74,9 @@ async function doStatus(argv) { ); if (statusResponse.status === "ready" && statusResponse.diff) { logLineWithTab("(use `fauna schema commit` to commit staged changes)"); - logDiff(statusResponse.diff); + logBlockWithTab(statusResponse.diff); } else if (statusResponse.pending_summary) { - logDiff(statusResponse.pending_summary, 1); + logBlockWithTab(statusResponse.pending_summary, 1); } break; case "failed": @@ -112,7 +111,7 @@ async function doStatus(argv) { logger.stdout(`\nLocal changes${dirInfo}:`); logLineWithTab("(use `fauna schema diff` to display local changes)"); logLineWithTab("(use `fauna schema push` to stage local changes)"); - logDiff(diffResponse.diff); + logBlockWithTab(diffResponse.diff); logger.stdout(""); } From 127f1e624300ba33abc72cbd9213ea817b18a337 Mon Sep 17 00:00:00 2001 From: Henry Ball Date: Thu, 12 Dec 2024 13:53:24 -0800 Subject: [PATCH 5/8] update message --- src/commands/schema/status.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commands/schema/status.mjs b/src/commands/schema/status.mjs index a7978f37..9e211328 100644 --- a/src/commands/schema/status.mjs +++ b/src/commands/schema/status.mjs @@ -65,7 +65,7 @@ async function doStatus(argv) { // Output the status response switch (statusResponse.status) { case "none": - logger.stdout(`No changes staged${formatDatabaseName(argv.database)}.`); + logger.stdout(`No staged changes${formatDatabaseName(argv.database)}.`); break; case "pending": case "ready": From feec095836f78643a18c88e1545276ffe2043835 Mon Sep 17 00:00:00 2001 From: Henry Ball Date: Thu, 12 Dec 2024 15:02:25 -0800 Subject: [PATCH 6/8] fix tests --- test/schema/status.mjs | 71 ++++++++++++++++++++++++++---------------- 1 file changed, 44 insertions(+), 27 deletions(-) diff --git a/test/schema/status.mjs b/test/schema/status.mjs index b100f949..28ca6a9a 100644 --- a/test/schema/status.mjs +++ b/test/schema/status.mjs @@ -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"; @@ -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`, ); }); @@ -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 () { @@ -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; }); @@ -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; }); @@ -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; }); From 51ab8f591973101f34975199365d7b919b7e5594 Mon Sep 17 00:00:00 2001 From: Henry Ball Date: Thu, 12 Dec 2024 15:05:52 -0800 Subject: [PATCH 7/8] let formatter run --- test/schema/status.mjs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/schema/status.mjs b/test/schema/status.mjs index 28ca6a9a..3c9330e6 100644 --- a/test/schema/status.mjs +++ b/test/schema/status.mjs @@ -253,13 +253,13 @@ describe("schema status", function () { " (use `fauna schema commit` to commit staged changes)", ); expect(logger.stdout).to.have.been.calledWith( - " " + summaryDiff.split("\n")[0], + ` ${ summaryDiff.split("\n")[0]}`, ); expect(logger.stdout).to.have.been.calledWith( - " " + summaryDiff.split("\n")[1], + ` ${ summaryDiff.split("\n")[1]}`, ); expect(logger.stdout).to.have.been.calledWith( - " " + summaryDiff.split("\n")[2], + ` ${ summaryDiff.split("\n")[2]}`, ); expect(logger.stderr).not.to.have.been.called; }); @@ -306,13 +306,13 @@ describe("schema status", function () { " (use `fauna schema commit` to commit staged changes)", ); expect(logger.stdout).to.have.been.calledWith( - " " + summaryDiff.split("\n")[0], + ` ${ summaryDiff.split("\n")[0]}`, ); expect(logger.stdout).to.have.been.calledWith( - " " + summaryDiff.split("\n")[1], + ` ${ summaryDiff.split("\n")[1]}`, ); expect(logger.stdout).to.have.been.calledWith( - " " + summaryDiff.split("\n")[2], + ` ${ summaryDiff.split("\n")[2]}`, ); expect(logger.stdout).to.have.been.calledWith(`\nLocal changes:`); expect(logger.stdout).to.have.been.calledWith( From 2776436dfc1be2feda15b8449988e11e4a0f9543 Mon Sep 17 00:00:00 2001 From: Henry Ball Date: Thu, 12 Dec 2024 16:04:59 -0800 Subject: [PATCH 8/8] appease linter --- test/schema/status.mjs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/schema/status.mjs b/test/schema/status.mjs index 3c9330e6..1eed85d1 100644 --- a/test/schema/status.mjs +++ b/test/schema/status.mjs @@ -253,13 +253,13 @@ describe("schema status", function () { " (use `fauna schema commit` to commit staged changes)", ); expect(logger.stdout).to.have.been.calledWith( - ` ${ summaryDiff.split("\n")[0]}`, + ` ${summaryDiff.split("\n")[0]}`, ); expect(logger.stdout).to.have.been.calledWith( - ` ${ summaryDiff.split("\n")[1]}`, + ` ${summaryDiff.split("\n")[1]}`, ); expect(logger.stdout).to.have.been.calledWith( - ` ${ summaryDiff.split("\n")[2]}`, + ` ${summaryDiff.split("\n")[2]}`, ); expect(logger.stderr).not.to.have.been.called; }); @@ -306,13 +306,13 @@ describe("schema status", function () { " (use `fauna schema commit` to commit staged changes)", ); expect(logger.stdout).to.have.been.calledWith( - ` ${ summaryDiff.split("\n")[0]}`, + ` ${summaryDiff.split("\n")[0]}`, ); expect(logger.stdout).to.have.been.calledWith( - ` ${ summaryDiff.split("\n")[1]}`, + ` ${summaryDiff.split("\n")[1]}`, ); expect(logger.stdout).to.have.been.calledWith( - ` ${ summaryDiff.split("\n")[2]}`, + ` ${summaryDiff.split("\n")[2]}`, ); expect(logger.stdout).to.have.been.calledWith(`\nLocal changes:`); expect(logger.stdout).to.have.been.calledWith(