Skip to content
Open
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
9 changes: 7 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1857,10 +1857,15 @@ cli({
const daemonCmd = program.command('daemon').description('Manage the webcmd daemon');
// Snapshot before applyRootSubcommandSummaries() rewrites .description() to a child-name listing.
const originalDaemonDescription = daemonCmd.description();
daemonCmd
const daemonStatusCmd = daemonCmd
.command('status')
.description('Show daemon status')
.action(async () => { await daemonStatus(); });
.option('-f, --format <fmt>', OUTPUT_FORMAT_HELP, 'table');
daemonStatusCmd.action(async (opts) => {
const fmt = resolveOutputFormat(opts.format);
if (fmt === null) return;
await daemonStatus(fmt);
});
daemonCmd
.command('stop')
.description('Stop the daemon')
Expand Down
59 changes: 59 additions & 0 deletions src/commands/daemon.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import yaml from 'js-yaml';

const {
fetchDaemonStatusMock,
Expand Down Expand Up @@ -105,6 +106,64 @@ describe('daemonStatus', () => {

expect(stdoutSpy).toHaveBeenCalledWith(expect.stringContaining('fake connected'));
});

it('renders a structured envelope for -f json when running (#175)', async () => {
fetchDaemonStatusMock.mockResolvedValue({
ok: true,
pid: 12345,
uptime: 60,
daemonVersion: PKG_VERSION,
runtimeConnected: true,
runtimeName: 'fake',
pending: 0,
memoryMB: 64,
port: 9777,
});

await daemonStatus('json');

const printed = stdoutSpy.mock.calls.map((c: unknown[]) => c[0]).join('\n');
const data = JSON.parse(printed);
expect(data).toMatchObject({ running: true, pid: 12345, port: 9777 });
});

it('renders a structured envelope for -f json when not running (#175)', async () => {
fetchDaemonStatusMock.mockResolvedValue(null);

await daemonStatus('json');

const printed = stdoutSpy.mock.calls.map((c: unknown[]) => c[0]).join('\n');
expect(JSON.parse(printed)).toEqual({ running: false });
});

it('renders a structured envelope for -f yaml when running (#175)', async () => {
fetchDaemonStatusMock.mockResolvedValue({
ok: true,
pid: 12345,
uptime: 60,
daemonVersion: PKG_VERSION,
runtimeConnected: true,
runtimeName: 'fake',
pending: 0,
memoryMB: 64,
port: 9777,
});

await daemonStatus('yaml');

const printed = stdoutSpy.mock.calls.map((c: unknown[]) => c[0]).join('\n');
const data = yaml.load(printed) as Record<string, unknown>;
expect(data).toMatchObject({ running: true, pid: 12345, port: 9777 });
});

it('renders a structured envelope for -f yaml when not running (#175)', async () => {
fetchDaemonStatusMock.mockResolvedValue(null);

await daemonStatus('yaml');

const printed = stdoutSpy.mock.calls.map((c: unknown[]) => c[0]).join('\n');
expect(yaml.load(printed)).toEqual({ running: false });
});
});

// ────────────────────────────────────────────────────────────────────
Expand Down
7 changes: 6 additions & 1 deletion src/commands/daemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@ import { formatDuration } from '../download/progress.js';
import { log } from '../logger.js';
import { PKG_VERSION } from '../version.js';
import { formatDaemonVersion, isDaemonStale } from '../browser/daemon-version.js';
import { render } from '../output.js';

export async function daemonStatus(): Promise<void> {
export async function daemonStatus(fmt: string = 'table'): Promise<void> {
const status = await fetchDaemonStatus();
if (fmt !== 'table') {
await render(status ? { running: true, ...status } : { running: false }, { fmt });
return;
}
if (!status) {
console.log('Daemon: not running');
return;
Expand Down