Skip to content

Commit bb7906c

Browse files
committed
fix: foo
1 parent 958537a commit bb7906c

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@
5252
"dependencies": {
5353
"@dword-design/defu": "^1.0.1",
5454
"commander": "^12.0.0",
55-
"lodash-es": "^4.17.21"
55+
"lodash-es": "^4.17.21",
56+
"p-is-promise": "^4.0.0"
5657
},
5758
"devDependencies": {
5859
"@dword-design/base": "^13.1.3",

pnpm-lock.yaml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import {
44
Option as CommanderOption,
55
} from 'commander';
66
import { compact } from 'lodash-es';
7+
import pIsPromise from 'p-is-promise';
8+
9+
export type HandlerInput = (...args: unknown[]) => unknown;
710

811
export type Handler = (...args: unknown[]) => void | Promise<void>;
912

@@ -34,8 +37,9 @@ export type Config = {
3437
defaultCommandName?: string;
3538
};
3639

37-
export type CommandObjectInput = Omit<Command, 'options'> & {
40+
export type CommandObjectInput = Omit<Command, 'options' | 'handler'> & {
3841
options?: OptionsInput;
42+
handler: HandlerInput;
3943
};
4044

4145
export type CommandObjectInObjectInput = Omit<CommandObjectInput, 'name'> &
@@ -51,9 +55,10 @@ export type OptionInObjectInput = Omit<Option, 'name'> &
5155
Partial<Pick<Option, 'name'>>;
5256

5357
export type OptionsInput = Option[] | Record<string, OptionInObjectInput>;
54-
type ConfigInput = Omit<Partial<Config>, 'commands' | 'options'> & {
58+
type ConfigInput = Omit<Partial<Config>, 'commands' | 'options' | 'action'> & {
5559
commands?: CommandsInput;
5660
options?: OptionsInput;
61+
action?: HandlerInput;
5762
};
5863

5964
const applyOptions = (program, options: Option[] = []) => {
@@ -95,24 +100,42 @@ const getNormalizedCommands = (commands?: CommandsInput): Command[] => {
95100
if (Array.isArray(commands)) {
96101
return commands.map(command => ({
97102
...command,
103+
handler: toVoidReturning(command.handler),
98104
options: getNormalizedOptions(command.options),
99105
}));
100106
}
101107

102108
return Object.entries(commands).map(([name, command]) => ({
103109
name,
104110
...(typeof command === 'function'
105-
? { handler: command, options: [] }
106-
: { ...command, options: getNormalizedOptions(command.options) }),
111+
? { handler: toVoidReturning(command), options: [] }
112+
: {
113+
...command,
114+
handler: toVoidReturning(command.handler),
115+
options: getNormalizedOptions(command.options),
116+
}),
107117
}));
108118
};
109119

120+
const toVoidReturning =
121+
func =>
122+
(...args) => {
123+
const result = func(...args);
124+
125+
if (pIsPromise(result)) {
126+
return Promise.resolve();
127+
}
128+
};
129+
110130
export default (configInput: ConfigInput = {}) => {
111131
const config: Config = defu(
112132
{
113133
...configInput,
114134
commands: getNormalizedCommands(configInput.commands),
115135
options: getNormalizedOptions(configInput.options),
136+
...(configInput.action && {
137+
action: toVoidReturning(configInput.action),
138+
}),
116139
},
117140
{ allowUnknownOption: false },
118141
);

0 commit comments

Comments
 (0)