Skip to content

Commit

Permalink
feat: add createMain utility (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
Barbapapazes authored Sep 9, 2023
1 parent 416cd8f commit 71802f9
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ runMain(main);

Runs a command with usage support and graceful error handling.

### `createMain`

Create a wrapper around command that calls `runMain` when called.

### `runCommand`

Parses input args and runs command and sub-commands (unsupervised).
Expand Down
8 changes: 4 additions & 4 deletions playground/hello.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import consola from 'consola'
import { defineCommand, runMain } from "../src";
import consola from "consola";
import { defineCommand, createMain } from "../src";

const main = defineCommand({
const command = defineCommand({
meta: {
name: "hello",
version: "1.0.0",
Expand All @@ -23,4 +23,4 @@ const main = defineCommand({
},
});

runMain(main);
createMain(command)();
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ export type { RunMainOptions } from "./main";
export { defineCommand, runCommand } from "./command";
export { parseArgs } from "./args";
export { renderUsage, showUsage } from "./usage";
export { runMain } from "./main";
export { runMain, createMain } from "./main";
6 changes: 6 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,9 @@ export async function runMain<T extends ArgsDef = ArgsDef>(
process.exit(1);
}
}

export function createMain<T extends ArgsDef = ArgsDef>(
cmd: CommandDef<T>,
): (opts?: RunMainOptions) => Promise<void> {
return (opts: RunMainOptions = {}) => runMain(cmd, opts);
}

0 comments on commit 71802f9

Please sign in to comment.