Skip to content

Commit d6b919d

Browse files
committed
feat(mongodb-runner): add mongodb-runner ls --json
Provide JSON-formatted output that contains serialized cluster data.
1 parent 63b1749 commit d6b919d

5 files changed

Lines changed: 42 additions & 9 deletions

File tree

packages/mongodb-runner/src/cli.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ import type { MongoClientOptions } from 'mongodb';
7979
})
8080
.option('debug', { type: 'boolean', describe: 'Enable debug output' })
8181
.option('verbose', { type: 'boolean', describe: 'Enable verbose output' })
82+
.option('json', {
83+
type: 'boolean',
84+
describe: 'Output machine-readable JSON ("ls" only)',
85+
})
8286
.command('start', 'Start a MongoDB instance')
8387
.command('stop', 'Stop a MongoDB instance')
8488
.command('prune', 'Clean up metadata for any dead MongoDB instances')
@@ -149,8 +153,16 @@ import type { MongoClientOptions } from 'mongodb';
149153
}
150154

151155
async function ls() {
152-
for await (const { id, connectionString } of utilities.instances(argv)) {
153-
console.log(`${id}: ${connectionString}`);
156+
if (argv.json) {
157+
console.log('[');
158+
for await (const { serialized, ...rest } of utilities.instances(argv)) {
159+
console.log(JSON.stringify({ ...rest, ...serialized }, null, 2));
160+
}
161+
console.log(']');
162+
} else {
163+
for await (const { id, connectionString } of utilities.instances(argv)) {
164+
console.log(`${id}: ${connectionString}`);
165+
}
154166
}
155167
}
156168

packages/mongodb-runner/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export {
22
MongoServer,
33
type MongoServerEvents,
44
MongoServerOptions,
5+
SerializedServerProperties as MongoServerSerializedProperties,
56
} from './mongoserver';
67
export {
78
MongoCluster,
@@ -12,6 +13,7 @@ export {
1213
RSOptions as MongoClusterRSOptions,
1314
CommonOptions as MongoClusterCommonOptions,
1415
ShardedOptions as MongoClusterShardedOptions,
16+
SerializedClusterProperties as MongoClusterSerializedProperties,
1517
} from './mongocluster';
1618
export type { LogEntry } from './mongologreader';
1719
export type { ConnectionString } from 'mongodb-connection-string-url';

packages/mongodb-runner/src/mongocluster.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import type { MongoServerEvents, MongoServerOptions } from './mongoserver';
1+
import type {
2+
MongoServerEvents,
3+
MongoServerOptions,
4+
SerializedServerProperties,
5+
} from './mongoserver';
26
import { MongoServer } from './mongoserver';
37
import { ConnectionString } from 'mongodb-connection-string-url';
48
import type { DownloadOptions } from '@mongodb-js/mongodb-downloader';
@@ -162,6 +166,16 @@ export type MongoClusterEvents = {
162166
removeListener: [keyof MongoClusterEvents];
163167
};
164168

169+
export interface SerializedClusterProperties {
170+
topology: MongoClusterOptions['topology'];
171+
replSetName?: string;
172+
servers: SerializedServerProperties[];
173+
shards: SerializedClusterProperties[];
174+
oidcMockProviderProcess?: ReturnType<OIDCMockProviderProcess['serialize']>;
175+
defaultConnectionOptions: Partial<MongoClientOptions>;
176+
users: MongoDBUserDoc[];
177+
}
178+
165179
function removePortArg([...args]: string[]): string[] {
166180
let portArgIndex = -1;
167181
if ((portArgIndex = args.indexOf('--port')) !== -1) {
@@ -309,7 +323,7 @@ export class MongoCluster extends EventEmitter<MongoClusterEvents> {
309323
});
310324
}
311325

312-
serialize(): unknown /* JSON-serializable */ {
326+
serialize(): SerializedClusterProperties {
313327
return {
314328
topology: this.topology,
315329
replSetName: this.replSetName,
@@ -328,7 +342,9 @@ export class MongoCluster extends EventEmitter<MongoClusterEvents> {
328342
return true;
329343
}
330344

331-
static async deserialize(serialized: any): Promise<MongoCluster> {
345+
static async deserialize(
346+
serialized: SerializedClusterProperties,
347+
): Promise<MongoCluster> {
332348
const cluster = new MongoCluster();
333349
cluster.topology = serialized.topology;
334350
cluster.replSetName = serialized.replSetName;

packages/mongodb-runner/src/mongoserver.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export interface MongoServerOptions {
5353
keyFileContents?: string;
5454
}
5555

56-
interface SerializedServerProperties {
56+
export interface SerializedServerProperties {
5757
_id: string;
5858
pid?: number;
5959
port?: number;

packages/mongodb-runner/src/runner-helpers.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { BSON } from 'mongodb';
22
import path from 'path';
3-
import type { MongoClusterOptions } from './mongocluster';
3+
import type {
4+
MongoClusterOptions,
5+
SerializedClusterProperties,
6+
} from './mongocluster';
47
import { MongoCluster } from './mongocluster';
58
import { parallelForEach } from './util';
69
import * as fs from 'fs/promises';
@@ -10,7 +13,7 @@ import { once } from 'events';
1013
interface StoredInstance {
1114
id: string;
1215
filepath: string;
13-
serialized: string;
16+
serialized: SerializedClusterProperties;
1417
connectionString: string;
1518
}
1619

@@ -31,7 +34,7 @@ export async function start(
3134
...argv,
3235
args,
3336
});
34-
const serialized = await cluster.serialize();
37+
const serialized = cluster.serialize();
3538
const { connectionString } = cluster;
3639

3740
await fs.writeFile(

0 commit comments

Comments
 (0)