import * as path from "path" ; import * as fs from "fs" ; import { AdobeAppName , AdobeAppScriptFileType , CommandFileCreator , AdobeScriptBuilder , BroadcastBuilder , Config , Options } from "./api" ; import newAdobeScriptBuilder from './script-builder' ; import { newBroadcastBuilder } from './broadcast' ; import defaults from './defaults' ; const scriptingExtension = new Map < AdobeAppName , AdobeAppScriptFileType > ( [ [ AdobeAppName . Animate , AdobeAppScriptFileType . Jsfl ] , [ AdobeAppName . Photoshop , AdobeAppScriptFileType . Jsx ] , [ AdobeAppName . Illustrator , AdobeAppScriptFileType . Jsx ] , [ AdobeAppName . InDesign , AdobeAppScriptFileType . Jsx ] ] ) ; const newAdobeScriptFileCreator = ( config ) : CommandFileCreator => { const appName = config . app . name ; const jsPath = config . jsPath || defaults . scriptsPath ; const adobeScriptsPath = config . app . adobeScriptsPath || defaults . adobeScriptsPath ; const scriptBuilder = newAdobeScriptBuilder ( ) ; const broadcastBuilder = newBroadcastBuilder ( config ) ; const buildVars = ( args ) : string => { let list = [ ] ; let arg ; for ( let name in args ) { arg = args [ name ] ; if ( arg ) { list . push ( `var ${ name }=${ arg . constructor . name === "String" ? `"${ arg }"` : arg . constructor . name === "Array" ? `${ JSON . stringify ( arg ) }` : arg };` ) ; } else { list . push ( `var ${ name };` ) ; } } return `${ list . length ? list . join ( '\n' ) : '' }` ; } ; const buildBody = ( command , useBuiltInScript ) : string => { const scriptPath = path . join ( jsPath , appName , `${ command }.js` ) ; const builtInScript = path . join ( __dirname , '..' , 'scripts' , appName , `${ command }.js` ) ; if ( useBuiltInScript && fs . existsSync ( builtInScript ) ) { console . info ( `Built-in Script file found: ${ builtInScript }` ) ; return fs . readFileSync ( builtInScript ) . toString ( ) ; } if ( fs . existsSync ( scriptPath ) ) { console . info ( `Custom script file found: ${ scriptPath }` ) ; return fs . readFileSync ( scriptPath ) . toString ( ) ; } return `"";` ; } ; const createFile = ( command , content ) : Promise < string > => new Promise ( ( resolve , reject ) => { const filePath = path . join ( adobeScriptsPath , `${ command }.${ scriptingExtension . get ( appName ) }` ) ; const fileDirname = path . dirname ( filePath ) ; if ( fs . existsSync ( fileDirname ) ) { fs . writeFile ( filePath , content , "utf-8" , ( err ) => { return err ? reject ( err ) : resolve ( filePath ) ; } ) ; } else { return reject ( `The path (${ fileDirname }) is not valid.` ) ; } } ) ; return { create : ( command , useBuiltInScript , args ? ) : Promise < string > => new Promise ( ( resolve , reject ) => { const variables = buildVars ( args ) ; const body = buildBody ( command , useBuiltInScript ) ; const broadcast = broadcastBuilder . build ( command ) ; let content = scriptBuilder . setName ( command ) . setVariables ( variables ) . setBody ( body ) . setBroadcast ( broadcast ) . build ( ) ; return createFile ( command , content ) . then ( resolve ) . catch ( reject ) ; } ) } ; } ; export default newAdobeScriptFileCreator ;