import { execSync } from 'child_process' ; import { basename } from 'path' ; import { cleanupTempFiles } from './cleanup' ; import { getConvertedFilePath } from './logs' ; import { unpack } from './unpack' ; export const defaultArgs = [ '--headless' , '--invisible' , '--nodefault' , '--view' , '--nolockcheck' , '--nologo' , '--norestore' , '--nofirststartwizard' ] ; const INPUT_PATH = '/opt/lo.tar.br' ; const OUTPUT_PATH = '/tmp/instdir/program/soffice.bin' ; const UNOPKG_OUTPUT_PATH = '/tmp/instdir/program/unopkg.bin' ; type ExtensionOptions = { extensions : string [ ] ; shouldThrowOnExtensionFail ? : boolean ; } ; param { String } filename param { String } format param { ExtensionOptions } options return { Promise < String > } export async function convertTo ( filename , format , options ? ) { let logs ; cleanupTempFiles ( ) ; await unpack ( { inputPath : INPUT_PATH } ) ; if ( options && options . extensions && options . extensions . length ) { const { extensions , shouldThrowOnExtensionFail = true } = options ; const enabledExtensions = execSync ( `${ UNOPKG_OUTPUT_PATH } list --shared` ) . toString ( ) ; extensions . forEach ( extension => { enableExtension ( enabledExtensions , extension , shouldThrowOnExtensionFail ) ; } ) ; } const cmd = `cd /tmp && ${ OUTPUT_PATH } ${ defaultArgs . join ( ' ' ) } --convert-to ${ format } --outdir /tmp /tmp/${ filename . split ( /\ / ) . join ( '\ ' ) }` ; try { logs = execSync ( cmd ) ; } catch ( e ) { logs = execSync ( cmd ) ; } execSync ( `rm /tmp/${ filename . split ( /\ / ) . join ( '\ ' ) }` ) ; cleanupTempFiles ( ) ; return getConvertedFilePath ( logs . toString ( 'utf8' ) ) ; } function enableExtension ( enabledExtensions , extension , shouldThrowOnExtensionFail ) { if ( ! enabledExtensions . includes ( basename ( extension ) ) ) { try { execSync ( `${ UNOPKG_OUTPUT_PATH } add --shared ${ extension }` ) ; } catch ( e ) { if ( shouldThrowOnExtensionFail ) { throw e ; } } } }