@@ -2,15 +2,25 @@ import { parseArgs } from "node:util";
22
33type CLICommand = "help" | "build" ;
44
5- type Flags = { } & Record < string , never > ;
5+ type Flags = { watch : boolean } ;
66
77interface Args {
88 values : Flags ;
99 positionals : string [ ] ;
1010}
1111
1212async function printHelp ( ) {
13- console . log ( "Run `usts build` to build a userscript" ) ;
13+ console . log ( `
14+ usts build - build a userscript
15+ usts build --watch - build userscript in watch mode
16+ ` ) ;
17+ }
18+
19+ function isSupportedCommand < T extends CLICommand > (
20+ supportedCommands : T [ ] ,
21+ cmd : string ,
22+ ) : cmd is T {
23+ return new Set < string > ( supportedCommands ) . has ( cmd ) ;
1424}
1525
1626function resolveCommand ( parsedArgs : Args ) : CLICommand {
@@ -20,30 +30,33 @@ function resolveCommand(parsedArgs: Args): CLICommand {
2030 return "help" ;
2131 }
2232
23- const supportedCommands = new Set ( [ "build" ] ) ;
24- if ( supportedCommands . has ( cmd ) ) {
25- return cmd as CLICommand ;
33+ if ( isSupportedCommand ( [ "build" ] , cmd ) ) {
34+ return cmd ;
2635 }
2736
2837 return "help" ;
2938}
3039
31- async function runCommand ( cmd : CLICommand ) {
40+ async function runCommand ( cmd : CLICommand , flags : Flags ) {
3241 switch ( cmd ) {
3342 case "help" : {
3443 await printHelp ( ) ;
3544 return ;
3645 }
3746 case "build" : {
3847 const { build } = await import ( "./build/index.js" ) ;
39- await build ( ) ;
48+ await build ( { watch : flags . watch } ) ;
4049 return ;
4150 }
4251 }
4352}
4453
4554export async function cli ( argv : string [ ] ) : Promise < void > {
46- const parsedArgs = parseArgs ( { args : argv , allowPositionals : true } ) ;
55+ const parsedArgs = parseArgs ( {
56+ args : argv ,
57+ allowPositionals : true ,
58+ options : { watch : { type : "boolean" , default : false } } ,
59+ } ) ;
4760 const cmd = resolveCommand ( parsedArgs ) ;
48- await runCommand ( cmd ) ;
61+ await runCommand ( cmd , parsedArgs . values ) ;
4962}
0 commit comments