Skip to content

Commit

Permalink
feat: provide better raw output handling (#16)
Browse files Browse the repository at this point in the history
* chore(deps): bump sdk to version 1.2.1

* lib(util): add data output utility

* feat: add `--pretty` option for json output

* refractor: replace raw output strategy

* chore(release): bump package version to 1.3.0
  • Loading branch information
fabio-nettis authored Apr 17, 2024
1 parent 26c56e1 commit c5277ee
Show file tree
Hide file tree
Showing 11 changed files with 23 additions and 10 deletions.
Binary file modified bun.lockb
Binary file not shown.
3 changes: 2 additions & 1 deletion commands/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { Command } from "commander";
import HellHub, { type GlobalEvent } from "@hellhub-collective/sdk";

import ascii from "utils/ascii";
import output from "utils/output";
import request from "utils/request";
import interval from "utils/interval";
import { createListCommand, parseListOptions } from "utils/options";
Expand Down Expand Up @@ -54,7 +55,7 @@ export default function events(program: Command) {
});

if (!!args[1].raw) {
console.log(data);
output(data, !!args[1].pretty);
process.exit(0);
}

Expand Down
3 changes: 2 additions & 1 deletion commands/major.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { Command } from "commander";
import HellHub, { type Assignment } from "@hellhub-collective/sdk";

import ascii from "utils/ascii";
import output from "utils/output";
import request from "utils/request";
import interval from "utils/interval";
import { createListCommand, parseListOptions } from "utils/options";
Expand All @@ -23,7 +24,7 @@ export default function major(program: Command) {
});

if (!!args[1].raw) {
console.log(data);
output(data, !!args[1].pretty);
process.exit(0);
}

Expand Down
3 changes: 2 additions & 1 deletion commands/planets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { formatMoney } from "accounting";
import HellHub, { type Planet } from "@hellhub-collective/sdk";

import ascii from "utils/ascii";
import output from "utils/output";
import request from "utils/request";
import interval from "utils/interval";
import { createListCommand, parseListOptions } from "utils/options";
Expand Down Expand Up @@ -35,7 +36,7 @@ export default function planets(program: Command) {
});

if (!!args[1].raw) {
console.log(data);
output(data, !!args[1].pretty);
process.exit(0);
}

Expand Down
3 changes: 2 additions & 1 deletion commands/reports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { Command } from "commander";
import HellHub, { type Report } from "@hellhub-collective/sdk";

import ascii from "utils/ascii";
import output from "utils/output";
import request from "utils/request";
import interval from "utils/interval";
import { createListCommand, parseListOptions } from "utils/options";
Expand Down Expand Up @@ -54,7 +55,7 @@ export default function reports(program: Command) {
});

if (!!args[1].raw) {
console.log(data);
output(data, !!args[1].pretty);
process.exit(0);
}

Expand Down
3 changes: 2 additions & 1 deletion commands/sectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { Command } from "commander";
import HellHub, { type Sector } from "@hellhub-collective/sdk";

import ascii from "utils/ascii";
import output from "utils/output";
import request from "utils/request";
import interval from "utils/interval";
import { createListCommand, parseListOptions } from "utils/options";
Expand All @@ -17,7 +18,7 @@ export default function sectors(program: Command) {
});

if (!!args[1].raw) {
console.log(data);
output(data, !!args[1].pretty);
process.exit(0);
}

Expand Down
3 changes: 2 additions & 1 deletion commands/statistics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { Command } from "commander";
import HellHub, { type Stat } from "@hellhub-collective/sdk";

import ascii from "utils/ascii";
import output from "utils/output";
import request from "utils/request";
import interval from "utils/interval";
import { createListCommand, parseListOptions } from "utils/options";
Expand All @@ -19,7 +20,7 @@ export default function statistics(program: Command) {
});

if (!!args[1].raw) {
console.log(data);
output(data, !!args[1].pretty);
process.exit(0);
}

Expand Down
3 changes: 2 additions & 1 deletion commands/stratagems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { Command } from "commander";
import HellHub, { type Stratagem } from "@hellhub-collective/sdk";

import ascii from "utils/ascii";
import output from "utils/output";
import request from "utils/request";
import interval from "utils/interval";
import { createListCommand, parseListOptions } from "utils/options";
Expand All @@ -17,7 +18,7 @@ export default function stratagems(program: Command) {
});

if (!!args[1].raw) {
console.log(data);
output(data, !!args[1].pretty);
process.exit(0);
}

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"license": "MIT",
"private": false,
"description": "A CLI for Helldivers 2: View the war progress without leaving your terminal! Powered by the HellHub API.",
"version": "1.2.6",
"version": "1.3.0",
"main": "build/index.mjs",
"types": "build/index.d.ts",
"keywords": [
Expand Down Expand Up @@ -54,7 +54,7 @@
"email": "[email protected]"
},
"dependencies": {
"@hellhub-collective/sdk": "latest",
"@hellhub-collective/sdk": "^1.2.1",
"accounting": "^0.4.1",
"boxen": "^7.1.1",
"chalk": "^5.3.0",
Expand Down
3 changes: 2 additions & 1 deletion utils/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ export function createListCommand(
.argument("[id]", "the id of the entry to be fetched")
.option("-u, --url [boolean]", "prints the request url")
.option("-o, --order-by <string>", "the field to order by")
.option("-r, --raw [boolean]", "outputs the raw response body")
.option("-a, --no-ascii [boolean]", "disables the ascii art headers")
.option("-r, --raw [boolean]", "outputs the raw response data as json")
.option("-p, --pretty [boolean]", "use with --raw to pretty print the json")
.option("-s, --start <number>", "the number of entries to skip", parseInt)
.option("-l, --limit <number>", "the number of entries to fetch", parseInt)
.option(
Expand Down
5 changes: 5 additions & 0 deletions utils/output.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { Entity } from "@hellhub-collective/sdk";

export default function output(data: Entity | Array<Entity>, pretty = true) {
console.log(JSON.stringify(data, null, pretty ? 1 : 0));
}

0 comments on commit c5277ee

Please sign in to comment.