11import arg from "arg" ;
22import { IUser } from "../types" ;
33import 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" ;
66import { promptForMissingUserState } from "./prompt-for-missing-user-state" ;
77
88type Commands = {
@@ -27,6 +27,28 @@ type SubmitCommand = {
2727
2828export type CommandOptions = BaseOptions & { command : string | null } & SetupCommand & SubmitCommand ;
2929
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+
3052const commandArguments = {
3153 setup : {
3254 1 : "challenge" ,
@@ -76,9 +98,6 @@ export async function parseCommandArgumentsAndOptions(
7698}
7799
78100export async function promptForMissingCommandArgs ( commands : CommandOptions , userState : IUser ) : Promise < CommandOptions > {
79- const cliAnswers = Object . fromEntries (
80- Object . entries ( commands ) . filter ( ( [ key , value ] ) => value !== null )
81- ) ;
82101 const questions = [ ] ;
83102
84103 const { command, challenge, contractAddress } = commands ;
@@ -90,9 +109,10 @@ export async function promptForMissingCommandArgs(commands: CommandOptions, user
90109 if ( command === "setup" ) {
91110 if ( ! challenge ) {
92111 questions . push ( {
93- type : "input " ,
112+ type : "search " ,
94113 name : "challenge" ,
95114 message : "Which challenge would you like to setup?" ,
115+ source : searchChallenges
96116 } ) ;
97117 }
98118 if ( ! installLocation ) {
@@ -108,28 +128,34 @@ export async function promptForMissingCommandArgs(commands: CommandOptions, user
108128
109129 if ( command === "submit" ) {
110130 // Need user state so direct to promptForMissingUserState
111- await promptForMissingUserState ( userState ) ;
131+ await promptForMissingUserState ( userState , true ) ;
112132
113133 if ( ! challenge ) {
114134 questions . push ( {
115- type : "input " ,
135+ type : "search " ,
116136 name : "challenge" ,
117137 message : "Which challenge would you like to submit?" ,
138+ source : searchChallenges
118139 } ) ;
119140 }
120141 if ( ! contractAddress ) {
121142 questions . push ( {
122143 type : "input" ,
123144 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" ,
126147 } ) ;
127148 }
128149 }
129- const answers = [ ] ;
150+ const answers : Record < string , string > = { } ;
130151 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+ }
133159 }
134160
135161 return {
0 commit comments