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

Improve schema command output #378

Merged
merged 2 commits into from
Oct 9, 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: 3 additions & 1 deletion src/commands/schema/push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export default class PushSchemaCommand extends SchemaCommand {
const params = new URLSearchParams({
...(hasColor() ? { color: colorParam() } : {}),
force: "true",
diff: "summary",
});
const path = new URL(`/schema/1/validate?${params}`, url);
const res = await fetch(path, {
Expand All @@ -74,12 +75,13 @@ export default class PushSchemaCommand extends SchemaCommand {

let message = "Accept and push changes?";
if (json.diff) {
this.log(`Proposed diff:\n`);
this.log("Proposed diff:\n");
this.log(json.diff);
} else {
this.log("No logical changes.");
message = "Push file contents anyway?";
}
this.log("(use `fauna schema diff` to show the complete diff)");
const confirmed = await confirm({
message,
default: false,
Expand Down
76 changes: 64 additions & 12 deletions src/commands/schema/status.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import SchemaCommand from "../../lib/schema-command";
import { colorParam, hasColor } from "../../lib/color";
import { bold, colorParam, hasColor, reset } from "../../lib/color";

export default class StatusSchemaCommand extends SchemaCommand {
static flags = {
Expand All @@ -10,15 +10,19 @@ export default class StatusSchemaCommand extends SchemaCommand {
static examples = ["$ fauna schema status"];

async run() {
try {
const { url, secret } = await this.fetchsetup();
process.removeAllListeners("warning");

const { url, secret } = await this.fetchsetup();
const fps = this.gatherRelativeFSLFilePaths();
const files = this.read(fps);

const params = new URLSearchParams({
try {
const statusParams = new URLSearchParams({
...(hasColor() ? { color: colorParam() } : {}),
diff: "true",
diff: "summary",
});
const res = await fetch(
new URL(`/schema/1/staged/status?${params}`, url),
const statusRes = await fetch(
new URL(`/schema/1/staged/status?${statusParams}`, url),
{
method: "GET",
headers: { AUTHORIZATION: `Bearer ${secret}` },
Expand All @@ -29,14 +33,62 @@ export default class StatusSchemaCommand extends SchemaCommand {
}
);

const json = await res.json();
if (json.error) {
this.error(json.error.message);
const statusJson = await statusRes.json();
if (statusJson.error) {
this.error(statusJson.error.message);
}

this.log(json.diff);
const validateParams = new URLSearchParams({
...(hasColor() ? { color: colorParam() } : {}),
diff: "summary",
staged: "true",
version: statusJson.version,
});
const validateRes = await fetch(
new URL(`/schema/1/validate?${validateParams}`, url),
{
method: "POST",
headers: { AUTHORIZATION: `Bearer ${secret}` },
body: this.body(files),
// https://github.com/nodejs/node/issues/46221
// https://github.com/microsoft/TypeScript-DOM-lib-generator/issues/1483
// @ts-expect-error-next-line
duplex: "half",
}
);

const validateJson = await validateRes.json();
if (validateJson.error) {
this.error(validateJson.error.message);
}

if (statusJson.status === "none") {
this.log(`Staged changes: ${bold()}none${reset()}`);
} else {
this.log(`Staged status: ${bold()}${statusJson.status}${reset()}`);
if (statusJson.pending_summary !== "") {
this.log(statusJson.pending_summary);
}
this.log("Staged changes:");
this.log();
this.log(" " + statusJson.diff.split("\n").join("\n "));

if (statusJson.status === "ready") {
this.log("(use `fauna schema commit` to commit staged changes)");
}
}

if (validateJson.diff === "") {
this.log(`Local changes: ${bold()}none${reset()}`);
} else {
this.log(`Local changes:`);
this.log();
this.log(" " + validateJson.diff.split("\n").join("\n "));

this.log("(use `fauna schema diff` to display local changes)");
this.log("(use `fauna schema push --staged` to stage local changes)");
}
} catch (err) {
console.log(err);
this.error(err);
}
}
Expand Down
17 changes: 13 additions & 4 deletions test/commands/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ describe("fauna schema diff test", () => {
});

describe("fauna schema push test", () => {
before(() => {
disableColor();
});

afterEach(() => {
nock.cleanAll();
});
Expand All @@ -134,9 +138,9 @@ describe("fauna schema push test", () => {
.persist()
.post("/", matchFqlReq(query.Now()))
.reply(200, new Date())
.post("/schema/1/validate?force=true")
.post("/schema/1/validate?force=true&diff=summary")
.reply(200, diff)
.post("/schema/1/update?version=0")
.post("/schema/1/update?version=0&staged=false")
.reply(200, updated);
// Stubbing the confirmation to always return true
const stubConfirm = sinon.stub(inquirer, "confirm").resolves(true);
Expand All @@ -153,7 +157,7 @@ describe("fauna schema push test", () => {
.persist()
.post("/", matchFqlReq(query.Now()))
.reply(200, new Date())
.post("/schema/1/validate?force=true")
.post("/schema/1/validate?force=true&diff=summary")
.reply(200, diff)
.post("/schema/1/update?version=0&staged=true")
.reply(200, updated);
Expand All @@ -173,11 +177,16 @@ describe("fauna schema push test", () => {
.persist()
.post("/", matchFqlReq(query.Now()))
.reply(200, new Date())
.get("/schema/1/staged/status?diff=true")
.get("/schema/1/staged/status?diff=summary")
.reply(200, {
version: 0,
status: "ready",
diff: diff.diff,
})
.post("/schema/1/validate?diff=summary&staged=true&version=0")
.reply(200, {
version: 0,
diff: diff.diff,
});

// Stubbing the confirmation to always return true
Expand Down