Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ulixee
GitHub Repository: ulixee/secret-agent
Path: blob/main/replay/install/Utils.ts
2650 views
1
import * as Path from 'path';
2
import * as Fs from 'fs';
3
import * as os from 'os';
4
import * as compareVersions from 'compare-versions';
5
6
const packageJson = require('../package.json');
7
8
const { version } = packageJson;
9
10
export function getInstallDirectory() {
11
return Path.join(getCacheDirectory(), 'secret-agent', 'replay');
12
}
13
14
export { version };
15
16
export function isBinaryInstalled() {
17
try {
18
const installedVersion = Fs.readFileSync(`${getInstallDirectory()}/version`, 'utf-8').trim();
19
const isCurrentVersionValid = compareVersions.compare(installedVersion, version, '>=');
20
if (!isCurrentVersionValid) {
21
return false;
22
}
23
} catch (ignored) {
24
return false;
25
}
26
27
return Fs.existsSync(getBinaryPath());
28
}
29
30
export function recordVersion() {
31
Fs.writeFileSync(`${getInstallDirectory()}/version`, version);
32
}
33
34
export function isLocalBuildPresent() {
35
return Fs.existsSync(getLocalBuildPath());
36
}
37
38
export function getLocalBuildPath() {
39
const platformPath = getPlatformExecutable();
40
41
const distDir = Path.join(__dirname, '..', 'dist', getDistDir());
42
43
return Path.join(distDir, platformPath);
44
}
45
46
function getDistDir() {
47
const platform = process.env.npm_config_platform || os.platform();
48
49
switch (platform) {
50
case 'mas':
51
case 'darwin':
52
if (os.arch() === 'arm64') return 'mac-arm64';
53
return 'mac';
54
default:
55
return '';
56
}
57
}
58
59
export function getBinaryPath() {
60
const platformPath = getPlatformExecutable();
61
return Path.join(getInstallDirectory(), platformPath);
62
}
63
64
function getPlatformExecutable() {
65
const platform = process.env.npm_config_platform || os.platform();
66
67
switch (platform) {
68
case 'mas':
69
case 'darwin':
70
return 'SecretAgentReplay.app/Contents/MacOS/SecretAgentReplay';
71
case 'freebsd':
72
case 'openbsd':
73
case 'linux':
74
return `replay-${version}-linux/secretagentreplay`;
75
case 'win32':
76
return `replay-${version}-win/SecretAgentReplay.exe`;
77
default:
78
throw new Error(`SecretAgent Replay builds are not available on platform: ${platform}`);
79
}
80
}
81
82
function getCacheDirectory(): string {
83
if (process.platform === 'linux') {
84
return process.env.XDG_CACHE_HOME || Path.join(os.homedir(), '.cache');
85
}
86
87
if (process.platform === 'darwin') {
88
return Path.join(os.homedir(), 'Library', 'Caches');
89
}
90
91
if (process.platform === 'win32') {
92
return process.env.LOCALAPPDATA || Path.join(os.homedir(), 'AppData', 'Local');
93
}
94
95
throw new Error(`Unsupported platform: ${process.platform}`);
96
}
97
98