|
| 1 | +const { exec } = require('child_process'); |
| 2 | +const { existsSync } = require('fs'); |
| 3 | + |
| 4 | +const K6_VERSION = "v0.55.0" |
| 5 | +const K6_PATH_MAC_AMD = `k6-${K6_VERSION}-macos-amd64` |
| 6 | +const K6_PATH_MAC_ARM = `k6-${K6_VERSION}-macos-arm64` |
| 7 | +const K6_PATH_WIN_AMD = "k6-${K6_VERSION}-windows-amd64" |
| 8 | + |
| 9 | +const existsK6 = (os, arch) => { |
| 10 | + return existsSync(`resources/${os}/${arch}/k6`) |
| 11 | +} |
| 12 | + |
| 13 | +const getMacOSK6Binary = () => { |
| 14 | + const command = ` |
| 15 | +# download binaries |
| 16 | +curl -LO https://github.com/grafana/k6/releases/download/${K6_VERSION}/${K6_PATH_MAC_AMD}.zip |
| 17 | +curl -LO https://github.com/grafana/k6/releases/download/${K6_VERSION}/${K6_PATH_MAC_ARM}.zip |
| 18 | +
|
| 19 | +# unzip & smoke test |
| 20 | +unzip ${K6_PATH_MAC_AMD}.zip |
| 21 | +unzip ${K6_PATH_MAC_ARM}.zip |
| 22 | +${K6_PATH_MAC_AMD}/k6 version |
| 23 | +${K6_PATH_MAC_ARM}/k6 version |
| 24 | +
|
| 25 | +# move to resource folder |
| 26 | +mv ${K6_PATH_MAC_AMD}/k6 resources/mac/x86_64 |
| 27 | +mv ${K6_PATH_MAC_ARM}/k6 resources/mac/arm64 |
| 28 | +
|
| 29 | +# cleanup |
| 30 | +rm ${K6_PATH_MAC_AMD}.zip |
| 31 | +rm ${K6_PATH_MAC_ARM}.zip |
| 32 | +rmdir ${K6_PATH_MAC_AMD} |
| 33 | +rmdir ${K6_PATH_MAC_ARM} |
| 34 | +` |
| 35 | + |
| 36 | + executeCommand(command) |
| 37 | + |
| 38 | +} |
| 39 | + |
| 40 | +const getWindowsK6Binary = () => { |
| 41 | + const command = ` |
| 42 | +# download binaries |
| 43 | +Invoke-WebRequest -Uri "https://github.com/grafana/k6/releases/download/${K6_VERSION}/${K6_PATH_WIN_AMD}.zip" -OutFile "k6-windows-amd64.zip" |
| 44 | +
|
| 45 | +# unzip & smoke test |
| 46 | +Expand-Archive -Path "k6-windows-amd64.zip" -DestinationPath "." |
| 47 | +& ${K6_PATH_WIN_AMD}\k6.exe version |
| 48 | +
|
| 49 | +# move to resource folder |
| 50 | +Move-Item -Path "${K6_PATH_WIN_AMD}\k6.exe" -Destination resources\win\x86_64 |
| 51 | +` |
| 52 | + |
| 53 | + executeCommand(command) |
| 54 | + |
| 55 | +} |
| 56 | + |
| 57 | + |
| 58 | +const executeCommand = (command) => { |
| 59 | + exec(command, (error, stdout, stderr) => { |
| 60 | + if (error) { |
| 61 | + console.error(`Error: ${error.message}`); |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + if (stderr) { |
| 66 | + console.error(`stderr: ${stderr}`); |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + console.log(`stdout: ${stdout}`); |
| 71 | + }); |
| 72 | +} |
| 73 | + |
| 74 | +switch (process.platform) { |
| 75 | + case 'darwin': |
| 76 | + // we check only for one arch since we include both binaries |
| 77 | + if (!existsK6('mac', 'arm64')) { |
| 78 | + console.log('k6 binary not found') |
| 79 | + console.log('downloading k6... this might take some time...') |
| 80 | + getMacOSK6Binary() |
| 81 | + console.log('k6 binary download completed') |
| 82 | + } |
| 83 | + break |
| 84 | + case 'win32': |
| 85 | + if (!existsK6('win', 'x86_64')) { |
| 86 | + console.log('k6 binary not found') |
| 87 | + console.log('downloading k6... this might take some time...') |
| 88 | + getWindowsK6Binary() |
| 89 | + console.log('k6 binary download completed') |
| 90 | + } |
| 91 | + break |
| 92 | + default: |
| 93 | + console.log(`unsupported platform found: ${process.platform}`) |
| 94 | +} |
| 95 | + |
0 commit comments