import Fs from 'fs' ; import Path from 'path' ; import { promisify } from 'util' ; import { REPO_ROOT } from '@kbn/utils' ; import { OptimizerConfig , runOptimizer , logOptimizerState } from '@kbn/optimizer' ; import { BuildContext } from '../build_context' ; const asyncRename = promisify ( Fs . rename ) ; export async function optimize ( { log , plugin , sourceDir , buildDir } ) { if ( ! plugin . manifest . ui ) { return ; } log . info ( 'running @kbn/optimizer' ) ; log . indent ( 2 ) ; const config = OptimizerConfig . create ( { repoRoot : REPO_ROOT , pluginPaths : [ sourceDir ] , cache : false , dist : true , filter : [ plugin . manifest . id ] , } ) ; const target = Path . resolve ( sourceDir , 'target' ) ; await runOptimizer ( config ) . pipe ( logOptimizerState ( log , config ) ) . toPromise ( ) ; Fs . unlinkSync ( Path . resolve ( target , 'public/metrics.json' ) ) ; Fs . unlinkSync ( Path . resolve ( target , 'public/.kbn-optimizer-cache' ) ) ; await asyncRename ( target , Path . resolve ( buildDir , 'target' ) ) ; log . indent ( - 2 ) ; }