Skip to content

Commit

Permalink
Request colors from core
Browse files Browse the repository at this point in the history
  • Loading branch information
macmv committed Sep 24, 2024
1 parent 9e4afd6 commit c62c003
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 10 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"object-sizeof": "^1.6.1",
"prettier": "^2.3.0",
"rate-limiter-flexible": "^2.3.6",
"stream-json": "^1.7.3"
"stream-json": "^1.7.3",
"supports-color": "^8"
},
"devDependencies": {
"@babel/eslint-parser": "^7.17.0",
Expand All @@ -39,6 +40,7 @@
"@types/mocha": "^10.0.1",
"@types/node": "^20.6.0",
"@types/sinon": "^17.0.3",
"@types/supports-color": "^8.1.3",
"@typescript-eslint/parser": "6.7.3",
"c8": "^8.0.1",
"chai": "^4.2.0",
Expand Down
8 changes: 6 additions & 2 deletions src/commands/schema/abandon.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { confirm } from "@inquirer/prompts";
import SchemaCommand from "../../lib/schema-command";
import { Flags } from "@oclif/core";
import { colorParam } from "../../lib/color";

export default class CommitSchemaCommand extends SchemaCommand {
static flags = {
Expand Down Expand Up @@ -41,9 +42,12 @@ export default class CommitSchemaCommand extends SchemaCommand {
this.log("Schema has been abandonded");
} else {
// Show status to confirm.
const { url, secret } = await this.fetchsetup();
const params = new URLSearchParams({
color: colorParam(),
diff: "true",
});
const res = await fetch(
new URL("/schema/1/staged/status?diff=true", url),
new URL(`/schema/1/staged/status?${params}`, url),
{
method: "GET",
headers: { AUTHORIZATION: `Bearer ${secret}` },
Expand Down
8 changes: 6 additions & 2 deletions src/commands/schema/commit.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { confirm } from "@inquirer/prompts";
import SchemaCommand from "../../lib/schema-command";
import { Flags } from "@oclif/core";
import { colorParam } from "../../lib/color";

export default class CommitSchemaCommand extends SchemaCommand {
static flags = {
Expand Down Expand Up @@ -41,9 +42,12 @@ export default class CommitSchemaCommand extends SchemaCommand {
this.log("Schema has been committed");
} else {
// Show status to confirm.
const { url, secret } = await this.fetchsetup();
const params = new URLSearchParams({
color: colorParam(),
diff: "true",
});
const res = await fetch(
new URL("/schema/1/staged/status?diff=true", url),
new URL(`/schema/1/staged/status?${params}`, url),
{
method: "GET",
headers: { AUTHORIZATION: `Bearer ${secret}` },
Expand Down
7 changes: 6 additions & 1 deletion src/commands/schema/diff.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import SchemaCommand from "../../lib/schema-command";
import { colorParam } from "../../lib/color";

export default class DiffSchemaCommand extends SchemaCommand {
static flags = {
Expand All @@ -16,7 +17,11 @@ export default class DiffSchemaCommand extends SchemaCommand {
const files = this.read(fps);
try {
const { url, secret } = await this.fetchsetup();
const res = await fetch(new URL("/schema/1/validate?force=true", url), {
const params = new URLSearchParams({
color: colorParam(),
force: "true",
});
const res = await fetch(new URL(`/schema/1/validate?${params}`, url), {
method: "POST",
headers: { AUTHORIZATION: `Bearer ${secret}` },
body: this.body(files),
Expand Down
2 changes: 2 additions & 0 deletions src/commands/schema/push.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { confirm } from "@inquirer/prompts";
import SchemaCommand from "../../lib/schema-command";
import { Flags } from "@oclif/core";
import { colorParam } from "../../lib/color";

export default class PushSchemaCommand extends SchemaCommand {
static flags = {
Expand Down Expand Up @@ -55,6 +56,7 @@ export default class PushSchemaCommand extends SchemaCommand {
// Confirm diff, then push it. `force` is set on `validate` so we don't
// need to pass the last known schema version through.
const params = new URLSearchParams({
color: colorParam(),
force: "true",
});
const path = new URL(`/schema/1/validate?${params}`, url);
Expand Down
8 changes: 7 additions & 1 deletion src/commands/schema/status.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import SchemaCommand from "../../lib/schema-command";
import { colorParam } from "../../lib/color";

export default class StatusSchemaCommand extends SchemaCommand {
static flags = {
Expand All @@ -11,8 +12,13 @@ export default class StatusSchemaCommand extends SchemaCommand {
async run() {
try {
const { url, secret } = await this.fetchsetup();

const params = new URLSearchParams({
color: colorParam(),
diff: "true",
});
const res = await fetch(
new URL("/schema/1/staged/status?diff=true", url),
new URL(`/schema/1/staged/status?${params}`, url),
{
method: "GET",
headers: { AUTHORIZATION: `Bearer ${secret}` },
Expand Down
10 changes: 10 additions & 0 deletions src/lib/color.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { stdout } from "supports-color";

// The value for the `color` parameter to the `/schema/1` endpoints.
export const colorParam = (): string => {
return hasColor() ? "ansi" : "";
};

export const hasColor = (): boolean => {
return stdout !== false && stdout.hasBasic;
};
9 changes: 9 additions & 0 deletions src/lib/fauna-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,15 @@ FaunaCommand.flags = {
environment: Flags.string({
description: "Environment to use, from a Fauna project",
}),

// FIXME(neil): Currently, the library `supports-color` parses out this flag,
// and enables or disables collor accordingly. This is somewhat horrible, but
// plumbing this flag down to all the commands would be more of a pain.
// So I'm going to leave it as-is, but this is quite brittle.
color: Flags.boolean({
description: "Force color output",
allowNo: true,
}),
};

export default FaunaCommand;
36 changes: 33 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1576,6 +1576,11 @@
resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-2.0.3.tgz#6209321eb2c1712a7e7466422b8cb1fc0d9dd5d8"
integrity sha512-9aEbYZ3TbYMznPdcdr3SmIrLXwC/AKZXQeCf9Pgao5CKb8CyHuEX5jzWPTkvregvhRJHcpRO6BFoGW9ycaOkYw==

"@types/supports-color@^8.1.3":
version "8.1.3"
resolved "https://registry.yarnpkg.com/@types/supports-color/-/supports-color-8.1.3.tgz#b769cdce1d1bb1a3fa794e35b62c62acdf93c139"
integrity sha512-Hy6UMpxhE3j1tLpl27exp1XqHD7n8chAiNPzWfz16LPZoMMoSc4dzLl6w9qijkEb/r5O1ozdu1CWGA2L83ZeZg==

"@types/vinyl@^2.0.4":
version "2.0.12"
resolved "https://registry.yarnpkg.com/@types/vinyl/-/vinyl-2.0.12.tgz#17642ca9a8ae10f3db018e9f885da4188db4c6e6"
Expand Down Expand Up @@ -6468,7 +6473,16 @@ string-length@^4.0.1:
char-regex "^1.0.2"
strip-ansi "^6.0.0"

"string-width-cjs@npm:string-width@^4.2.0", "string-width@^1.0.2 || 2 || 3 || 4", string-width@^4.0.0, string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.2, string-width@^4.2.3:
"string-width-cjs@npm:string-width@^4.2.0":
version "4.2.3"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
dependencies:
emoji-regex "^8.0.0"
is-fullwidth-code-point "^3.0.0"
strip-ansi "^6.0.1"

"string-width@^1.0.2 || 2 || 3 || 4", string-width@^4.0.0, string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.2, string-width@^4.2.3:
version "4.2.3"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
Expand Down Expand Up @@ -6500,7 +6514,7 @@ string_decoder@~1.1.1:
dependencies:
safe-buffer "~5.1.0"

"strip-ansi-cjs@npm:strip-ansi@^6.0.1", strip-ansi@^6.0.0, strip-ansi@^6.0.1:
"strip-ansi-cjs@npm:strip-ansi@^6.0.1":
version "6.0.1"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
Expand All @@ -6514,6 +6528,13 @@ strip-ansi@^5.0.0:
dependencies:
ansi-regex "^4.1.0"

strip-ansi@^6.0.0, strip-ansi@^6.0.1:
version "6.0.1"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
dependencies:
ansi-regex "^5.0.1"

strip-ansi@^7.0.1:
version "7.1.0"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-7.1.0.tgz#d5b6568ca689d8561370b0707685d22434faff45"
Expand Down Expand Up @@ -7046,7 +7067,7 @@ workerpool@^6.5.1:
resolved "https://registry.yarnpkg.com/workerpool/-/workerpool-6.5.1.tgz#060f73b39d0caf97c6db64da004cd01b4c099544"
integrity sha512-Fs4dNYcsdpYSAfVxhnl1L5zTksjvOJxtC5hzMNl+1t9B8hTJTdKDyZ5ju7ztgPy+ft9tBFXoOlDNiOT9WUXZlA==

"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0", wrap-ansi@^7.0.0:
"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0":
version "7.0.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
Expand All @@ -7064,6 +7085,15 @@ wrap-ansi@^6.0.1, wrap-ansi@^6.2.0:
string-width "^4.1.0"
strip-ansi "^6.0.0"

wrap-ansi@^7.0.0:
version "7.0.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
dependencies:
ansi-styles "^4.0.0"
string-width "^4.1.0"
strip-ansi "^6.0.0"

wrap-ansi@^8.1.0:
version "8.1.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-8.1.0.tgz#56dc22368ee570face1b49819975d9b9a5ead214"
Expand Down

0 comments on commit c62c003

Please sign in to comment.