Skip to content

Commit 8b3511e

Browse files
committed
chore: replace bash script with node script
Helps with cross-platform support, in case `bash` is not available or easily invoked (e.g., CI in Windows running in pwsh). Signed-off-by: JP-Ellis <josh@jpellis.me>
1 parent 0f88d90 commit 8b3511e

3 files changed

Lines changed: 106 additions & 108 deletions

File tree

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"clean": "rimraf dist coverage .nyc_output logs pacts",
1010
"predist": "npm run clean",
1111
"dist": "tsc && copyfiles package.json ./dist",
12-
"install-plugins": "./scripts/install-plugins",
12+
"install-plugins": "node ./scripts/install-plugins.js",
1313
"lint:biome": "biome lint",
1414
"lint:tsc": "tsc --noEmit",
1515
"lint": "npm run lint:biome && npm run lint:tsc",
@@ -20,7 +20,7 @@
2020
"check": "npm run format && npm run lint",
2121
"check:fix": "npm run format:fix && npm run lint:fix",
2222
"release": "commit-and-tag-version",
23-
"pretest": "npm run install-plugins",
23+
"pretest": "node ./scripts/install-plugins.js",
2424
"test": "vitest run",
2525
"test:regression": "npm --prefix regression run test",
2626
"docker:alpine:build": "docker build --build-arg NODE_VERSION=${NODE_VERSION:-current} -f Dockerfile.alpine -t pact-js:alpine .",

scripts/install-plugins

Lines changed: 0 additions & 106 deletions
This file was deleted.

scripts/install-plugins.js

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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

Comments
 (0)