-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
executable file
·142 lines (126 loc) · 3.33 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env node
const { spawn } = require('child_process');
const path = require('path');
const { BinWrapper } = require('@saucelabs/bin-wrapper');
const { Writable } = require('stream');
const version = '0.190.1';
const defaultBinInstallBase =
'https://github.com/saucelabs/saucectl/releases/download';
const binWrapper = (binInstallURL = null, binInstallBase = null) => {
const bw = new BinWrapper();
if (binInstallURL) {
try {
new URL(binInstallURL);
} catch (e) {
console.error(
`Please ensure the provided saucectl binary source is valid: ${e}`,
);
return;
}
}
if (binInstallBase) {
try {
new URL(binInstallBase);
} catch (e) {
console.error(
`Please ensure the provided saucectl binary source mirror is valid: ${e}`,
);
return;
}
}
if (process.env.GITHUB_TOKEN) {
bw.httpOptions({
headers: {
authorization: `Bearer ${process.env.GITHUB_TOKEN}`,
},
});
}
const base = binInstallBase || defaultBinInstallBase;
let sources = [
{
url: `${base}/v${version}/saucectl_${version}_mac_64-bit.tar.gz`,
os: 'darwin',
arch: 'x64',
},
{
url: `${base}/v${version}/saucectl_${version}_mac_arm64.tar.gz`,
os: 'darwin',
arch: 'arm64',
},
{
url: `${base}/v${version}/saucectl_${version}_linux_32-bit.tar.gz`,
os: 'linux',
arch: 'x86',
},
{
url: `${base}/v${version}/saucectl_${version}_linux_64-bit.tar.gz`,
os: 'linux',
arch: 'x64',
},
{
url: `${base}/v${version}/saucectl_${version}_linux_arm64.tar.gz`,
os: 'linux',
arch: 'arm64',
},
{
url: `${base}/v${version}/saucectl_${version}_win_32-bit.zip`,
os: 'win32',
arch: 'x86',
},
{
url: `${base}/v${version}/saucectl_${version}_win_64-bit.zip`,
os: 'win32',
arch: 'x64',
},
];
sources = sources.filter(
(x) => process.platform === x.os && process.arch === x.arch,
);
if (binInstallURL) {
bw.src(binInstallURL, process.platform, process.arch);
} else {
if (sources.length === 0) {
return Promise.reject(
new Error(
`No binary found matching your system. It's probably not supported.`,
),
);
}
bw.src(sources[0].url, process.platform, process.arch);
}
bw.dest(path.join(__dirname, 'bin'));
bw.use(process.platform.startsWith('win') ? 'saucectl.exe' : 'saucectl');
return bw;
};
async function preRun(b) {
let buf = Buffer.from('');
const st = new Writable();
st._write = (d) => (buf = Buffer.concat([buf, d]));
const exitCode = await b.run(['--version'], {
stdout: st,
stderr: st,
});
if (exitCode !== 0) {
console.log('Post-installation checks failed. saucectl output was:');
console.log(buf.toString());
process.exit(exitCode);
}
}
async function main(b, args) {
await preRun(b);
const saucectlProcess = spawn(b.path(), args, {
stdio: [process.stdin, process.stdout, process.stderr],
});
saucectlProcess.on('exit', function (code) {
process.exit(code);
});
}
if (require.main === module) {
const bw = binWrapper(
process.env.SAUCECTL_INSTALL_BINARY,
process.env.SAUCECTL_INSTALL_BINARY_MIRROR,
);
main(bw, process.argv.slice(2));
}
module.exports = main;
module.exports.binWrapper = binWrapper;