Skip to content
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
6 changes: 5 additions & 1 deletion src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import pc from "picocolors";
import { ExitCode } from "./exit-code";
import { parseArgs } from "./parse-args";
import type { CliOptions } from "./types";
import { symbols, ui } from "./ui";
import { setSilentMode, symbols, ui } from "./ui";

export const CLI_NAME = "docs-cache";

Expand Down Expand Up @@ -33,6 +33,7 @@ Global options:
--concurrency <n>
--json
--timeout-ms <n>
--silent
`;

const printHelp = () => {
Expand Down Expand Up @@ -324,6 +325,9 @@ export async function main(): Promise<void> {
const parsed = parseArgs();
const _rawArgs = parsed.rawArgs;

// Set silent mode if the flag is present
setSilentMode(parsed.options.silent);

if (parsed.help) {
printHelp();
process.exit(ExitCode.Success);
Expand Down
2 changes: 2 additions & 0 deletions src/cli/parse-args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export const parseArgs = (argv = process.argv): ParsedArgs => {
.option("--concurrency <n>", "Concurrency limit")
.option("--json", "Output JSON")
.option("--timeout-ms <n>", "Network timeout in milliseconds")
.option("--silent", "Suppress non-error output")
.help();

const result = cli.parse(argv, { run: false });
Expand All @@ -64,6 +65,7 @@ export const parseArgs = (argv = process.argv): ParsedArgs => {
timeoutMs: result.options.timeoutMs
? Number(result.options.timeoutMs)
: undefined,
silent: Boolean(result.options.silent),
};

if (options.concurrency !== undefined && options.concurrency < 1) {
Expand Down
1 change: 1 addition & 0 deletions src/cli/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export type CliOptions = {
concurrency?: number;
json: boolean;
timeoutMs?: number;
silent: boolean;
};
14 changes: 13 additions & 1 deletion src/cli/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ export const symbols = {
warn: pc.yellow("⚠"),
};

let _silentMode = false;

export const setSilentMode = (silent: boolean) => {
_silentMode = silent;
};

export const ui = {
// Formatters
path: (value: string) => {
Expand All @@ -24,19 +30,25 @@ export const ui = {
pad: (value: string, length: number) => value.padEnd(length),

// Components
line: (text: string = "") => process.stdout.write(`${text}\n`),
line: (text: string = "") => {
if (_silentMode) return;
process.stdout.write(`${text}\n`);
},

header: (label: string, value: string) => {
if (_silentMode) return;
process.stdout.write(`${pc.blue("ℹ")} ${label.padEnd(10)} ${value}\n`);
},

item: (icon: string, label: string, details?: string) => {
if (_silentMode) return;
const partLabel = pc.bold(label);
const partDetails = details ? pc.gray(details) : "";
process.stdout.write(` ${icon} ${partLabel} ${partDetails}\n`);
},

step: (action: string, subject: string, details?: string) => {
if (_silentMode) return;
const icon = pc.cyan("→");
process.stdout.write(
` ${icon} ${action} ${pc.bold(subject)}${details ? ` ${pc.dim(details)}` : ""}\n`,
Expand Down
24 changes: 24 additions & 0 deletions tests/cli-parse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,27 @@ test("parseArgs handles ssh repo only", async (t) => {
"git@github.com:fbosch/docs-cache.git",
]);
});

test("parseArgs handles --silent flag", async (t) => {
const module = await loadCliModule();
if (!module) {
t.skip("CLI not built yet");
return;
}
const result = module.parseArgs(["node", "docs-cache", "status", "--silent"]);

assert.equal(result.command, "status");
assert.equal(result.options.silent, true);
});

test("parseArgs silent flag defaults to false", async (t) => {
const module = await loadCliModule();
if (!module) {
t.skip("CLI not built yet");
return;
}
const result = module.parseArgs(["node", "docs-cache", "status"]);

assert.equal(result.command, "status");
assert.equal(result.options.silent, false);
});
Loading