Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extended commands api #1

Merged
merged 9 commits into from
Sep 28, 2024
1 change: 1 addition & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"@consy/configs/prettier/config.json"
Binary file added assets/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion core/src/interactive-object-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class InteractiveObjectBuilder {

public addCommand(command: CommandDefinition): void {
const collableCommand: CallableCommand = () => {
const commandResult: void | Promise<void> = command.callback();
const commandResult: void | Promise<void> = command.callback({} as any);
ScarletFlash marked this conversation as resolved.
Show resolved Hide resolved

const onDoneMessage: string = `Command ${command.name} is executed.`;
commandResult instanceof Promise
Expand Down
9 changes: 9 additions & 0 deletions declarations/src/command-definition-base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { AnyFunction } from './any-function';
import { SyncOrAsyncFunction } from './sync-or-async-function';

export interface CommandDefinitionBase<C extends AnyFunction> {
readonly name: string;
readonly description: string;

readonly callback: SyncOrAsyncFunction<C>;
}
13 changes: 3 additions & 10 deletions declarations/src/command-definition.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
import { AnyFunction } from './any-function';
import { SyncOrAsyncFunction } from './sync-or-async-function';
import { NonParameterizableCommand } from './non-parameterizable-command';
import { ParameterizableCommand } from './parameterizable-command';

interface CommandDefinitionBase<C extends AnyFunction> {
name: string;
description: string;

callback: SyncOrAsyncFunction<C>;
}

export type CommandDefinition = CommandDefinitionBase<() => void>;
export type CommandDefinition = NonParameterizableCommand | ParameterizableCommand;
5 changes: 5 additions & 0 deletions declarations/src/command-param-definition-base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { CommandParamType } from './command-param-type';

export interface CommandParamDefinitionBase<T extends CommandParamType = CommandParamType> {
readonly type: T;
}
14 changes: 14 additions & 0 deletions declarations/src/command-param-definition-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { CommandParamDefinitionBase } from './command-param-definition-base';
import { CommandParamType } from './command-param-type';
import { CommandParamsDefinition } from './command-params-definition';

type CommandParamDefinitionType<T extends CommandParamDefinitionBase> = T['type'];
export type CommandParams<P extends CommandParamsDefinition = CommandParamsDefinition> = {
[K in keyof P]: CommandParamDefinitionType<P[K]> extends CommandParamType.String
? string
: CommandParamDefinitionType<P[K]> extends CommandParamType.Toggle
? boolean
: CommandParamDefinitionType<P[K]> extends CommandParamType.Select
? string[]
: never;
};
21 changes: 21 additions & 0 deletions declarations/src/command-param-definition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { CommandParamDefinitionBase } from './command-param-definition-base';
import { CommandParamType } from './command-param-type';

type StringParamDefinition = CommandParamDefinitionBase<CommandParamType.String>;

interface ToggleParamDefinition extends CommandParamDefinitionBase<CommandParamType.Toggle> {
readonly isEnabledByDefault: boolean;
}

interface SelectParamDefinition extends CommandParamDefinitionBase<CommandParamType.Select> {
readonly isMultipleChoiceAllowed: boolean;
readonly options: ReadonlyArray<string>;
}

export type CommandParamDefinition<T extends CommandParamType = CommandParamType> = T extends CommandParamType.String
? StringParamDefinition
: T extends CommandParamType.Toggle
? ToggleParamDefinition
: T extends CommandParamType.Select
? SelectParamDefinition
: never;
5 changes: 5 additions & 0 deletions declarations/src/command-param-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export enum CommandParamType {
String = 'string',
Toggle = 'toggle',
Select = 'select'
}
5 changes: 5 additions & 0 deletions declarations/src/command-params-definition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { CommandParamDefinition } from './command-param-definition';

export interface CommandParamsDefinition {
readonly [key: string]: CommandParamDefinition;
}
3 changes: 3 additions & 0 deletions declarations/src/non-parameterizable-command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { CommandDefinitionBase } from './command-definition-base';

export type NonParameterizableCommand = CommandDefinitionBase<() => void>;
8 changes: 8 additions & 0 deletions declarations/src/parameterizable-command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { CommandDefinitionBase } from './command-definition-base';
import { CommandParams } from './command-param-definition-type';
import { CommandParamsDefinition } from './command-params-definition';

export interface ParameterizableCommand<P extends CommandParamsDefinition = CommandParamsDefinition>
extends CommandDefinitionBase<(params: CommandParams<P>) => void> {
readonly params: P;
}
Loading