import { AdobeAppEvent , AdobeAppProcess , CommandFileCreator as AdobeScriptCreator , CommandStack , Config , NewDocumentOptions , Options , AdobeApp , AdobeEventListener } from "./api" ; import newAdobeAppListener from './listener' ; import newAdobeAppProcess from './process' ; import newCommandStack from './commands' ; import newAdobeScriptCreator from './script-file-creator' ; import { broadcast } from './broadcast' ; export const newAdobeApp = ( config , timeoutCallback ? ) : AdobeApp => { const scriptCreator = newAdobeScriptCreator ( config ) ; const commandStack = newCommandStack ( ) ; const eventListener = newAdobeAppListener ( config . host , config . port , eventListenerCallback ) ; const appProcess = newAdobeAppProcess ( config . app . path , appCloseCallback , { timeout : config . appTimeout , timeoutCallback } ) ; function eventListenerCallback ( commandName ) { commandStack . resolve ( commandName ) ; } function appCloseCallback ( ) { broadcast ( config . host , config . port , { command : AdobeAppEvent . CloseApp } ) ; } function useBuiltInScript ( command ) { return command === AdobeAppEvent . CloseDocument || command === AdobeAppEvent . SaveDocument || command === AdobeAppEvent . SaveAndCloseDocument || command === AdobeAppEvent . SaveAsDocument || command === AdobeAppEvent . OpenDocument || command === AdobeAppEvent . NewDocument || command === AdobeAppEvent . SelectDocument ; } const app = { init ( ) { eventListener . start ( ) ; } , on ( event , callback ) { eventListener . addEventListener ( event , callback ) ; return app ; } , runScript : ( command , options ? ) : Promise < any > => new Promise ( async ( resolve , reject ) => { const commandPath = await scriptCreator . create ( command , useBuiltInScript ( command ) , options ) ; commandStack . push ( { command , resolve , reject } ) ; appProcess . run ( commandPath ) ; } ) , saveDocument : ( ... documents ) : Promise < any > => app . runScript ( AdobeAppEvent . SaveDocument , { documents } ) , selectDocument : ( document ) : Promise < any > => app . runScript ( AdobeAppEvent . SelectDocument , { document } ) , saveAsDocument : ( document , saveAs , options ? ) : Promise < any > => app . runScript ( AdobeAppEvent . SaveAsDocument , { document , saveAs , options } ) , openDocument : ( ... documents ) : Promise < any > => app . runScript ( AdobeAppEvent . OpenDocument , { documents } ) , closeDocument : ( ... documents ) : Promise < any > => app . runScript ( AdobeAppEvent . CloseDocument , { documents } ) , saveAndCloseDocument : ( ... documents ) : Promise < any > => app . runScript ( AdobeAppEvent . SaveAndCloseDocument , { documents } ) , newDocument : ( options ? ) : Promise < any > => app . runScript ( AdobeAppEvent . NewDocument , options ) , open : ( ) : Promise < any > => new Promise ( async ( resolve , reject ) => { const scriptPath = await scriptCreator . create ( AdobeAppEvent . OpenApp ) ; appProcess . create ( scriptPath ) ; commandStack . push ( { command : AdobeAppEvent . OpenApp , resolve , reject } ) ; } ) , close : ( ) : Promise < void > => new Promise ( async ( resolve , reject ) => { appProcess . kill ( ) ; commandStack . push ( { command : AdobeAppEvent . CloseApp , resolve , reject } ) ; } ) , dispose : ( ) : void => { eventListener . close ( ) ; } } ; return app ; } ;