file copyright author Vivek M. Chawla <@VivekMChawla>
 *  summary description version license import { ConfigFile } from '@salesforce/core' ; import { JsonMap } from '@salesforce/ts-types' ; import * as path from 'path' ; import { SfdxFalconDebug } from '../sfdx-falcon-debug' ; import { SfdxFalconError } from '../sfdx-falcon-error' ; const uuid = require ( 'uuid/v1' ) ; const dbgNs = 'UTILITY:general:' ; function param { string } baseUsername returns { string } description version public export function createUniqueUsername ( baseUsername ) { const usernameMaxLength = 35 ; if ( typeof baseUsername === 'undefined' ) throw new SfdxFalconError ( `Function createUniqueUsername() expects a value for baseUsername but got undefined` , `InvalidArgument` ) ; if ( baseUsername . length > usernameMaxLength ) throw new SfdxFalconError ( `Username can not be longer than ${ usernameMaxLength } chars to keep room for appending a UUID` , `InvalidUsername` ) ; return baseUsername + uuid ( ) ; } function param { string } rootFolder param { string } filename returns { Promise < any > } description version public async export async function readConfigFile ( rootFolder , filename ) { const filePath = path . join ( rootFolder , filename ) ; const configFileOptions = { rootFolder : rootFolder , filename : filename , isGlobal : false , isState : false } ; SfdxFalconDebug . obj ( `${ dbgNs }readConfigFile:configFileOptions:` , configFileOptions , `configFileOptions: ` ) ; const configFile = await ConfigFile . create ( configFileOptions ) ; if ( await configFile . exists ( ) === false ) { throw new SfdxFalconError ( `Config file does not exist - ${ filePath }` , `FileNotFound` ) ; } SfdxFalconDebug . obj ( `${ dbgNs }readConfigFile:configFile:` , configFile , `configFile: ` ) ; return configFile . toObject ( ) as JsonMap ; } function param { any } contentToParse returns { object } description public export function safeParse ( contentToParse ) { SfdxFalconDebug . obj ( `${ dbgNs }safeParse:contentToParse:` , { contentToParse : contentToParse } , `contentToParse: ` ) ; try { return JSON . parse ( contentToParse ) ; } catch ( e ) { return { unparsed : `${ contentToParse }` } ; } } function param { string } contentToSearch returns { JsonMap } description public export function findJson ( contentToSearch ) { SfdxFalconDebug . str ( `${ dbgNs }findJson:contentToSearch:` , contentToSearch , `contentToSearch: ` ) ; const possibleJson = contentToSearch . substring ( contentToSearch . indexOf ( '{' ) , contentToSearch . lastIndexOf ( '}' ) + 1 ) ; let foundJson = safeParse ( possibleJson ) ; if ( foundJson . hasOwnProperty ( 'unparsed' ) ) { foundJson = null ; } return foundJson ; }