|
const os = require('os'); |
|
const fs = require('fs'); |
|
const path = require('path'); |
|
const shell = require('shelljs'); |
|
const pino = require('pino'); |
|
|
|
const WORKING_DIR = path.join(__dirname, '..', 'tmp', 'pact'); |
|
const VERSION_CHECK_PATH = path.join(WORKING_DIR, '.pact-version'); |
|
const PACT_LATEST_RELEASES = |
|
'https://api.github.com/repos/pact-foundation/pact-ruby-standalone/releases/latest'; |
|
|
|
const logger = pino({ name: 'Pact Downloader', timestamp: false }); |
|
|
|
let releaseInfo; |
|
releaseInfo = shell.exec(`curl -s ${PACT_LATEST_RELEASES}`, { |
|
silent: true, |
|
}); |
|
releaseInfo = JSON.parse(releaseInfo.stdout); |
|
|
|
logger.info('Checking for pact download dir.'); |
|
shell.mkdir('-p', WORKING_DIR); |
|
|
|
let trigger_download = true; |
|
|
|
logger.info('Checking for pact version.'); |
|
if (fs.existsSync(VERSION_CHECK_PATH)) { |
|
trigger_download = |
|
fs.readFileSync(VERSION_CHECK_PATH).toString() !== releaseInfo.tag_name; |
|
} |
|
|
|
if (trigger_download) { |
|
logger.info('Fetching correct asset'); |
|
const asset = releaseInfo.assets.find((asset) => { |
|
const name = (asset?.name || '').toString(); |
|
const notCheckSum = !name.includes('checksum'); |
|
switch (os.platform()) { |
|
case 'darwin': |
|
return notCheckSum && name.includes('osx'); |
|
default: |
|
return notCheckSum && name.includes(os.platform()); |
|
} |
|
}); |
|
if (asset) { |
|
logger.info('Downloading related asset'); |
|
shell.exec(`wget -P ${WORKING_DIR} ${asset.browser_download_url}`); |
|
fs.writeFileSync(VERSION_CHECK_PATH, releaseInfo.tag_name); |
|
} else { |
|
logger.error('No asset found!'); |
|
} |
|
} else { |
|
logger.info('No need to download Pact again <3'); |
|
} |