-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·179 lines (162 loc) · 4.48 KB
/
index.js
File metadata and controls
executable file
·179 lines (162 loc) · 4.48 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env node
const { spawn } = require('child_process');
const path = require('path');
const fs = require('fs');
const { BinWrapper } = require('@saucelabs/bin-wrapper');
const { Writable } = require('stream');
const version = '0.205.1';
const defaultBinInstallBase =
'https://github.com/saucelabs/saucectl/releases/download';
const binWrapper = (
binInstallURL = null,
binInstallBase = null,
binLocalPath = null,
) => {
const bw = new BinWrapper();
if (binLocalPath) {
const resolvedPath = path.resolve(binLocalPath);
if (!fs.existsSync(resolvedPath)) {
console.error(
`Please ensure the path provided by SAUCECTL_INSTALL_BINARY_LOCAL exists: ${resolvedPath}`,
);
return;
}
if (!fs.statSync(resolvedPath).isFile()) {
console.error(
`Please ensure the path provided by SAUCECTL_INSTALL_BINARY_LOCAL points to a file, not a directory: ${resolvedPath}`,
);
return;
}
if (binInstallURL || binInstallBase) {
console.warn(
'SAUCECTL_INSTALL_BINARY_LOCAL is set alongside other binary source environment variables. The local path takes precedence.',
);
}
const binName = process.platform.startsWith('win')
? 'saucectl.exe'
: 'saucectl';
const binDir = path.join(__dirname, 'bin');
fs.mkdirSync(binDir, { recursive: true });
fs.copyFileSync(resolvedPath, path.join(binDir, binName));
fs.chmodSync(path.join(binDir, binName), 0o755);
bw.dest(binDir);
bw.use(binName);
return bw;
}
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,
process.env.SAUCECTL_INSTALL_BINARY_LOCAL,
);
main(bw, process.argv.slice(2));
}
module.exports = main;
module.exports.binWrapper = binWrapper;