Skip to content

Instantly share code, notes, and snippets.

@mbruno-kr
Last active October 7, 2021 22:13
Show Gist options
  • Save mbruno-kr/fe3aee12621ca0d0658ff011c0cdf13f to your computer and use it in GitHub Desktop.
Save mbruno-kr/fe3aee12621ca0d0658ff011c0cdf13f to your computer and use it in GitHub Desktop.
Pact download binary

Pact Downloader

Downloads the latest pact binary via wget & curl. Both wget and curl work well with corporate firewalls and proxy settings; assuming you have your system propery configured.

Installation

  1. Add this file to your repo under a scripts folder.
  2. Run yarn add -D shelljs pino (NOTE if you wish to not have pino, it can be stripped out)
  3. Run node ./scripts/pact-download.js
  4. Add the following portion to your package.json
"config": {
  "pact_binary_location": "./tmp/pact"
}
  1. Run a yarn

Conclusion

Now you can add pact via yarn and node have to worry about it erroring out. Example:

yarn add -D jest-pact @pact-foundation/pact
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');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment