1import fs from 'node:fs';
2import path from 'node:path';
3import { libreVersion, libreList } from '../../config/binaries.js';
4import { tmpFile, downloadToFile, extractBinary } from './downloadHelper.js';
5
6const binaryName = `librespeed-cli${process.platform === 'win32' ? '.exe' : ''}`;
7const binaryRegex = /librespeed-cli(.exe)?$/;
8const binaryDirectory = path.join(process.cwd(), 'bin');
9const binaryPath = path.join(binaryDirectory, binaryName);
10
11const downloadPath = `https://github.com/librespeed/speedtest-cli/releases/download/v${libreVersion}/librespeed-cli_${libreVersion}_`;
12
13export const fileExists = async () => fs.existsSync(binaryPath);
14
15export const downloadFile = async () => {
16 const binary = libreList.find(b => b.os === process.platform && b.arch === process.arch);
17 if (!binary)
18 throw new Error(`Your platform (${process.platform}-${process.arch}) is not supported by the LibreSpeed CLI`);
19
20 const archivePath = tmpFile(binary.suffix);
21 await downloadToFile(downloadPath + binary.suffix, archivePath);
22 await extractBinary(archivePath, binaryDirectory, binaryRegex, binaryName);
23};
24
25export const load = async () => {
26 if (!await fileExists()) await downloadFile();
27};
28