Skip to content

Commit

Permalink
feat: add watcher and no-ascii modes (#3)
Browse files Browse the repository at this point in the history
* feat: add option to disable ascii art

* refractor: add request utility too handle requests

* feat: respect `--no-ascii` option automatically

* refractor: make sure utility is default export

* feat: implement new request utility

* refractor: rename file

* deps: add dependency for `bun-plugin-dts`

* fix: add missing dts file to build output

* lib(util): add interval utility for watcher mode

* feat: add `--watch` option

* feat: implement watch mode

* chore(release): bump package version to 1.1.0
  • Loading branch information
fabio-nettis committed Apr 5, 2024
1 parent b0d6a65 commit 6fd2305
Show file tree
Hide file tree
Showing 14 changed files with 244 additions and 255 deletions.
Binary file modified bun.lockb
Binary file not shown.
56 changes: 21 additions & 35 deletions commands/events.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import chalk from "chalk";
import boxen from "boxen";
import type { Command } from "commander";

import HellHub, {
type APIResponse,
type GlobalEvent,
} from "@hellhub-collective/sdk";
import HellHub, { type GlobalEvent } from "@hellhub-collective/sdk";

import ascii from "utils/ascii";
import { createListCommand, parseListOptions } from "utils/list-options";
import request from "utils/request";
import interval from "utils/interval";
import { createListCommand, parseListOptions } from "utils/options";

const border = (message: string) => {
if (message.includes("MAJOR")) {
Expand Down Expand Up @@ -47,35 +45,13 @@ const description = (message: string) => {
};

export default function events(program: Command) {
createListCommand(
program,
"events",
"fetch a list of events or get a event by id",
).action(async (...args) => {
const [id, query] = parseListOptions(...args);

let response: APIResponse<GlobalEvent | GlobalEvent[]> | undefined;
if (!!id) {
response = await HellHub.events(id, { query });
} else {
response = await HellHub.events({
limit: 1,
sort: ["id:desc"],
...query,
} as any);
}

if (!response) {
console.error("An error occurred while fetching data.");
process.exit(1);
}
const handler = async (...args: any[]) => {
const [id, query] = parseListOptions<GlobalEvent>(...args);

const { data, error } = await response.json();

if (!response.ok || !!error || !data) {
console.error(error?.details?.[0]);
process.exit(1);
}
const { data, url } = await request<GlobalEvent>(HellHub.events, id, {
...query,
...(!id ? { limit: 1, sort: ["id:desc"] } : {}),
});

if (!!args[1].raw) {
console.log(data);
Expand Down Expand Up @@ -121,7 +97,17 @@ export default function events(program: Command) {

if (!!args[1].url) {
console.log(chalk.bold("\nRequest Source"));
console.log(chalk.gray(`/${response.url.split("/").slice(3).join("/")}`));
console.log(chalk.gray(`/${url.split("/").slice(3).join("/")}`));
}
};

createListCommand(
program,
"events",
"fetch a list of events or get a event by id",
).action(async (...args: any[]) => {
await handler(...args);
if (!args[1].watch) process.exit(0);
interval(async () => await handler(...args), args[1].watch);
});
}
70 changes: 24 additions & 46 deletions commands/major.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,25 @@ import chalk from "chalk";
import boxen from "boxen";
import { formatMoney } from "accounting";
import type { Command } from "commander";

import HellHub, {
type APIResponse,
type Assignment,
} from "@hellhub-collective/sdk";
import HellHub, { type Assignment } from "@hellhub-collective/sdk";

import ascii from "utils/ascii";
import { createListCommand, parseListOptions } from "utils/list-options";
import request from "utils/request";
import interval from "utils/interval";
import { createListCommand, parseListOptions } from "utils/options";

export default function major(program: Command) {
createListCommand(
program,
"major",
"show details for the current major order",
).action(async (...args) => {
const handler = async (...args: any[]) => {
const [id, query] = parseListOptions(...args);

let response: APIResponse<Assignment | Assignment[]> | undefined;
if (!!id) {
response = await HellHub.assignments(id, {
query: {
...query,
include: [
...(Array.isArray(query.include) ? query.include : [] ?? []),
"reward",
],
} as any,
});
} else {
response = await HellHub.assignments({
limit: 1,
sort: ["id:desc"],
...query,
include: [
...(Array.isArray(query.include) ? query.include : [] ?? []),
"reward",
],
} as any);
}

if (!response) {
console.error("An error occurred while fetching data.");
process.exit(1);
}

const { data, error } = await response.json();

if (!response.ok || !!error || !data) {
console.error(error?.details?.[0]);
process.exit(1);
}
const { data, url } = await request<Assignment>(HellHub.assignments, id, {
...(!id ? { limit: 1, sort: ["id:desc"] } : {}),
...query,
include: [
...(Array.isArray(query.include) ? query.include : [] ?? []),
"reward",
],
});

if (!!args[1].raw) {
console.log(data);
Expand Down Expand Up @@ -107,7 +75,17 @@ export default function major(program: Command) {

if (!!args[1].url) {
console.log(chalk.bold("\nRequest Source"));
console.log(chalk.gray(`/${response.url.split("/").slice(3).join("/")}`));
console.log(chalk.gray(`/${url.split("/").slice(3).join("/")}`));
}
};

createListCommand(
program,
"major",
"show details for the current major order",
).action(async (...args: any[]) => {
await handler(...args);
if (!args[1].watch) process.exit(0);
interval(async () => await handler(...args), args[1].watch);
});
}
49 changes: 18 additions & 31 deletions commands/planets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ import chalk from "chalk";
import Table from "cli-table3";
import type { Command } from "commander";
import { formatMoney } from "accounting";

import HellHub, {
type APIResponse,
type Planet,
} from "@hellhub-collective/sdk";
import HellHub, { type Planet } from "@hellhub-collective/sdk";

import ascii from "utils/ascii";
import { createListCommand, parseListOptions } from "utils/list-options";
import request from "utils/request";
import interval from "utils/interval";
import { createListCommand, parseListOptions } from "utils/options";

const owner = (index: number) => {
switch (index) {
Expand All @@ -25,31 +23,10 @@ const owner = (index: number) => {
};

export default function planets(program: Command) {
createListCommand(
program,
"planets",
"fetch a list of planets or get a planet by id",
).action(async (...args) => {
const [id, query] = parseListOptions(...args);

let response: APIResponse<Planet | Planet[]> | undefined;
if (!!id) {
response = await HellHub.planets(id, { query });
} else {
response = await HellHub.planets(query);
}

if (!response) {
console.error("An error occurred while fetching data.");
process.exit(1);
}

const { data, error } = await response.json();
const handler = async (...args: any[]) => {
const [id, query] = parseListOptions<Planet>(...args);

if (!response.ok || !!error || !data) {
console.error(error?.details?.[0]);
process.exit(1);
}
const { data, url } = await request<Planet>(HellHub.planets, id, query);

if (!!args[1].raw) {
console.log(data);
Expand Down Expand Up @@ -115,7 +92,17 @@ export default function planets(program: Command) {

if (!!args[1].url) {
console.log(chalk.bold("\nRequest Source"));
console.log(chalk.gray(`/${response.url.split("/").slice(3).join("/")}`));
console.log(chalk.gray(`/${url.split("/").slice(3).join("/")}`));
}
};

createListCommand(
program,
"planets",
"fetch a list of planets or get a planet by id",
).action(async (...args: any[]) => {
await handler(...args);
if (!args[1].watch) process.exit(0);
interval(async () => await handler(...args), args[1].watch);
});
}
56 changes: 21 additions & 35 deletions commands/reports.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import chalk from "chalk";
import boxen from "boxen";
import type { Command } from "commander";

import HellHub, {
type APIResponse,
type Report,
} from "@hellhub-collective/sdk";
import HellHub, { type Report } from "@hellhub-collective/sdk";

import ascii from "utils/ascii";
import { createListCommand, parseListOptions } from "utils/list-options";
import request from "utils/request";
import interval from "utils/interval";
import { createListCommand, parseListOptions } from "utils/options";

const border = (message: string) => {
if (message.includes("MAJOR")) {
Expand Down Expand Up @@ -47,35 +45,13 @@ const description = (message: string) => {
};

export default function reports(program: Command) {
createListCommand(
program,
"reports",
"fetch a list of reports or get a report by id",
).action(async (...args) => {
const [id, query] = parseListOptions(...args);

let response: APIResponse<Report | Report[]> | undefined;
if (!!id) {
response = await HellHub.reports(id, { query });
} else {
response = await HellHub.reports({
limit: 1,
sort: ["id:desc"],
...query,
} as any);
}

if (!response) {
console.error("An error occurred while fetching data.");
process.exit(1);
}
const handler = async (...args: any[]) => {
const [id, query] = parseListOptions<Report>(...args);

const { data, error } = await response.json();

if (!response.ok || !!error || !data) {
console.error(error?.details?.[0]);
process.exit(1);
}
const { data, url } = await request<Report>(HellHub.reports, id, {
...(!id ? { limit: 1, sort: ["id:desc"] } : {}),
...query,
});

if (!!args[1].raw) {
console.log(data);
Expand Down Expand Up @@ -121,7 +97,17 @@ export default function reports(program: Command) {

if (!!args[1].url) {
console.log(chalk.bold("\nRequest Source"));
console.log(chalk.gray(`/${response.url.split("/").slice(3).join("/")}`));
console.log(chalk.gray(`/${url.split("/").slice(3).join("/")}`));
}
};

createListCommand(
program,
"reports",
"fetch a list of reports or get a report by id",
).action(async (...args: any[]) => {
await handler(...args);
if (!args[1].watch) process.exit(0);
interval(async () => await handler(...args), args[1].watch);
});
}
51 changes: 20 additions & 31 deletions commands/sectors.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,20 @@
import chalk from "chalk";
import Table from "cli-table3";
import type { Command } from "commander";

import HellHub, {
type Sector,
type APIResponse,
} from "@hellhub-collective/sdk";
import HellHub, { type Sector } from "@hellhub-collective/sdk";

import ascii from "utils/ascii";
import { createListCommand, parseListOptions } from "utils/list-options";
import request from "utils/request";
import interval from "utils/interval";
import { createListCommand, parseListOptions } from "utils/options";

export default function sectors(program: Command) {
createListCommand(
program,
"sectors",
"fetch a list of sectors or get a sector by id",
).action(async (...args) => {
const [id, query] = parseListOptions(...args);

let response: APIResponse<Sector | Sector[]> | undefined;
if (!!id) {
response = await HellHub.sectors(id, { query });
} else {
response = await HellHub.sectors(query);
}

if (!response) {
console.error("An error occurred while fetching data.");
process.exit(1);
}
const handler = async (...args: any[]) => {
const [id, query] = parseListOptions<Sector>(...args);

const { data, error } = await response.json();

if (!response.ok || !!error || !data) {
console.error(error?.details?.[0]);
process.exit(1);
}
const { data, url } = await request<Sector>(HellHub.sectors, id, {
...query,
});

if (!!args[1].raw) {
console.log(data);
Expand Down Expand Up @@ -68,7 +47,17 @@ export default function sectors(program: Command) {

if (!!args[1].url) {
console.log(chalk.bold("\nRequest Source"));
console.log(chalk.gray(`/${response.url.split("/").slice(3).join("/")}`));
console.log(chalk.gray(`/${url.split("/").slice(3).join("/")}`));
}
};

createListCommand(
program,
"sectors",
"fetch a list of sectors or get a sector by id",
).action(async (...args: any[]) => {
await handler(...args);
if (!args[1].watch) process.exit(0);
interval(async () => await handler(...args), args[1].watch);
});
}
Loading

0 comments on commit 6fd2305

Please sign in to comment.