import { join , resolve } from 'path' ; import { v4 as uuidv4 } from 'uuid' ; import { BOT_CREDS_FILENAME , BOTONIC_HOME_DIRNAME , BOTONIC_PROJECT_PATH , GLOBAL_CREDS_FILENAME , } from '../constants' ; import { BotCredentials , CredentialsHandlerArgs , GlobalCredentials , JSONObject , } from '../interfaces' ; import { createDir , getHomeDirectory , pathExists , readJSON , writeJSON , } from '../util/file-system' ; export class CredentialsHandler { homeDir : string ; pathToCredentials : string ; constructor ( args ) { this . homeDir = resolve ( args . homeDir ) ; this . pathToCredentials = join ( this . homeDir , args . filename ) ; this . initialize ( ) ; } initialize ( ) { this . createDirIfNotExists ( ) ; } generateId ( ) { return uuidv4 ( ) ; } createDirIfNotExists ( ) { if ( ! pathExists ( this . homeDir ) ) createDir ( this . homeDir ) ; } loadJSON ( ) { try { if ( ! pathExists ( this . pathToCredentials ) ) return undefined ; return readJSON ( this . pathToCredentials ) ; } catch ( e ) { console . warn ( 'Credentials could not be loaded' ) ; return undefined ; } } dumpJSON ( obj ) { try { writeJSON ( this . pathToCredentials , obj ) ; } catch ( e ) { console . warn ( 'Credentials could not be overwritten' ) ; } } } export class GlobalCredentialsHandler extends CredentialsHandler { constructor ( ) { super ( { homeDir : join ( getHomeDirectory ( ) , BOTONIC_HOME_DIRNAME ) , filename : GLOBAL_CREDS_FILENAME , } ) ; } initialize ( ) { this . createDirIfNotExists ( ) ; if ( ! pathExists ( this . pathToCredentials ) || ! this . hasAnonymousId ( ) ) { this . refreshAnonymousId ( ) ; } } getAnonymousId ( ) { const content = this . load ( ) ; return content ?. analytics . anonymous_id ; } hasAnonymousId ( ) { if ( ! pathExists ( this . pathToCredentials ) ) return false ; return Boolean ( this . getAnonymousId ( ) ) ; } refreshAnonymousId ( ) { const newId = this . generateId ( ) ; this . dump ( { analytics : { anonymous_id : newId } , } ) ; return newId ; } load ( ) { const json = this . loadJSON ( ) ; if ( ! json ) return undefined ; return ( json as unknown ) as GlobalCredentials ; } dump ( obj ) { return this . dumpJSON ( ( obj as unknown ) as JSONObject ) ; } } export class BotCredentialsHandler extends CredentialsHandler { constructor ( ) { super ( { homeDir : BOTONIC_PROJECT_PATH , filename : BOT_CREDS_FILENAME , } ) ; } load ( ) { const json = this . loadJSON ( ) ; if ( ! json ) return undefined ; return ( json as unknown ) as BotCredentials ; } dump ( obj ) { return this . dumpJSON ( ( obj as unknown ) as JSONObject ) ; } }