import { resolve } from 'path' ; import { createReadStream } from 'fs' ; import { Readable , Writable } from 'stream' ; import { Client } from 'elasticsearch' ; import { ToolingLog , OsdClient } from '@osd/dev-utils' ; import { createPromiseFromStreams } from '../lib/streams' ; import { isGzip , createStats , prioritizeMappings , readDirectory , createParseArchiveStreams , createFilterRecordsStream , createDeleteIndexStream , } from '../lib' ; export async function unloadAction ( { name , client , dataDir , log , osdClient : osdClient , } ) { const inputDir = resolve ( dataDir , name ) ; const stats = createStats ( name , log ) ; const opensearchDashboardsPluginIds = await osdClient . plugins . getEnabledIds ( ) ; const files = prioritizeMappings ( await readDirectory ( inputDir ) ) ; for ( const filename of files ) { log . info ( '[%s] Unloading indices from %j' , name , filename ) ; await createPromiseFromStreams ( [ createReadStream ( resolve ( inputDir , filename ) ) as Readable , ... createParseArchiveStreams ( { gzip : isGzip ( filename ) } ) , createFilterRecordsStream ( 'index' ) , createDeleteIndexStream ( client , stats , log , opensearchDashboardsPluginIds ) , ] as [ Readable , ... Writable [ ] ] ) ; } return stats . toJSON ( ) ; }