import { resolve , dirname , relative } from 'path' ; import { stat , Stats , rename , createReadStream , createWriteStream } from 'fs' ; import { Readable , Writable } from 'stream' ; import { fromNode } from 'bluebird' ; import { ToolingLog } from '@osd/dev-utils' ; import { createPromiseFromStreams } from '../lib/streams' ; import { prioritizeMappings , readDirectory , isGzip , createParseArchiveStreams , createFormatArchiveStreams , } from '../lib' ; async function isDirectory ( path ) { const stats = await fromNode ( ( cb ) => stat ( path , cb ) ) ; return stats . isDirectory ( ) ; } export async function rebuildAllAction ( { dataDir , log , rootDir = dataDir , } ) { const childNames = prioritizeMappings ( await readDirectory ( dataDir ) ) ; for ( const childName of childNames ) { const childPath = resolve ( dataDir , childName ) ; if ( await isDirectory ( childPath ) ) { await rebuildAllAction ( { dataDir : childPath , log , rootDir , } ) ; continue ; } const archiveName = dirname ( relative ( rootDir , childPath ) ) ; log . info ( `${ archiveName } Rebuilding ${ childName }` ) ; const gzip = isGzip ( childPath ) ; const tempFile = childPath + ( gzip ? '.rebuilding.gz' : '.rebuilding' ) ; await createPromiseFromStreams ( [ createReadStream ( childPath ) as Readable , ... createParseArchiveStreams ( { gzip } ) , ... createFormatArchiveStreams ( { gzip } ) , createWriteStream ( tempFile ) , ] as [ Readable , ... Writable [ ] ] ) ; await fromNode ( ( cb ) => rename ( tempFile , childPath , cb ) ) ; log . info ( `${ archiveName } Rebuilt ${ childName }` ) ; } }