Commit 4893344d authored by Hasang Cho's avatar Hasang Cho
Browse files

fix(cli): Resolve circular dependency and refactor helper function

- Renamed the function from formatObjectForOutput to formatCliObjectOutputWithColor
- Moved the formatObjectForOutput function and colorEnabled constant to a utils file
parent af459424
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -34,6 +34,7 @@ import { ActivityEntryPage, ActivityListPage } from "./inbox/view.tsx";
import { recordingSink } from "./log.ts";
import { tableStyle } from "./table.ts";
import { spawnTemporaryServer, type TemporaryServer } from "./tempserver.ts";
import { colorEnabled } from "./utils.ts";

/**
 * Context data for the ephemeral ActivityPub inbox server.
+2 −2
Original line number Diff line number Diff line
@@ -18,8 +18,8 @@ import * as colors from "@std/fmt/colors";
import { dirname, isAbsolute, resolve } from "@std/path";
import ora from "ora";
import { getContextLoader, getDocumentLoader } from "./docloader.ts";
import { colorEnabled, formatObjectForOutput } from "./mod.ts";
import { spawnTemporaryServer, type TemporaryServer } from "./tempserver.ts";
import { colorEnabled, formatCliObjectOutputWithColor } from "./utils.ts";

const logger = getLogger(["fedify", "cli", "lookup"]);

@@ -109,7 +109,7 @@ export async function writeObjectToStream(
    }

    const enableColors = colorEnabled && options.output === undefined;
    content = formatObjectForOutput(content, enableColors);
    content = formatCliObjectOutputWithColor(content, enableColors);

    const encoder = new TextEncoder();
    const bytes = encoder.encode(content + "\n");
+1 −12
Original line number Diff line number Diff line
@@ -11,22 +11,11 @@ import { logFile, recordingSink } from "./log.ts";
import { command as lookup } from "./lookup.ts";
import { command as nodeinfo } from "./nodeinfo.ts";
import { command as tunnel } from "./tunnel.ts";
import { colorEnabled } from "./utils.ts";
import { command as webfinger } from "./webfinger.ts";

export const colorEnabled: boolean = Deno.stdout.isTerminal() &&
  !Deno.env.has("NO_COLOR");
setColorEnabled(colorEnabled);

export function formatObjectForOutput(obj: unknown, colors?: boolean): string {
  const enableColors = colors ?? colorEnabled;

  if (enableColors) {
    return Deno.inspect(obj, { colors: true });
  } else {
    return Deno.inspect(obj, { colors: false });
  }
}

const command = new Command()
  .name("fedify")
  .version(metadata.version)
+4 −3
Original line number Diff line number Diff line
@@ -7,8 +7,7 @@ import * as colors from "@std/fmt/colors";
import { isICO, parseICO } from "icojs";
import { defaultFormats, defaultPlugins, intToRGBA } from "jimp";
import ora from "ora";
import { formatObjectForOutput } from "./mod.ts";
import { printJson } from "./utils.ts";
import { formatCliObjectOutputWithColor, printJson } from "./utils.ts";

const logger = getLogger(["fedify", "cli", "nodeinfo"]);

@@ -213,7 +212,9 @@ export const command = new Command()
      for (const [key, value] of Object.entries(nodeInfo.metadata)) {
        layout[next()] += `  ${colors.dim(key + ":")} ${
          indent(
            typeof value === "string" ? value : formatObjectForOutput(value),
            typeof value === "string"
              ? value
              : formatCliObjectOutputWithColor(value),
            defaultWidth + 4 + key.length,
          )
        }`;
+11 −0
Original line number Diff line number Diff line
@@ -4,3 +4,14 @@ export function printJson(json: unknown): void {
  const formatted = JSON.stringify(json, null, 2);
  console.log(highlight(formatted, { language: "json" }));
}

export const colorEnabled: boolean = Deno.stdout.isTerminal() &&
  !Deno.env.has("NO_COLOR");

export function formatCliObjectOutputWithColor(
  obj: unknown,
  colors?: boolean,
): string {
  const enableColors = colors ?? colorEnabled;
  return Deno.inspect(obj, { colors: enableColors });
}