import * as vscode from 'vscode' ; import * as path from 'path' ; import { RenderError , IRender } from '../renders/interfaces' ; import { Diagram } from '../diagram/diagram' ; import { config } from '../config' ; import { localize } from '../common' ; import { mkdirsSync , parseError , calculateExportPath } from '../tools' ; import { appliedRender } from './appliedRender' ; import { exportDiagram } from './exportDiagram' ; param diagrams param format param bar returns export function exportDiagrams ( diagrams , format , bar ) { if ( ! diagrams || ! diagrams . length ) { return Promise . resolve ( [ ] ) ; } if ( appliedRender ( diagrams [ 0 ] . parentUri ) . limitConcurrency ( ) ) { let concurrency = config . exportConcurrency ( diagrams [ 0 ] . parentUri ) ; return doExportsLimited ( diagrams , format , concurrency , bar ) ; } else { return doExportsUnLimited ( diagrams , format , bar ) ; } } param diagrams param format param bar returns function doExportsUnLimited ( diagrams , format , bar ) { let errors = [ ] ; let promises = diagrams . map ( ( diagram , index ) => { if ( ! path . isAbsolute ( diagram . dir ) ) return Promise . reject ( localize ( 1 , null ) ) ; let savePath = calculateExportPath ( diagram , format . split ( ":" ) [ 0 ] ) ; mkdirsSync ( path . dirname ( savePath ) ) ; return exportDiagram ( diagram , format , savePath , bar ) . promise . then ( r => r , err => { errors . push ( ... parseError ( err ) ) ; return [ ] ; } ) ; } ) ; return new Promise ( ( resolve , reject ) => { Promise . all ( promises ) . then ( r => { if ( errors . length ) reject ( errors ) ; else resolve ( r ) ; } , e => { errors . push ( ... parseError ( e ) ) ; reject ( errors ) ; } ) ; } ) ; } param diagrams param format param concurrency param bar returns function doExportsLimited ( diagrams , format , concurrency , bar ) { concurrency = concurrency > 0 ? concurrency : 1 ; concurrency = concurrency > diagrams . length ? diagrams . length : concurrency ; let promises = [ ] ; let errors = [ ] ; for ( let i = 0 ; i < concurrency ; i ++ ) { promises . push ( diagrams . reduce ( ( prev , diagram , index ) => { if ( index % concurrency != i ) { return prev ; } if ( ! path . isAbsolute ( diagram . dir ) ) return Promise . reject ( localize ( 1 , null ) ) ; let savePath = calculateExportPath ( diagram , format . split ( ":" ) [ 0 ] ) ; mkdirsSync ( path . dirname ( savePath ) ) ; return prev . then ( ( ) => { return exportDiagram ( diagram , format , savePath , bar ) . promise ; } , err => { errors . push ( ... parseError ( err ) ) ; return exportDiagram ( diagram , format , savePath , bar ) . promise ; } ) ; } , Promise . resolve ( < Buffer [ ] > [ ] ) ) . then ( r => { return r ; } , err => { errors . push ( ... parseError ( err ) ) ; return [ ] ; } ) ) ; } let all = Promise . all ( promises ) ; return new Promise ( ( resolve , reject ) => { all . then ( r => { if ( errors . length ) reject ( errors ) ; else resolve ( r ) ; } ) ; } ) ; }