Skip to content

Commit

Permalink
Add basic CLI interface (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vylpes authored Oct 27, 2023
1 parent 093ff19 commit af92755
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
"placeholder"
],
"dependencies": {
"commander": "^11.1.0",
"glob-parent": "^6.0.0",
"got-cjs": "^12.5.4",
"linqts": "^1.14.4"
},
"scripts": {
"build": "tsc",
"start": "ts-node app.ts",
"cli": "ts-node src/cli.ts",
"test": "jest",
"lint": "eslint .",
"release": "np --no-publish"
Expand Down
9 changes: 9 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Command } from "commander";
const program = new Command();

program
.name('random-bunny')
.description('Get a random image url from a subreddit of your choosing')
.version('2.2');

program.parse();
54 changes: 54 additions & 0 deletions tests/cli.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { exec } from "child_process";
import path from "path";

describe('version', () => {
test('GIVEN -V flag is supplied, EXPECT version returned', async () => {
const result = await cli(['-V'], '.');

expect(result.code).toBe(0);
expect(result.stdout).toBe('2.2\n');
});

test('GIVEN --version is supplied, EXPECT version returned', async () => {
const result = await cli(['--version'], '.');

expect(result.code).toBe(0);
expect(result.stdout).toBe('2.2\n');
});
});

describe('help', () => {
test('GIVEN -h is supplied, EXPECT help returned', async () => {
const result = await cli(['-h'], '.');

expect(result.code).toBe(0);
expect(result.stdout.split('\n')[0]).toBe('Usage: random-bunny [options]');
});

test('GIVEN --help is supplied, EXPECT help returned', async () => {
const result = await cli(['--help'], '.');

expect(result.code).toBe(0);
expect(result.stdout.split('\n')[0]).toBe('Usage: random-bunny [options]');
});
})

function cli(args: string[], cwd: string): Promise<cliResult> {
return new Promise(resolve => {
exec(`node ${path.resolve('./dist/cli.js')} ${args.join(' ')}`,
{ cwd },
(error, stdout, stderr) => { resolve({
code: error && error.code ? error.code : 0,
error,
stdout,
stderr });
});
});
}

interface cliResult {
code: number,
error: any,
stdout: string,
stderr: string,
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1585,6 +1585,11 @@ commander-version@^1.1.0:
"@bconnorwhite/module" "^2.0.2"
commander "^6.1.0"

commander@^11.1.0:
version "11.1.0"
resolved "https://registry.yarnpkg.com/commander/-/commander-11.1.0.tgz#62fdce76006a68e5c1ab3314dc92e800eb83d906"
integrity sha512-yPVavfyCcRhmorC7rWlkHn15b4wDVgVmBA7kV4QVBsF7kv/9TKJAbAXVTxvTnwP8HHKjRCJDClKbciiYS7p0DQ==

commander@^6.1.0:
version "6.2.1"
resolved "https://registry.yarnpkg.com/commander/-/commander-6.2.1.tgz#0792eb682dfbc325999bb2b84fddddba110ac73c"
Expand Down

0 comments on commit af92755

Please sign in to comment.