import { Argv } from 'yargs' ; import chalk from 'chalk' ; import { prompt } from 'inquirer' ; import { CommandOptions , LogSettings } from './types/' ; import { Ora } from 'ora' ; import ora = require ( 'ora' ) ; returns export const cmd = ( name , desc ) : CommandOptions => ( { name : name , command : `${ name } <command> [options]` , desc : desc , builder : ( yargs ) => yargs . commandDir ( `commands/${ name }` ) , } ) ; returns export const pluralize = ( count , singular , plural ) : string => ( count > 1 || count === 0 ? plural : singular ) ; returns export const log = ( text , settings ? ) : void => { if ( settings && settings . error ) { return console . error ( chalk . red ( text ) ) ; } if ( settings && settings . warn ) { return console . warn ( chalk . yellow ( text ) ) ; } if ( settings && settings . color ) { return console . log ( chalk [ settings . color ] ( text ) ) ; } return console . log ( text ) ; } ; returns export const serverTokenPrompt = ( account ) : Promise < string > => new Promise < string > ( ( resolve , reject ) => { const tokenType = account ? 'account' : 'server' ; prompt ( [ { type : 'password' , name : 'token' , message : `Please enter your ${ tokenType } token` , mask : '•' , } , ] ) . then ( ( answer ) => { const { token } = answer ; if ( ! token ) { log ( `Invalid ${ tokenType } token` , { error : true } ) ; process . exit ( 1 ) ; return reject ( ) ; } return resolve ( token ) ; } ) ; } ) ; return export const validateToken = ( token , account = false ) : Promise < string > => new Promise < string > ( resolve => { if ( ! token ) { return serverTokenPrompt ( account ) . then ( tokenPrompt => resolve ( tokenPrompt ) ) ; } return resolve ( token ) ; } ) ; /**
 * Handle starting/stopping spinner and console output
 */ export class CommandResponse { private spinner : Ora ; public constructor ( ) { this . spinner = ora ( ) . clear ( ) ; } public initResponse ( message ) { this . spinner = ora ( message ) . start ( ) ; } public response ( text , settings ? ) { this . spinner . stop ( ) ; log ( text , settings ) ; } public errorResponse ( error , showJsonError = false ) { this . spinner . stop ( ) ; if ( showJsonError === true ) { log ( JSON . stringify ( error ) , { error : true } ) ; } log ( error , { error : true } ) ; process . exit ( 1 ) ; } }