termost
Version:
Get the most of your terminal
124 lines • 4.59 kB
TypeScript
//#region src/types.d.ts
/**
* Raw CLI arguments parsed from user inputs.
*/
type ArgumentValues = {
command: CommandName;
operands: string[];
options: Record<string, boolean | number | string>;
};
type CommandName = string;
type Context<Values extends ObjectLikeConstraint> = Values;
type EmptyObject = {};
type InstructionKey<Key> = {
/**
* Makes the method output available in the context object.
* By default, if no provided key, the output is not included in the context.
*/
key: Key;
};
type InstructionParameters<Values extends ObjectLikeConstraint, ExtraParameters extends ObjectLikeConstraint = EmptyObject> = {
skip?: (context: Context<Values>, argv: ArgumentValues) => boolean;
validate?: (context: Context<Values>, argv: ArgumentValues) => Error | undefined;
} & ExtraParameters;
type Label<Values extends ObjectLikeConstraint> = ((context: Context<Values>, argv: ArgumentValues) => string) | string;
type ObjectLikeConstraint = Record<string, any>;
type PackageMetadata = {
description: string;
name: string;
version: string;
};
//#endregion
//#region src/api/command/command.d.ts
type CommandParameters = {
description: string;
name: string;
};
//#endregion
//#region src/api/input/index.d.ts
type InputParameters<Values extends ObjectLikeConstraint, Key extends keyof Values> = InstructionParameters<Values, ({
defaultValue?: Values[Key] extends boolean ? Values[Key] : never;
type: "confirm";
} | {
defaultValue?: Values[Key] extends readonly string[] | string[] ? Values[Key][number][] : never;
options: Values[Key] extends readonly string[] | string[] ? Values[Key] : never;
type: "multiselect";
} | {
defaultValue?: Values[Key] extends string ? Values[Key] : never;
options: Values[Key] extends string ? readonly Values[Key][] | Values[Key][] : never;
type: "select";
} | {
defaultValue?: Values[Key] extends string ? Values[Key] : never;
type: "text";
}) & {
label: Label<Values>;
} & InstructionKey<Key>>;
//#endregion
//#region src/api/option/index.d.ts
type OptionParameters<Values extends ObjectLikeConstraint, Key extends keyof Values> = InstructionParameters<Values, {
defaultValue?: Values[Key];
description: string;
name: {
long: string;
short: string;
} | string;
} & InstructionKey<Key>>;
//#endregion
//#region src/api/task/index.d.ts
type TaskParameters<Values extends ObjectLikeConstraint, Key extends keyof Values | undefined = undefined> = InstructionParameters<Values, {
handler: (context: Context<Values>, argv: ArgumentValues) => Key extends keyof Values ? Promise<Values[Key]> | Values[Key] : Promise<void> | void;
label?: Label<Values>;
} & Partial<InstructionKey<Key>>>;
//#endregion
//#region src/termost.d.ts
/**
* The termost fluent interface API.
*/
type Termost<Values extends ObjectLikeConstraint = EmptyObject> = {
/**
* Allows to attach a new sub-command to the program.
* @param parameters - The CLI command name and description.
* @returns The Command API.
*/
command: <CommandValues extends ObjectLikeConstraint = EmptyObject>(parameters: CommandParameters) => Termost<CommandValues & Values>;
input: <Key extends keyof Values>(parameters: InputParameters<Values, Key>) => Termost<Values>;
option: <Key extends keyof Values>(parameters: OptionParameters<Values, Key>) => Termost<Values>;
task: <Key extends keyof Values | undefined = undefined>(parameters: TaskParameters<Values, Key>) => Termost<Values>;
};
declare function termost<Values extends ObjectLikeConstraint = EmptyObject>({
description,
name,
onException,
onShutdown,
version
}: PackageMetadata & TerminationCallbacks): Termost<Values>;
type TerminationCallbacks = Partial<{
onException: ((error: Error) => void) | undefined;
onShutdown: (() => void) | undefined;
}>;
//#endregion
//#region src/index.d.ts
declare const helpers: {
exec: (command: string, options?: {
cwd?: string;
hasLiveOutput?: boolean;
}) => Promise<string>;
format: (message: string, options?: {
color?: "black" | "blue" | "cyan" | "green" | "grey" | "magenta" | "red" | "white" | "yellow";
modifiers?: ("bold" | "italic" | "lowercase" | "strikethrough" | "underline" | "uppercase")[];
}) => string;
message: (content: Error | string, {
label: optionLabel,
lineBreak: optionlineBreak,
type: optionType
}?: {
label?: false | string;
lineBreak?: boolean | {
end: boolean;
start: boolean;
};
type?: "error" | "information" | "success" | "warning";
}) => void;
};
//#endregion
export { type Termost, helpers, termost };