@jill64/ts-cli
Advanced tools
Comparing version 0.1.0 to 0.2.0
import { Config } from './types/Config.js'; | ||
import { InvokeHandler } from './types/invoke-route/InvokeHandler.js'; | ||
import { InvokeParam } from './types/invoke-route/InvokeParam.js'; | ||
import { MergeConfig } from './types/util/MergeConfig.js'; | ||
export declare class App<RC extends Config, RH extends InvokeHandler<RC>, RT extends { | ||
[route: string]: { | ||
config: Config; | ||
handler: InvokeHandler<Config>; | ||
}; | ||
}> { | ||
private config; | ||
private handler; | ||
private routes; | ||
export declare class App<RC extends Config, RH extends InvokeHandler<RC>, RT extends Record<string, { | ||
config: Config; | ||
handler: InvokeHandler<Config>; | ||
}>> { | ||
private readonly config; | ||
private readonly handler; | ||
private readonly routes; | ||
readonly invoke: { [K in keyof RT]: InvokeHandler<RT[K]["config"]>; }; | ||
constructor(config: RC, handler: RH, routes?: RT); | ||
add<T extends string, C extends Config, H extends InvokeHandler<MergeConfig<RC, C>>>(route: T, config: C, handler: H): App<RC, RH, RT & Record<T, { | ||
add<T extends string, C extends Config, H extends InvokeHandler<C>>(route: T, config: C, handler: H): App<RC, RH, RT & Record<T, { | ||
config: C; | ||
@@ -20,10 +18,7 @@ handler: H; | ||
execute(param: InvokeParam<RC>): import("./types/invoke-route/InvokeHandlerReturn.js").InvokeHandlerReturn<RC>; | ||
invoke<R extends keyof RT, P extends InvokeParam<RT[R]['config']>>(route: R, param: P): import("./types/invoke-route/InvokeHandlerReturn.js").InvokeHandlerReturn<Config>; | ||
private lookup; | ||
private get_merged_config; | ||
private convert_to_native_options; | ||
private split_args; | ||
private alignment_arguments; | ||
private extract; | ||
run(argv: string[]): void | keyof RC["codes"] | Promise<void | keyof RC["codes"]>; | ||
run(argv: string[]): void | keyof RC["codes"] | Promise<void | keyof RC["codes"]> | keyof RT[keyof RT]["config"]["codes"] | Promise<void | keyof RT[keyof RT]["config"]["codes"]>; | ||
} |
@@ -7,2 +7,3 @@ import { transform } from '@jill64/transform'; | ||
routes; | ||
invoke; | ||
constructor(config, handler, routes = {}) { | ||
@@ -12,5 +13,12 @@ this.config = config; | ||
this.routes = routes; | ||
this.config = config; | ||
this.handler = handler; | ||
this.routes = routes; | ||
this.invoke = transform(routes, ([route, { handler }]) => [ | ||
route, | ||
handler | ||
]); | ||
} | ||
add(route, config, handler) { | ||
this.routes = { | ||
return new App(this.config, this.handler, { | ||
...this.routes, | ||
@@ -21,4 +29,3 @@ [route]: { | ||
} | ||
}; | ||
return this; | ||
}); | ||
} | ||
@@ -28,5 +35,2 @@ execute(param) { | ||
} | ||
invoke(route, param) { | ||
return this.routes[route].handler(param); | ||
} | ||
lookup(args) { | ||
@@ -41,31 +45,2 @@ const input = args.join(' ').trim(); | ||
} | ||
get_merged_config(route) { | ||
const root = this.config; | ||
const config = this.routes[route].config; | ||
return { | ||
...config, | ||
options: { | ||
...root.options, | ||
...config.options | ||
}, | ||
codes: { | ||
...root.options, | ||
...config.codes | ||
} | ||
}; | ||
} | ||
convert_to_native_options(options) { | ||
return transform(options ?? {}, ([k, v]) => [ | ||
k, | ||
{ | ||
type: 'type' in v && (v.type === 'string' || v.type === 'string[]') | ||
? 'string' | ||
: 'boolean', | ||
multiple: 'type' in v && (v.type === 'string[]' || v.type === 'boolean[]') | ||
? true | ||
: false, | ||
short: v.alias | ||
} | ||
]); | ||
} | ||
split_args(args, config, options, alignedArgs, alignedOpts, route) { | ||
@@ -119,4 +94,15 @@ const routeDepth = route?.toString()?.split(' ').length ?? 0; | ||
extract(args, route) { | ||
const config = route ? this.get_merged_config(route) : this.config; | ||
const options = this.convert_to_native_options(config.options); | ||
const config = route ? this.routes[route].config : this.config; | ||
const options = transform(config.options ?? {}, ([k, v]) => [ | ||
k, | ||
{ | ||
type: 'type' in v && (v.type === 'string' || v.type === 'string[]') | ||
? 'string' | ||
: 'boolean', | ||
multiple: 'type' in v && (v.type === 'string[]' || v.type === 'boolean[]') | ||
? true | ||
: false, | ||
short: v.alias | ||
} | ||
]); | ||
const alignedArgs = this.alignment_arguments(config.args); | ||
@@ -139,3 +125,3 @@ const alignedOpts = this.alignment_arguments(config.optional); | ||
])), | ||
rest: rest | ||
rest | ||
}; | ||
@@ -147,6 +133,6 @@ return param; | ||
const route = this.lookup(args); | ||
const handler = route ? this.routes[route].handler : this.handler; | ||
const param = this.extract(args, route); | ||
return handler(param); | ||
return route | ||
? this.invoke[route](this.extract(args, route)) | ||
: this.execute(this.extract(args)); | ||
} | ||
} |
@@ -0,1 +1,2 @@ | ||
import { OptionAlias } from '../OptionAlias.js'; | ||
import { OptionType } from '../OptionType.js'; | ||
@@ -12,3 +13,3 @@ export type OptionDescriptions = Record<string, { | ||
*/ | ||
alias?: string; | ||
alias?: OptionAlias; | ||
/** | ||
@@ -15,0 +16,0 @@ * Type of option. |
{ | ||
"name": "@jill64/ts-cli", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"type": "module", | ||
@@ -44,5 +44,5 @@ "files": [ | ||
"url": "https://github.com/jill64/ts-cli.git", | ||
"image": "https://opengraph.githubassets.com/8f62a9306055fb3b0aa92185a4a5805897ef1ed84d351d1dee197860320d505b/jill64/ts-cli" | ||
"image": "https://opengraph.githubassets.com/2969dd415b0a650a0e890541fa728cab2b69d208b7f5439f3e4d8666d481a62a/jill64/ts-cli" | ||
}, | ||
"description": "> Solidly-Typed CLI Application Builder", | ||
"description": "> Solidly-Typed CLI Router", | ||
"publishConfig": { | ||
@@ -49,0 +49,0 @@ "access": "public" |
@@ -11,3 +11,3 @@ <!----- BEGIN GHOST DOCS HEADER -----> | ||
> Solidly-Typed CLI Application Builder | ||
> Solidly-Typed CLI Router | ||
@@ -75,5 +75,2 @@ <!----- END GHOST DOCS HEADER -----> | ||
> [!NOTE] | ||
> Root `options` and `codes` are inherited by all route. | ||
```js | ||
@@ -122,12 +119,10 @@ import { App } from '@jill64/ts-cli' | ||
export const { execute, invoke } = new App(/* ... */) | ||
.add(/* ... */) | ||
.add(/* ... */) | ||
export const command = new App(/* ... */).add(/* ... */).add(/* ... */) | ||
``` | ||
```js | ||
import { invoke } from 'index.js' | ||
import { command } from 'index.js' | ||
// `example` | ||
execute({ | ||
command.execute({ | ||
args: { | ||
@@ -152,3 +147,3 @@ arg1: 'value1', | ||
// `example test` | ||
invoke('test', { | ||
command.invoke.test({ | ||
// ... | ||
@@ -158,3 +153,3 @@ }) | ||
// `example test start` | ||
invoke('test start', { | ||
command.invoke['test start']({ | ||
// ... | ||
@@ -161,0 +156,0 @@ }) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
0
16086
39
308
161