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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
3 changes: 2 additions & 1 deletion core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"extract-api": "api-extractor run --local --typescript-compiler-folder node_modules/typescript"
},
"dependencies": {
"@consy/declarations": "workspace:*"
"@consy/declarations": "workspace:*",
"@consy/utilities": "workspace:*"
},
"devDependencies": {
"@consy/configs": "workspace:*",
Expand Down
14 changes: 9 additions & 5 deletions core/src/core.ts → core/src/consy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { CommandDefinition, InteractiveObject } from '@consy/declarations';
import { Acessor } from './accessor';
import { InteractiveObjectBuilder } from './interactive-object-builder';

export class Core<K extends string> {
export class Consy<K extends string = string> {
readonly #interactor: InteractiveObjectBuilder = new InteractiveObjectBuilder();

readonly #accessor: Acessor<InteractiveObject, K> = new Acessor<InteractiveObject, K>(window);
Expand All @@ -13,19 +13,23 @@ export class Core<K extends string> {
this.#key = key;
}

public mount(): void {
public mount(): this {
this.#accessor.mount(this.#key, this.#interactor.payload);
return this;
}

public unmount(): void {
public unmount(): this {
this.#accessor.unmount(this.#key);
return this;
}

public addCommand(command: CommandDefinition): void {
public addCommand(command: CommandDefinition): this {
this.#interactor.addCommand(command);
return this;
}

public removeCommand(name: string): void {
public removeCommand(name: string): this {
this.#interactor.removeCommand(name);
return this;
}
}
2 changes: 1 addition & 1 deletion core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './core';
export * from './consy';
24 changes: 17 additions & 7 deletions core/src/interactive-object-builder.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,35 @@
import { CallableCommand, CommandDefinition, InteractiveObject } from '@consy/declarations';
import { getValidatedParameterizableCommandParams, isParameterizableCommand } from '@consy/utilities';
import { Acessor } from './accessor';

export class InteractiveObjectBuilder {
readonly #payload: InteractiveObject = {};
readonly #accessor: Acessor<CallableCommand> = new Acessor<CallableCommand>(this.#payload);
readonly #payload: InteractiveObject;
readonly #accessor: Acessor<CallableCommand>;

public get payload(): InteractiveObject {
return this.#payload;
}

public addCommand(command: CommandDefinition): void {
const collableCommand: CallableCommand = () => {
const commandResult: void | Promise<void> = command.callback();
constructor() {
this.#payload = Object.setPrototypeOf({}, null);
this.#accessor = new Acessor<CallableCommand>(this.#payload);
}

public addCommand(commandDefinition: CommandDefinition): void {
const collableCommand: CallableCommand = (...callParams: unknown[]) => {
const commandResult: void | Promise<void> = commandDefinition.callback(
isParameterizableCommand(commandDefinition)
? getValidatedParameterizableCommandParams({ commandDefinition, callParams })
: {}
);

const onDoneMessage: string = `Command ${command.name} is executed.`;
const onDoneMessage: string = `Command ${commandDefinition.name} is executed.`;
commandResult instanceof Promise
? commandResult.finally(() => console.log(onDoneMessage))
: console.log(onDoneMessage);
};

this.#accessor.mount(command.name, collableCommand);
this.#accessor.mount(commandDefinition.name, collableCommand);
}

public removeCommand(name: string): void {
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;
}
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;
}
15 changes: 15 additions & 0 deletions declarations/src/command-params.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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<D extends CommandParamsDefinition = CommandParamsDefinition> = {
[K in keyof D]: CommandParamDefinitionType<D[K]> extends CommandParamType.String
? string
: CommandParamDefinitionType<D[K]> extends CommandParamType.Toggle
? boolean
: CommandParamDefinitionType<D[K]> extends CommandParamType.Select
? string[]
: never;
};
6 changes: 6 additions & 0 deletions declarations/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
export * from './callable-command';
export * from './command-definition';
export * from './interactive-object';
export * from './parameterizable-command';
export * from './non-parameterizable-command';
export * from './command-param-definition';
export * from './command-param-type';
export * from './command-params';
export * from './command-params-definition';
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-params';
import { CommandParamsDefinition } from './command-params-definition';

export interface ParameterizableCommand<D extends CommandParamsDefinition = CommandParamsDefinition>
extends CommandDefinitionBase<(params: CommandParams<D>) => void> {
readonly params: D;
}
3 changes: 2 additions & 1 deletion dist/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@
"prettier": "catalog:codebase",
"@microsoft/api-extractor": "catalog:build-tools"
},
"typings": "src/index.ts"
"typings": "src/index.ts",
"main": "src/index.ts"
}
9 changes: 7 additions & 2 deletions dist/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
export { Core } from '@consy/core';
export { CommandDefinition } from '@consy/declarations';
export { Consy } from '@consy/core';
export {
CommandDefinition,
CommandParamType,
NonParameterizableCommand,
ParameterizableCommand
} from '@consy/declarations';
1 change: 1 addition & 0 deletions examples/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
serve/
27 changes: 27 additions & 0 deletions examples/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "@consy/examples",
"private": true,
"scripts": {
"compiler__run": "pnpm run compiler__run:build && pnpm run compiler__run:scripts",
"compiler__check": "pnpm run compiler__check:build && pnpm run compiler__check:scripts",
"compiler__run:build": "tsc --project tsconfig.build.json",
"compiler__check:build": "tsc --project tsconfig.build.json --noEmit",
"compiler__run:scripts": "tsc --project tsconfig.scripts.json",
"compiler__check:scripts": "tsc --project tsconfig.scripts.json --noEmit",
"formatter__check": "prettier --config node_modules/@consy/configs/prettier/config.json --ignore-path node_modules/@consy/configs/prettier/ignore --cache --cache-location .prettiercache --cache-strategy content --check .",
"formatter__fix": "prettier --config node_modules/@consy/configs/prettier/config.json --ignore-path node_modules/@consy/configs/prettier/ignore --cache --cache-location .prettiercache --cache-strategy content --write .",
"start": "ts-node --project tsconfig.scripts.json ./start.script.ts"
},
"dependencies": {
"consy": "workspace:*",
"tailwindcss": "catalog:build-tools"
},
"devDependencies": {
"@consy/configs": "workspace:*",
"typescript": "catalog:codebase",
"ts-node": "catalog:build-tools",
"esbuild": "catalog:build-tools",
"@types/node": "catalog:build-tools",
"prettier": "catalog:codebase"
}
}
3 changes: 3 additions & 0 deletions examples/src/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@tailwind base;
@tailwind components;
@tailwind utilities;
18 changes: 18 additions & 0 deletions examples/src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!doctype html>

<html lang="en">
<head>
<meta charset="utf-8" />
<meta
name="viewport"
content="width=device-width"
/>

<link
rel="stylesheet"
href="./index.css"
/>
</head>

<body class="bg-[#ff0103]"></body>
</html>
Loading
Loading