import type * as d from '../declarations' ; import { readJson , uuidv4 , UUID_REGEX } from './telemetry/helpers' ; export const isTest = ( ) => process . env . JEST_WORKER_ID !== undefined ; export const defaultConfig = ( sys ) => sys . resolvePath ( `${ sys . homeDir ( ) }/.ionic/${ isTest ( ) ? 'tmp-config.json' : 'config.json' }` ) ; export const defaultConfigDirectory = ( sys ) => sys . resolvePath ( `${ sys . homeDir ( ) }/.ionic` ) ; param sys returns export async function readConfig ( sys ) { let config = await readJson ( sys , defaultConfig ( sys ) ) ; if ( ! config ) { config = { 'tokens.telemetry' : uuidv4 ( ) , 'telemetry.stencil' : true , } ; await writeConfig ( sys , config ) ; } else if ( ! UUID_REGEX . test ( config [ 'tokens.telemetry' ] ) ) { const newUuid = uuidv4 ( ) ; await writeConfig ( sys , { ... config , 'tokens.telemetry' : newUuid } ) ; config [ 'tokens.telemetry' ] = newUuid ; } return config ; } param sys param config returns export async function writeConfig ( sys , config ) { let result = false ; try { await sys . createDir ( defaultConfigDirectory ( sys ) , { recursive : true } ) ; await sys . writeFile ( defaultConfig ( sys ) , JSON . stringify ( config , null , 2 ) ) ; result = true ; } catch ( error ) { console . error ( `Stencil Telemetry: couldn't write configuration file to ${ defaultConfig ( sys ) } - ${ error }.` ) ; } return result ; } param sys param newOptions returns export async function updateConfig ( sys , newOptions ) { const config = await readConfig ( sys ) ; return await writeConfig ( sys , Object . assign ( config , newOptions ) ) ; }