|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const { execFileSync } = require('node:child_process'); |
| 4 | +const { |
| 5 | + existsSync, |
| 6 | + mkdirSync, |
| 7 | + createWriteStream, |
| 8 | + chmodSync, |
| 9 | +} = require('node:fs'); |
| 10 | +const { get } = require('node:https'); |
| 11 | +const { homedir } = require('node:os'); |
| 12 | +const { join } = require('node:path'); |
| 13 | +const { createGunzip } = require('node:zlib'); |
| 14 | + |
| 15 | +const PLUGIN_CLI_VERSION = '0.1.2'; |
| 16 | +const MATT_PLUGIN_VERSION = '0.1.1'; |
| 17 | + |
| 18 | +function detectOsArch() { |
| 19 | + const { platform, arch } = process; |
| 20 | + if (platform === 'linux' && arch === 'x64') |
| 21 | + return { os: 'linux', arch: 'x86_64', ext: '' }; |
| 22 | + if (platform === 'linux' && arch === 'arm64') |
| 23 | + return { os: 'linux', arch: 'aarch64', ext: '' }; |
| 24 | + if (platform === 'darwin' && arch === 'x64') |
| 25 | + return { os: 'osx', arch: 'x86_64', ext: '' }; |
| 26 | + if (platform === 'darwin' && arch === 'arm64') |
| 27 | + return { os: 'osx', arch: 'aarch64', ext: '' }; |
| 28 | + if (platform === 'win32') |
| 29 | + return { os: 'windows', arch: 'x86_64', ext: '.exe' }; |
| 30 | + throw new Error( |
| 31 | + `Unsupported platform: ${platform} ${arch}. Please install the plugin CLI manually.`, |
| 32 | + ); |
| 33 | +} |
| 34 | + |
| 35 | +function download(url, dest) { |
| 36 | + return new Promise((resolve, reject) => { |
| 37 | + get(url, (res) => { |
| 38 | + if (res.statusCode === 301 || res.statusCode === 302) { |
| 39 | + download(res.headers.location, dest).then(resolve, reject); |
| 40 | + return; |
| 41 | + } |
| 42 | + if (res.statusCode !== 200) { |
| 43 | + reject( |
| 44 | + new Error(`Download failed with HTTP ${res.statusCode}: ${url}`), |
| 45 | + ); |
| 46 | + return; |
| 47 | + } |
| 48 | + const out = createWriteStream(dest); |
| 49 | + res.pipe(createGunzip()).pipe(out); |
| 50 | + out.on('finish', () => out.close(resolve)); |
| 51 | + out.on('error', reject); |
| 52 | + }).on('error', reject); |
| 53 | + }); |
| 54 | +} |
| 55 | + |
| 56 | +async function installPluginCli() { |
| 57 | + const { os, arch, ext } = detectOsArch(); |
| 58 | + const binDir = join(homedir(), '.pact', 'bin'); |
| 59 | + const cliPath = join(binDir, `pact-plugin-cli${ext}`); |
| 60 | + |
| 61 | + if (existsSync(cliPath)) { |
| 62 | + console.log('=> Plugin CLI already installed'); |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + const url = `https://github.com/pact-foundation/pact-plugins/releases/download/pact-plugin-cli-v${PLUGIN_CLI_VERSION}/pact-plugin-cli-${os}-${arch}${ext}.gz`; |
| 67 | + console.log(`=> Installing plugin CLI v${PLUGIN_CLI_VERSION}`); |
| 68 | + console.log(` OS: ${os}, Arch: ${arch}`); |
| 69 | + console.log(` Downloading into: ${binDir}`); |
| 70 | + |
| 71 | + mkdirSync(binDir, { recursive: true }); |
| 72 | + await download(url, cliPath); |
| 73 | + if (ext !== '.exe') chmodSync(cliPath, 0o755); |
| 74 | +} |
| 75 | + |
| 76 | +function installMattPlugin() { |
| 77 | + const pluginDir = join( |
| 78 | + homedir(), |
| 79 | + '.pact', |
| 80 | + 'plugins', |
| 81 | + `matt-${MATT_PLUGIN_VERSION}`, |
| 82 | + ); |
| 83 | + |
| 84 | + if (existsSync(pluginDir)) { |
| 85 | + console.log('=> MATT plugin already installed'); |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + const { ext } = detectOsArch(); |
| 90 | + const cliPath = join(homedir(), '.pact', 'bin', `pact-plugin-cli${ext}`); |
| 91 | + const url = `https://github.com/mefellows/pact-matt-plugin/releases/tag/v${MATT_PLUGIN_VERSION}`; |
| 92 | + console.log(`=> Installing MATT plugin v${MATT_PLUGIN_VERSION}`); |
| 93 | + execFileSync(cliPath, ['install', url], { stdio: 'inherit' }); |
| 94 | +} |
| 95 | + |
| 96 | +async function main() { |
| 97 | + await installPluginCli(); |
| 98 | + installMattPlugin(); |
| 99 | +} |
| 100 | + |
| 101 | +main().catch((err) => { |
| 102 | + console.error(err.message); |
| 103 | + process.exit(1); |
| 104 | +}); |
0 commit comments