import enquirer from "enquirer" ; import pLimit from "p-limit" ; import DataLoader from "dataloader" ; import chalk from "chalk" ; export let limit = pLimit ( 1 ) ; let prefix = `🎁 ${ chalk . green ( "?" ) }` ; type NamedThing = { readonly name : string ; } ; export function createPromptConfirmLoader ( message ) { let loader = new DataLoader < NamedThing , boolean > ( ( pkgs ) => limit ( ( ) => ( async ( ) => { if ( pkgs . length === 1 ) { let { confirm } = await enquirer . prompt ( [ { type : "confirm" , name : "confirm" , message , prefix : prefix + " " + pkgs [ 0 ] . name , initial : true , } , ] ) ; return [ confirm ] ; } let { answers } = await enquirer . prompt ( [ { type : "multiselect" as const , name : "answers" , message , choices : pkgs . map ( ( pkg ) => ( { name : pkg . name , initial : true } ) ) , prefix , } , ] ) ; return pkgs . map ( ( pkg ) => { return answers . includes ( pkg . name ) ; } ) ; } ) ( ) ) ) ; return ( pkg ) => loader . load ( pkg ) ; } export let promptConfirm = async ( message ) : Promise < boolean > => { let { confirm } = await enquirer . prompt ( [ { type : "confirm" , name : "confirm" , message , prefix : prefix , initial : true , } , ] ) ; return confirm ; } ; export let doPromptInput = async ( message , pkg , defaultAnswer ? ) : Promise < string > => { let { input } = await enquirer . prompt ( [ { type : "input" , name : "input" , message , prefix : prefix + " " + pkg . name , initial : defaultAnswer , } , ] ) ; return input ; } ; export let promptInput = ( message , pkg , defaultAnswer ? ) => limit ( ( ) => doPromptInput ( message , pkg , defaultAnswer ) ) ;