1
1
import arg from "arg" ;
2
2
import { IUser } from "../types" ;
3
3
import fs from "fs" ;
4
- import { select , input } from "@inquirer/prompts" ;
5
- import { isValidAddress } from "../utils/helpers" ;
4
+ import { search , input } from "@inquirer/prompts" ;
5
+ import { isValidAddress , searchChallenges } from "../utils/helpers" ;
6
6
import { promptForMissingUserState } from "./prompt-for-missing-user-state" ;
7
7
8
8
type Commands = {
@@ -27,6 +27,28 @@ type SubmitCommand = {
27
27
28
28
export type CommandOptions = BaseOptions & { command : string | null } & SetupCommand & SubmitCommand ;
29
29
30
+ export type Choice < Value > = {
31
+ value : Value ;
32
+ name ?: string ;
33
+ description ?: string ;
34
+ short ?: string ;
35
+ disabled ?: boolean | string ;
36
+ } ;
37
+
38
+ type SearchOptions = {
39
+ type : "search" ;
40
+ name : string ;
41
+ message : string ;
42
+ source : ( term : string | undefined ) => Promise < Choice < string > [ ] > ;
43
+ }
44
+
45
+ type InputOptions = {
46
+ type : "input" ;
47
+ name : string ;
48
+ message : string ;
49
+ validate : ( value : string ) => string | true ;
50
+ }
51
+
30
52
const commandArguments = {
31
53
setup : {
32
54
1 : "challenge" ,
@@ -76,9 +98,6 @@ export async function parseCommandArgumentsAndOptions(
76
98
}
77
99
78
100
export async function promptForMissingCommandArgs ( commands : CommandOptions , userState : IUser ) : Promise < CommandOptions > {
79
- const cliAnswers = Object . fromEntries (
80
- Object . entries ( commands ) . filter ( ( [ key , value ] ) => value !== null )
81
- ) ;
82
101
const questions = [ ] ;
83
102
84
103
const { command, challenge, contractAddress } = commands ;
@@ -90,9 +109,10 @@ export async function promptForMissingCommandArgs(commands: CommandOptions, user
90
109
if ( command === "setup" ) {
91
110
if ( ! challenge ) {
92
111
questions . push ( {
93
- type : "input " ,
112
+ type : "search " ,
94
113
name : "challenge" ,
95
114
message : "Which challenge would you like to setup?" ,
115
+ source : searchChallenges
96
116
} ) ;
97
117
}
98
118
if ( ! installLocation ) {
@@ -108,28 +128,34 @@ export async function promptForMissingCommandArgs(commands: CommandOptions, user
108
128
109
129
if ( command === "submit" ) {
110
130
// Need user state so direct to promptForMissingUserState
111
- await promptForMissingUserState ( userState ) ;
131
+ await promptForMissingUserState ( userState , true ) ;
112
132
113
133
if ( ! challenge ) {
114
134
questions . push ( {
115
- type : "input " ,
135
+ type : "search " ,
116
136
name : "challenge" ,
117
137
message : "Which challenge would you like to submit?" ,
138
+ source : searchChallenges
118
139
} ) ;
119
140
}
120
141
if ( ! contractAddress ) {
121
142
questions . push ( {
122
143
type : "input" ,
123
144
name : "contractAddress" ,
124
- message : "What is the deployed contract address?" ,
125
- validate : isValidAddress ,
145
+ message : "What is the contract address of your completed challenge ?" ,
146
+ validate : ( value : string ) => isValidAddress ( value ) ? true : "Please enter a valid contract address" ,
126
147
} ) ;
127
148
}
128
149
}
129
- const answers = [ ] ;
150
+ const answers : Record < string , string > = { } ;
130
151
for ( const question of questions ) {
131
- const answer = await input ( question ) ;
132
- answers . push ( answer ) ;
152
+ if ( question . type === "search" ) {
153
+ const answer = await search ( question as unknown as SearchOptions ) ;
154
+ answers [ question . name ] = answer ;
155
+ } else if ( question . type === "input" ) {
156
+ const answer = await input ( question as InputOptions ) ;
157
+ answers [ question . name ] = answer ;
158
+ }
133
159
}
134
160
135
161
return {
0 commit comments