file copyright author Vivek M. Chawla <@VivekMChawla>
 *  summary description version license import * as path from 'path' ; import { SfdxFalconDebug } from '../../modules/sfdx-falcon-debug' ; import { SfdxFalconError } from '../../modules/sfdx-falcon-error' ; import { SfdxOrgInfo } from '../../modules/sfdx-falcon-util/sfdx' ; import { StatusMessage } from '../../modules/sfdx-falcon-util/ux' ; import { printStatusMessages } from '../../modules/sfdx-falcon-util/ux' ; import { YeomanChoice } from '../../modules/sfdx-falcon-types' ; const pad = require ( 'pad' ) ; const dbgNs = 'UTILITY:yeoman:' ; class summary description public export class YeomanSeparator { public name : string ; public value : string ; public short : string ; public type : string ; public line ? : string ; constructor ( separatorLine ? ) { this . name = '' ; this . value = '' ; this . short = '' ; this . type = 'separator' ; if ( typeof separatorLine !== 'undefined' ) { this . line = separatorLine ; } } } class summary description public export class GeneratorStatus { public aborted : boolean ; public completed : boolean ; public running : boolean ; public messages : StatusMessage [ ] ; constructs description public constructor ( ) { this . aborted = false ; this . running = false ; this . completed = false ; this . messages = new Array < StatusMessage > ( ) ; } method param { statusMessage } statusMessage returns { void } description public public start ( statusMessage ? ) { if ( this . aborted || this . completed ) { throw new SfdxFalconError ( `Can not call start() on an aborted or completed Generator Status object` , `GeneratorStatusError` , `${ dbgNs }GeneratorStatus:start` ) ; } this . running = true ; this . aborted = false ; this . completed = false ; if ( typeof statusMessage !== 'undefined' ) { this . messages . push ( statusMessage ) ; } } method param { statusMessage } statusMessage returns { void } description public public abort ( statusMessage ) { if ( this . completed || this . running === false ) { throw new SfdxFalconError ( `Can not call abort() on a non-running or completed Generator Status object` , `GeneratorStatusError` , `${ dbgNs }GeneratorStatus:abort` ) ; } this . aborted = true ; this . completed = false ; this . running = false ; if ( typeof statusMessage !== 'undefined' ) { this . messages . push ( statusMessage ) ; } } method param { statusMessage [ ] } statusMessages returns { void } description public public complete ( statusMessages ? ) { if ( this . aborted || this . running === false ) { throw new SfdxFalconError ( `Can not call complete() on a non-running or aborted Generator Status object` , `GeneratorStatusError` , `${ dbgNs }GeneratorStatus:complete` ) ; } this . completed = true ; this . aborted = false ; this . running = false ; if ( Array . isArray ( statusMessages ) ) { for ( const statusMessage of statusMessages ) { this . messages . push ( statusMessage ) ; } } } method param { statusMessage } statusMessage returns { void } description public public addMessage ( statusMessage ) { if ( typeof statusMessage . title !== 'string' ) { throw new SfdxFalconError ( `Expected string for statusMessage.title but got '${ typeof statusMessage . title }'` , `TypeError` , `${ dbgNs }GeneratorStatus:addMessage` ) ; } if ( typeof statusMessage . message !== 'string' ) { throw new SfdxFalconError ( `Expected string for statusMessage.message but got '${ typeof statusMessage . message }'` , `TypeError` , `${ dbgNs }GeneratorStatus:addMessage` ) ; } if ( typeof statusMessage . type !== 'string' ) { throw new SfdxFalconError ( `Expected string for statusMessage.type but got '${ typeof statusMessage . type }'` , `TypeError` , `${ dbgNs }GeneratorStatus:addMessage` ) ; } this . messages . push ( statusMessage ) ; } method returns { void } description public public printStatusMessages ( ) { printStatusMessages ( this . messages ) ; } } function param { SfdxOrgInfo } alias param { number } longestAlias param { number } longestUsername returns { YeomanChoice } description private function createOrgAliasChoice ( orgInfo , longestAlias , longestUsername ) { SfdxFalconDebug . obj ( `${ dbgNs }createOrgAliasChoice:` , arguments , `arguments: ` ) ; return { name : `${ pad ( orgInfo . alias , longestAlias ) } -- ${ pad ( orgInfo . username , longestUsername ) }${ orgInfo . nsPrefix ? ' [' + orgInfo . nsPrefix + ']' : '' }` , value : orgInfo . username , short : ( typeof orgInfo . alias !== 'undefined' && orgInfo . alias !== '' ) ? `${ orgInfo . alias } (${ orgInfo . username })${ orgInfo . nsPrefix ? ' [' + orgInfo . nsPrefix + ']' : '' }` : orgInfo . username + ( orgInfo . nsPrefix ? '[' + orgInfo . nsPrefix + ']' : '' ) } ; } function param { string } alias param { string } username param { number } padLength returns { YeomanChoice } description public export function buildOrgAliasChoices ( sfdxOrgInfos ) { SfdxFalconDebug . obj ( `${ dbgNs }buildOrgAliasChoices:` , arguments , `arguments: ` ) ; const orgAliasChoices = new Array < YeomanChoice > ( ) ; let longestAlias = 0 ; for ( const orgInfo of sfdxOrgInfos ) { if ( typeof orgInfo . alias !== 'undefined' ) { longestAlias = Math . max ( orgInfo . alias . length , longestAlias ) ; } } let longestUsername = 0 ; for ( const orgInfo of sfdxOrgInfos ) { if ( typeof orgInfo . username !== 'undefined' ) { longestUsername = Math . max ( orgInfo . username . length , longestUsername ) ; } } for ( const orgInfo of sfdxOrgInfos ) { orgAliasChoices . push ( createOrgAliasChoice ( orgInfo , longestAlias , longestUsername ) ) ; } SfdxFalconDebug . obj ( `${ dbgNs }buildOrgAliasChoices:` , orgAliasChoices , `orgAliasChoices: ` ) ; return orgAliasChoices ; } function param { string } localPath returns { string } description export function filterLocalPath ( localPath ) { SfdxFalconDebug . obj ( `${ dbgNs }filterLocalPath:` , arguments , `arguments: ` ) ; if ( typeof localPath !== 'string' ) { throw new SfdxFalconError ( `Expected boolean for localPath but got '${ typeof localPath }'` , `TypeError` , `${ dbgNs }filterLocalPath` ) ; } return path . resolve ( localPath ) ; }