@@ -2,6 +2,7 @@ import { parseArgs } from "node:util";
2
2
3
3
import type {
4
4
AddCommand ,
5
+ BaseCommand ,
5
6
Command ,
6
7
CommandArgs ,
7
8
CommandContext ,
@@ -10,7 +11,7 @@ import type {
10
11
LiteralString ,
11
12
ucmdState ,
12
13
} from "./types" ;
13
- import { generateOptions , toCommandArgs , CommandArg } from "./utils" ;
14
+ import { normalizeCommandArgs , NormalizedCommandArg , toParseArgOptions } from "./utils" ;
14
15
15
16
const defaultState : ucmdState < { } > = {
16
17
name : "" ,
@@ -43,16 +44,19 @@ class UCMD<TCommands extends CommandsLike, TBaseCommand> {
43
44
> (
44
45
command :
45
46
| LiteralString < TNewCommandName >
46
- | Partial < Command < TNewCommandArgs , LiteralString < TNewCommandName > > >
47
- | Command < TNewCommandArgs , LiteralString < TNewCommandName > > ,
47
+ | Command < TNewCommandArgs , LiteralString < TNewCommandName > >
48
+ | Partial < Command < TNewCommandArgs , LiteralString < TNewCommandName > > > ,
48
49
run ?: CommandFn < TNewCommandArgs extends infer T ? T : never > ,
49
50
) : UCMD < TUpdatedCommands , TBaseCommand > {
50
51
let newCMD = createCommand ( command , run ) ;
51
52
Object . assign ( this . #state. commands , { [ newCMD . name ] : newCMD } ) ;
52
53
return this as unknown as UCMD < TUpdatedCommands , TBaseCommand > ;
53
54
}
54
55
55
- withBaseCommand < TNewBaseCommand extends Command < CommandArgs , string > > ( ) : UCMD < TCommands , TNewBaseCommand > {
56
+ withBaseCommand < TNewBaseCommand extends BaseCommand < CommandArgs > > (
57
+ baseCommand : TNewBaseCommand ,
58
+ ) : UCMD < TCommands , TNewBaseCommand > {
59
+ ( this . #state as unknown as { baseCommand : TNewBaseCommand } ) . baseCommand = baseCommand ;
56
60
return this as unknown as UCMD < TCommands , TNewBaseCommand > ;
57
61
}
58
62
@@ -61,34 +65,32 @@ class UCMD<TCommands extends CommandsLike, TBaseCommand> {
61
65
62
66
let run : CommandFn < { } > = defaultCommand ;
63
67
let commandArgs = args || process . argv . slice ( 2 ) ;
64
- let command : string | undefined = undefined ;
65
- let options : CommandArg [ ] = [ ] ;
68
+ let command : string | undefined = commandArgs [ 0 ] ;
69
+ let options : NormalizedCommandArg [ ] = [ ] ;
66
70
67
71
// Get the command
68
- if ( commandArgs [ 0 ] && ! commandArgs [ 0 ] . startsWith ( "-" ) ) {
69
- command = commandArgs [ 0 ] ;
72
+ if ( command && ! command . startsWith ( "-" ) && this . #state. commands [ command ] ) {
70
73
commandArgs = commandArgs . slice ( 1 ) ;
71
74
72
- if ( ! this . #state. commands [ command ] ) return console . log ( "Command not found. Try --help" ) ;
73
75
run = this . #state. commands [ command ] ?. run ?? defaultCommand ;
74
- options = toCommandArgs ( this . #state. commands ?. [ command ] ?. args || [ ] ) ;
76
+ options = normalizeCommandArgs ( this . #state. commands ?. [ command ] ?. args || [ ] ) ;
75
77
}
76
78
77
79
// If no command is specified, run the baseCommand
78
- else if ( ! command ) {
80
+ else {
79
81
if ( ! this . #state. baseCommand ) {
80
82
console . log ( "No command specified" ) ;
81
83
return ;
82
84
}
83
85
84
86
run = this . #state. baseCommand . run ?? defaultCommand ;
85
- options = toCommandArgs ( this . #state. baseCommand . args || [ ] ) ;
86
- return ;
87
+ options = normalizeCommandArgs ( this . #state. baseCommand . args || [ ] ) ;
87
88
}
88
89
89
90
let res = parseArgs ( {
90
91
args : commandArgs ,
91
- options : generateOptions ( options ) ,
92
+ allowPositionals : true ,
93
+ options : toParseArgOptions ( options ) ,
92
94
} ) ;
93
95
94
96
return { res, run } ;
@@ -103,7 +105,6 @@ class UCMD<TCommands extends CommandsLike, TBaseCommand> {
103
105
}
104
106
105
107
export const ucmd = < T extends string > ( name : T ) => {
106
- console . log ( name ) ;
107
108
return new UCMD ( ) . withName ( name ) ;
108
109
} ;
109
110
0 commit comments