Skip to content

Commit 4b79c00

Browse files
committed
preinstall script
1 parent 04ff607 commit 4b79c00

File tree

5 files changed

+96
-0
lines changed

5 files changed

+96
-0
lines changed

install-k6.js

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"repository": "github:grafana/k6-studio",
77
"main": ".vite/build/main.js",
88
"scripts": {
9+
"preinstall": "node install-k6.js",
910
"start": "electron-forge start",
1011
"package": "electron-forge package",
1112
"make": "electron-forge make",

resources/mac/arm64/k6

-58.4 MB
Binary file not shown.

resources/mac/x86_64/k6

-59.9 MB
Binary file not shown.

resources/win/x86_64/k6.exe

-60.2 MB
Binary file not shown.

0 commit comments

Comments
 (0)