1- import readline from "node:readline " ;
1+ import { confirm } from "@inquirer/prompts " ;
22import c from "chalk" ;
33import * as chrono from "chrono-node" ;
44import type { Command } from "commander" ;
55import dayjs from "dayjs" ;
66import duration from "dayjs/plugin/duration" ;
77import relativeTime from "dayjs/plugin/relativeTime" ;
88import parseDuration from "parse-duration" ;
9- import { loadConfig } from "../helpers/config" ;
9+ import { getAuthToken , isLoggedIn } from "../helpers/config" ;
1010import { logAndQuit , logLoginMessageAndQuit } from "../helpers/errors" ;
1111import { getApiUrl } from "../helpers/urls" ;
1212import {
@@ -33,30 +33,14 @@ export function registerBuy(program: Command) {
3333 } ) ;
3434}
3535
36- const rl = readline . createInterface ( {
37- input : process . stdin ,
38- output : process . stdout ,
39- } ) ;
40-
41- async function prompt ( msg : string ) {
42- const answer = await new Promise ( ( resolve ) =>
43- rl . question ( msg , ( ans ) => {
44- rl . close ( ) ;
45- resolve ( ans ) ;
46- } ) ,
47- ) ;
48-
49- return answer ;
50- }
51-
5236function confirmPlaceOrderParametersMessage ( params : PlaceOrderParameters ) {
5337 const { quantity, price, instance_type, duration, start_at } = params ;
5438
5539 const startDate = new Date ( start_at ) ;
5640
5741 const fromNowTime = dayjs ( startDate ) . fromNow ( ) ;
5842 const humanReadableStartAt = dayjs ( startDate ) . format ( "MM/DD/YYYY hh:mm A" ) ;
59- const centicentsAsDollars = ( price / 10000 ) . toFixed ( 2 ) ;
43+ const centicentsAsDollars = ( price / 10_000 ) . toFixed ( 2 ) ;
6044 const durationHumanReadable = formatDuration ( duration * 1000 ) ;
6145
6246 const topLine = `${ c . green ( quantity ) } ${ c . green ( instance_type ) } nodes for ${ c . green ( durationHumanReadable ) } starting ${ c . green ( humanReadableStartAt ) } (${ c . green ( fromNowTime ) } )` ;
@@ -95,11 +79,11 @@ interface PlaceBuyOrderArguments {
9579}
9680
9781async function placeBuyOrder ( props : PlaceBuyOrderArguments ) {
98- const { type, duration, price, quantity, start } = props ;
99- const config = await loadConfig ( ) ;
100- if ( ! config . auth_token ) {
82+ const loggedIn = await isLoggedIn ( ) ;
83+ if ( loggedIn ) {
10184 return logLoginMessageAndQuit ( ) ;
10285 }
86+ const { type, duration, price, quantity, start } = props ;
10387
10488 const orderQuantity = quantity ? Number ( quantity ) : 1 ;
10589 const durationMs = parseDuration ( duration ) ;
@@ -120,11 +104,13 @@ async function placeBuyOrder(props: PlaceBuyOrderArguments) {
120104 start_at : startDate . toISOString ( ) ,
121105 } ;
122106
123- const msg = confirmPlaceOrderParametersMessage ( params ) ;
124-
125107 if ( ! props . yes ) {
126- const answer = await prompt ( msg ) ;
127- if ( answer !== "y" ) {
108+ const placeBuyOrderConfirmed = await confirm ( {
109+ message : confirmPlaceOrderParametersMessage ( params ) ,
110+ default : false ,
111+ } ) ;
112+
113+ if ( ! placeBuyOrderConfirmed ) {
128114 return logAndQuit ( "Order cancelled" ) ;
129115 }
130116 }
@@ -134,7 +120,7 @@ async function placeBuyOrder(props: PlaceBuyOrderArguments) {
134120 body : JSON . stringify ( params ) ,
135121 headers : {
136122 "Content-Type" : "application/json" ,
137- Authorization : `Bearer ${ config . auth_token } ` ,
123+ Authorization : `Bearer ${ await getAuthToken ( ) } ` ,
138124 } ,
139125 } ) ;
140126
0 commit comments