import { exec } from '@actions/exec' ; import { find , cacheDir , downloadTool , extractZip } from '@actions/tool-cache' ; import { which } from '@actions/io' ; import * as path from 'path' ; import { _filterVersion , _readFile } from './util' ; import { mv } from '@actions/io' ; const IS_WINDOWS = process . platform === 'win32' ? true : false ; export class DownloadExtractInstall { private readonly downloadUrl : string ; private readonly fileType : string ; public constructor ( downloadUrl ) { this . downloadUrl = downloadUrl ; this . fileType = this . downloadUrl . substr ( - 4 ) ; } public async isAlreadyInstalled ( toolName ) { const cachePath = await find ( toolName , '*' ) ; const systemPath = await which ( toolName ) ; if ( cachePath ) return cachePath ; if ( systemPath ) { return systemPath ; } return false ; } private async _getVersion ( installedBinary ) { const versionCommandOutput = await this . _getCommandOutput ( installedBinary , [ '--version' ] ) ; const installedVersion = _filterVersion ( versionCommandOutput ) ; return installedVersion ; } private async _getCommandOutput ( commandStr , args ) { const logFile = path . join ( __dirname , 'log.txt' ) ; let stdErr = '' ; let stdOut = '' ; const options = { windowsVerbatimArguments : false , listeners : { stderr : ( data ) => { stdErr += data . toString ( ) ; } , stdout : ( data ) => { stdOut += data . toString ( ) ; } } } ; if ( IS_WINDOWS ) { args . push ( '>' , logFile ) ; args . unshift ( commandStr ) ; args . unshift ( '/c' ) ; commandStr = 'cmd' ; await exec ( commandStr , args , options ) ; return await _readFile ( logFile , { } ) ; } else { await exec ( commandStr , args , options ) ; if ( stdOut === '' ) return stdErr ; return stdOut ; } } public async downloadFile ( ) { const filePath = await downloadTool ( this . downloadUrl ) ; const destPath = `${ filePath }${ this . fileType }` ; await mv ( filePath , destPath ) ; return destPath ; } public async extractFile ( filePath ) { const extractDir = path . dirname ( filePath ) ; if ( process . platform === 'linux' ) { await exec ( `unzip ${ filePath }` , [ '-d' , extractDir ] ) ; return extractDir ; } return await extractZip ( filePath , extractDir ) ; } public async installPackage ( installCommand , installArgs ) { return await exec ( installCommand , installArgs ) ; } public async cacheTool ( installedBinary ) { const installedVersion = await this . _getVersion ( installedBinary ) ; const cachedPath = await cacheDir ( path . parse ( installedBinary ) . dir , 'aws' , installedVersion ) ; return cachedPath ; } }