From 38054d38129178f3f4aa6382b7dabeb2b19e6b66 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Thu, 7 Dec 2023 18:14:52 +0000 Subject: [PATCH 1/3] Add JSON flag --- src/cli.ts | 9 ++++++++- src/contracts/ICliOptions.ts | 1 + 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/cli.ts b/src/cli.ts index 0de5ab3..4989718 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -9,7 +9,8 @@ program .name('random-bunny') .description('Get a random image url from a subreddit of your choosing') .version('2.2') - .option('-s, --subreddit ', 'The subreddit to search', 'rabbits'); + .option('-s, --subreddit ', 'The subreddit to search', 'rabbits') + .option('-j, --json', 'Output as JSON'); program.parse(); @@ -21,6 +22,12 @@ randomBunny(options.subreddit) const result = response.Result!; const outputLines: string[] = []; + + if (options.json) { + console.log(JSON.stringify(result)); + return; + } + outputLines.push(`Archived = ${result.Archived}`); outputLines.push(`Downvotes = ${result.Downs}`); outputLines.push(`Hidden = ${result.Hidden}`); diff --git a/src/contracts/ICliOptions.ts b/src/contracts/ICliOptions.ts index c85971d..5aab9da 100644 --- a/src/contracts/ICliOptions.ts +++ b/src/contracts/ICliOptions.ts @@ -1,3 +1,4 @@ export default interface ICliOptions { subreddit: string, + json?: boolean, } \ No newline at end of file -- 2.43.4 From b2722b487ea8c7cecc682616a3d7601eb37351e9 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Thu, 7 Dec 2023 18:18:46 +0000 Subject: [PATCH 2/3] Update tests --- tests/cli.test.ts | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/tests/cli.test.ts b/tests/cli.test.ts index 6d105d3..79d30b6 100644 --- a/tests/cli.test.ts +++ b/tests/cli.test.ts @@ -88,7 +88,25 @@ describe('subreddit', () => { expect(subreddit).toBe('Horses'); }, 5000); -}) +}); + +describe('json', () => { + test('GIVEN -j is supplied, EXPECT output to be valid JSON', async () => { + const result = await cli(['-j'], '.'); + + const json = JSON.parse(result.stdout); + + expect(json).toBeDefined(); + }, 5000); + + test('GIVEN --json is supplied, EXPECT output to be valid JSON', async () => { + const result = await cli(['--json'], '.'); + + const json = JSON.parse(result.stdout); + + expect(json).toBeDefined(); + }, 5000); +}); function cli(args: string[], cwd: string): Promise { return new Promise(resolve => { -- 2.43.4 From 55c11761be5d729cefc31c90f9c615febbbc7de8 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Wed, 17 Jan 2024 18:18:15 +0000 Subject: [PATCH 3/3] Fix tests --- tests/cli.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/cli.test.ts b/tests/cli.test.ts index b1b50bc..9671235 100644 --- a/tests/cli.test.ts +++ b/tests/cli.test.ts @@ -105,7 +105,7 @@ describe('json', () => { const json = JSON.parse(result.stdout); expect(json).toBeDefined(); - }); + }, 5000); }); describe('sort', () => { @@ -134,7 +134,7 @@ describe('sort', () => { expect(result.code).toBe(1); expect(result.stderr).toBe("error: option '--sort ' argument 'invalid' is invalid. Allowed choices are hot, new, top.\n"); - }); + }, 5000); }); describe('query-metadata', () => { -- 2.43.4