import { existsSync , promises } from 'fs' ; import { join } from 'path' ; const packageDir = 'packages' ; const integrationDir = 'integration' ; const coverageTempDir = 'coverage-tmp' ; const coverageFinal = 'coverage-final.json' ; const coverageDir = 'coverage' ; async function readPackagesDirectory ( ) { return promises . readdir ( packageDir ) ; } async function readCoverageFile ( directory ) { return promises . readFile ( join ( process . cwd ( ) , packageDir , directory , coverageDir , coverageFinal ) ) ; } async function readIntegrationCoverage ( ) { return promises . readFile ( join ( process . cwd ( ) , integrationDir , coverageDir , coverageFinal ) ) ; } async function mapCoverages ( directories ) { const coverages = { } ; for ( const direct of directories ) { try { coverages [ direct ] = await readCoverageFile ( direct ) ; } catch { } } return coverages ; } async function writeCoveragesToSingleDir ( coverages ) { for ( const key of Object . keys ( coverages ) ) { await promises . writeFile ( join ( process . cwd ( ) , coverageTempDir , key + '-coverage.json' ) , coverages [ key ] ) ; } } async function migrateCoverage ( ) { if ( existsSync ( join ( process . cwd ( ) , coverageTempDir ) ) ) { await promises . rm ( coverageTempDir , { recursive : true } ) ; } const directories = await readPackagesDirectory ( ) ; const coverages = await mapCoverages ( directories ) ; coverages [ 'integration' ] = await readIntegrationCoverage ( ) ; await promises . mkdir ( join ( process . cwd ( ) , coverageTempDir ) ) ; await writeCoveragesToSingleDir ( coverages ) ; } migrateCoverage ( ) ;