import * as fs from "fs" ; import * as defaults from "./defaults" ; import { UsageDataLevel } from "./usageData" ; param groupName returns export function needToPromptForUsageData ( groupName ) { return ! groupNameExists ( groupName ) ; } param groupName param property param value export function modifyUsageDataJsonData ( groupName , property , value ) { try { if ( readUsageDataJsonData ( ) ) { const usageDataJsonData = readUsageDataJsonData ( ) ; if ( ! groupNameExists ( groupName ) ) { usageDataJsonData . usageDataInstances [ groupName ] = { usageDataLevel : String , } ; } usageDataJsonData . usageDataInstances [ groupName ] [ property ] = value ; fs . writeFileSync ( defaults . usageDataJsonFilePath , JSON . stringify ( usageDataJsonData , null , 2 ) ) ; } else { let usageDataJsonData = { } ; usageDataJsonData [ groupName ] = value ; usageDataJsonData = { usageDataInstances : usageDataJsonData } ; usageDataJsonData = { usageDataInstances : { [ groupName ] : { [ property ] : value } } , } ; fs . writeFileSync ( defaults . usageDataJsonFilePath , JSON . stringify ( usageDataJsonData , null , 2 ) ) ; } } catch ( err ) { throw new Error ( err ) ; } } returns export function readUsageDataJsonData ( ) { if ( fs . existsSync ( defaults . usageDataJsonFilePath ) ) { const jsonData = fs . readFileSync ( defaults . usageDataJsonFilePath , "utf8" ) ; return JSON . parse ( jsonData . toString ( ) ) ; } } param groupName returns export function readUsageDataLevel ( groupName ) { const jsonData = readUsageDataJsonData ( ) ; return jsonData . usageDataInstances [ groupName ] . usageDataLevel ; } param groupName param propertyName returns export function readUsageDataObjectProperty ( groupName , propertyName ) { const jsonData = readUsageDataJsonData ( ) ; return jsonData . usageDataInstances [ groupName ] [ propertyName ] ; } param groupName param level export function writeUsageDataJsonData ( groupName , level ) { if ( fs . existsSync ( defaults . usageDataJsonFilePath ) && fs . readFileSync ( defaults . usageDataJsonFilePath , "utf8" ) !== "" && fs . readFileSync ( defaults . usageDataJsonFilePath , "utf8" ) !== "undefined" ) { if ( groupNameExists ( groupName ) ) { modifyUsageDataJsonData ( groupName , "usageDataLevel" , level ) ; } else { const usageDataJsonData = readUsageDataJsonData ( ) ; usageDataJsonData . usageDataInstances [ groupName ] = { usageDataLevel : String , } ; usageDataJsonData . usageDataInstances [ groupName ] . usageDataLevel = level ; fs . writeFileSync ( defaults . usageDataJsonFilePath , JSON . stringify ( usageDataJsonData , null , 2 ) ) ; } } else { let usageDataJsonData = { } ; usageDataJsonData [ groupName ] = level ; usageDataJsonData = { usageDataInstances : usageDataJsonData } ; usageDataJsonData = { usageDataInstances : { [ groupName ] : { [ "usageDataLevel" ] : level } } , } ; fs . writeFileSync ( defaults . usageDataJsonFilePath , JSON . stringify ( usageDataJsonData , null , 2 ) ) ; } } param groupName returns export function groupNameExists ( groupName ) { if ( fs . existsSync ( defaults . usageDataJsonFilePath ) && fs . readFileSync ( defaults . usageDataJsonFilePath , "utf8" ) !== "" && fs . readFileSync ( defaults . usageDataJsonFilePath , "utf8" ) !== "undefined" ) { const jsonData = readUsageDataJsonData ( ) ; return Object . getOwnPropertyNames ( jsonData . usageDataInstances ) . includes ( groupName ) ; } return false ; } returns export function readUsageDataSettings ( groupName = defaults . groupName ) { if ( fs . existsSync ( defaults . usageDataJsonFilePath ) ) { return readUsageDataJsonData ( ) . usageDataInstances [ groupName ] ; } else { return undefined ; } }