Skip to content

Commit

Permalink
feat: async functions
Browse files Browse the repository at this point in the history
  • Loading branch information
k2on committed Oct 28, 2024
1 parent 2ed104a commit c5bffe9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@koons/cli",
"version": "0.1.3",
"version": "0.1.4",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
Expand Down
14 changes: 7 additions & 7 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class CommandBuilder<T extends ZodSchema> {
return new CommandBuilder<z.infer<typeof schema>>(this);
}

fn(fn: (input: { input: T }) => void): BuildCommand<any> {
fn(fn: (input: { input: T }) => Promise<void>): BuildCommand<any> {
return {
_type: "command",
input: this.schema,
Expand All @@ -41,7 +41,7 @@ interface BuildCommand<T extends ZodTypeAny> {
_type: "command";
input?: ZodSchema<T>;
description?: string;
fn: (input: { input: T }) => void;
fn: (input: { input: T }) => Promise<void>;
}

export type Commands = {
Expand All @@ -51,7 +51,7 @@ export type Commands = {

export type CLI = BuildCommand<any> | Commands;

function next(input: CLI, current: string | undefined, args: string[]) {
async function next(input: CLI, current: string | undefined, args: string[]) {
if (!current) return commands(input);

if (input._type == "cli") {
Expand All @@ -75,10 +75,10 @@ function next(input: CLI, current: string | undefined, args: string[]) {
return acc;
}, {} as Record<string, unknown>);

handler.fn({ input: obj });
await handler.fn({ input: obj });
return;
} else {
handler.fn({ input: {} });
await handler.fn({ input: {} });
return;
}
} else {
Expand All @@ -91,12 +91,12 @@ function next(input: CLI, current: string | undefined, args: string[]) {
}
}

export function run(input: CLI) {
export async function run(input: CLI) {
const args = process.argv.slice(2);
const current = args[0];
const commands = input;

next(commands, current, args);
await next(commands, current, args);
}

export const router = (commands: Commands["commands"]): Commands => {
Expand Down

0 comments on commit c5bffe9

Please sign in to comment.