Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ulixee
GitHub Repository: ulixee/secret-agent
Path: blob/main/replay/install/index.ts
2676 views
1
#!/usr/bin/env node
2
3
import * as Fs from 'fs';
4
import * as Tar from 'tar';
5
import * as ProgressBar from 'progress';
6
import { createGunzip } from 'zlib';
7
import * as os from 'os';
8
import * as Path from 'path';
9
import { IncomingMessage } from 'http';
10
import { httpGet } from '@secret-agent/commons/downloadFile';
11
import { getInstallDirectory, isBinaryInstalled, recordVersion, version } from './Utils';
12
13
if (Boolean(JSON.parse(process.env.SA_REPLAY_SKIP_BINARY_DOWNLOAD ?? 'false')) === true) {
14
process.exit(0);
15
}
16
17
if (isBinaryInstalled()) {
18
process.exit(0);
19
}
20
21
(async function downloadAndExtract() {
22
const platform = process.env.npm_config_platform || process.platform;
23
24
const platformNames = {
25
darwin: 'mac',
26
linux: 'linux',
27
win32: 'win',
28
};
29
30
let archAddon = '';
31
if (platform === 'darwin' && os.arch() === 'arm64') archAddon = '-arm64';
32
33
const response = await download(
34
`https://github.com/ulixee/secret-agent/releases/download/v${version}/replay-${version}-${platformNames[platform]}${archAddon}.tar.gz`,
35
);
36
const length = parseInt(response.headers['content-length'], 10);
37
38
const tmpFile = Path.join(os.tmpdir(), 'SecretAgentReplay.tar.gz');
39
const output = Fs.createWriteStream(tmpFile, { autoClose: true });
40
41
const bar = new ProgressBar(' Downloading SecretAgent Replay [:bar] :percent :etas', {
42
complete: '=',
43
incomplete: ' ',
44
width: 20,
45
total: length,
46
});
47
for await (const chunk of response) {
48
bar.tick(chunk.length);
49
output.write(chunk);
50
}
51
52
const installDir = getInstallDirectory();
53
if (!Fs.existsSync(installDir)) Fs.mkdirSync(installDir, { recursive: true });
54
55
await new Promise(resolve => {
56
Fs.createReadStream(tmpFile)
57
.pipe(createGunzip())
58
.pipe(
59
Tar.extract({
60
cwd: installDir,
61
}),
62
)
63
.on('finish', resolve);
64
});
65
66
recordVersion();
67
})().catch(err => {
68
console.error(err.stack);
69
process.exit(1);
70
});
71
72
function download(filepath: string): Promise<IncomingMessage> {
73
return new Promise<IncomingMessage>((resolve, reject) => {
74
const req = httpGet(filepath, res => {
75
if (res.statusCode >= 400) {
76
return reject(
77
new Error(
78
`ERROR downloading needed Secret Agent library - ${res.statusCode}:${res.statusMessage}`,
79
),
80
);
81
}
82
83
resolve(res);
84
});
85
req.on('error', err => {
86
console.log('ERROR downloading needed Secret Agent library %s', filepath, err);
87
reject(err);
88
});
89
});
90
}
91
92