diff --git a/src/commands/schema/push.ts b/src/commands/schema/push.ts index 87f19f4b..f17a7aa8 100644 --- a/src/commands/schema/push.ts +++ b/src/commands/schema/push.ts @@ -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, { @@ -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, diff --git a/src/commands/schema/status.ts b/src/commands/schema/status.ts index 6315db6d..a7b1eb86 100644 --- a/src/commands/schema/status.ts +++ b/src/commands/schema/status.ts @@ -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 = { @@ -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}` }, @@ -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); } } diff --git a/test/commands/schema.test.ts b/test/commands/schema.test.ts index dadd0013..2588b4e5 100644 --- a/test/commands/schema.test.ts +++ b/test/commands/schema.test.ts @@ -125,6 +125,10 @@ describe("fauna schema diff test", () => { }); describe("fauna schema push test", () => { + before(() => { + disableColor(); + }); + afterEach(() => { nock.cleanAll(); }); @@ -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); @@ -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); @@ -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