diff --git a/src/lib/fauna-client.mjs b/src/lib/fauna-client.mjs index 59a756cc..737902a0 100644 --- a/src/lib/fauna-client.mjs +++ b/src/lib/fauna-client.mjs @@ -1,12 +1,18 @@ //@ts-check +import stripAnsi from "strip-ansi"; + import { container } from "../cli.mjs"; import { isUnknownError } from "./errors.mjs"; import { faunaToCommandError } from "./fauna.mjs"; import { faunadbToCommandError } from "./faunadb.mjs"; import { colorize, Format } from "./formatting/colorize.mjs"; -const SUMMARY_FQL_REGEX = /^(\s\s\|)|(\d\s\|)/; +/** + * Regex to match the FQL diagnostic line. + * @type {RegExp} + */ +export const FQL_DIAGNOSTIC_REGEX = /^(\s{2,}\|)|(\s*\d{1,}\s\|)/; /** * Gets a secret for the current credentials. @@ -168,7 +174,7 @@ export const formatQuerySummary = (summary) => { try { const lines = summary.split("\n").map((line) => { - if (!line.match(SUMMARY_FQL_REGEX)) { + if (!line.match(FQL_DIAGNOSTIC_REGEX)) { return line; } return colorize(line, { format: Format.FQL }); @@ -228,14 +234,16 @@ export const formatQueryInfo = (response, { apiVersion, color, include }) => { if (Object.keys(queryInfoToDisplay).length === 0) return ""; - const SUMMARY_IN_QUERY_INFO_FQL_REGEX = /^(\s\s\s\s\|)|(\d\s\|)/; + // We colorize the entire query info object as YAML, but then need to + // colorize the diagnostic lines individually. To simplify this, we + // strip the ansi when we're checking if the line is a diagnostic line. const colorized = colorize(queryInfoToDisplay, { color, format: Format.YAML, }) .split("\n") .map((line) => { - if (!line.match(SUMMARY_IN_QUERY_INFO_FQL_REGEX)) { + if (!stripAnsi(line).match(FQL_DIAGNOSTIC_REGEX)) { return line; } return colorize(line, { format: Format.FQL }); diff --git a/test/lib/fauna-client.mjs b/test/lib/fauna-client.mjs new file mode 100644 index 00000000..fb71603c --- /dev/null +++ b/test/lib/fauna-client.mjs @@ -0,0 +1,30 @@ +import { expect } from "chai"; + +import { FQL_DIAGNOSTIC_REGEX } from "../../src/lib/fauna-client.mjs"; + +describe("FQL_DIAGNOSTIC_REGEX", () => { + const validLines = ["1 |", "12 |", "123 |", " |", " |", " |", " 1 |"]; + + const invalidLines = [ + "normal text", + "1 |", + "| invalid", + "abc |", + "|", + "1|", + " | ", + "text | more", + ]; + + validLines.forEach((line) => { + it(`should match diagnostic line: "${line}"`, () => { + expect(line).to.match(FQL_DIAGNOSTIC_REGEX); + }); + }); + + invalidLines.forEach((line) => { + it(`should not match non-diagnostic line: "${line}"`, () => { + expect(line).to.not.match(FQL_DIAGNOSTIC_REGEX); + }); + }); +});