import graphiteCLIRoutes from '@screenplaydev/graphite-cli-routes' ; import { request } from '@screenplaydev/retyped-routes' ; import fs from 'fs-extra' ; import os from 'os' ; import path from 'path' ; import { API_SERVER } from '../api' ; import { cliAuthPrecondition } from '../preconditions' ; import { SurveyResponseT } from '../telemetry/survey/survey' ; const SURVEY_CONFIG_NAME = '.graphite_beta_survey' ; const SURVEY_CONFIG_PATH = path . join ( os . homedir ( ) , SURVEY_CONFIG_NAME ) ; type TSurveyConfig = { responses : SurveyResponseT | undefined ; postingResponse : boolean ; } ; class SurveyConfig { _data : TSurveyConfig ; constructor ( data ) { this . _data = data ; } public setSurveyResponses ( responses ) { this . _data . responses = responses ; this . save ( ) ; } public hasSurveyResponse ( ) { return this . _data . responses !== undefined ; } public clearPriorSurveyResponse ( ) { this . _data . responses = undefined ; this . save ( ) ; } public async postResponses ( ) { try { const surveyResponse = this . _data . responses ; if ( surveyResponse === undefined ) { return false ; } const authToken = cliAuthPrecondition ( ) ; const response = await request . requestWithArgs ( API_SERVER , graphiteCLIRoutes . surveyResponse , { authToken : authToken , responses : { timestamp : surveyResponse . timestamp , responses : Object . keys ( surveyResponse . responses ) . map ( ( question ) => { return { question : question , response : surveyResponse . responses [ question ] , } ; } ) , exitedEarly : surveyResponse . exitedEarly , } , } ) ; if ( response . _response . status === 200 ) { return true ; } } catch ( e ) { } return false ; } public path ( ) { return SURVEY_CONFIG_PATH ; } private save ( ) { if ( this . _data . responses !== undefined ) { fs . writeFileSync ( SURVEY_CONFIG_PATH , JSON . stringify ( this . _data ) ) ; return ; } SurveyConfig . delete ( ) ; } static delete ( ) { if ( fs . existsSync ( SURVEY_CONFIG_PATH ) ) { fs . unlinkSync ( SURVEY_CONFIG_PATH ) ; return ; } } } function readSurveyConfig ( ) { if ( fs . existsSync ( SURVEY_CONFIG_PATH ) ) { const raw = fs . readFileSync ( SURVEY_CONFIG_PATH ) ; try { const parsedConfig = JSON . parse ( raw . toString ( ) . trim ( ) ) as TSurveyConfig ; return new SurveyConfig ( parsedConfig ) ; } catch ( e ) { SurveyConfig . delete ( ) ; } } return new SurveyConfig ( { responses : undefined , postingResponse : false , } ) ; } const surveyConfigSingleton = readSurveyConfig ( ) ; export default surveyConfigSingleton ;