2024-01-25 16:51:06 +01:00
|
|
|
const child_process = require('child_process')
|
|
|
|
const { promisify } = require('util')
|
|
|
|
const exec = promisify(child_process.exec)
|
2024-08-13 08:25:39 +02:00
|
|
|
const os = require('os')
|
2024-07-27 20:51:31 +02:00
|
|
|
const axios = require('axios')
|
2024-08-13 08:25:39 +02:00
|
|
|
const path = require('path')
|
2023-12-05 20:19:17 +01:00
|
|
|
const which = require('../libs/which')
|
|
|
|
const fs = require('../libs/fsExtra')
|
|
|
|
const Logger = require('../Logger')
|
2023-12-14 08:47:18 +01:00
|
|
|
const fileUtils = require('../utils/fileUtils')
|
2024-07-27 20:51:31 +02:00
|
|
|
const StreamZip = require('../libs/nodeStreamZip')
|
2023-12-05 20:19:17 +01:00
|
|
|
|
2024-08-13 08:25:39 +02:00
|
|
|
class ZippedAssetDownloader {
|
|
|
|
constructor() {
|
2024-07-27 20:51:31 +02:00
|
|
|
this.assetCache = {}
|
|
|
|
}
|
|
|
|
|
2024-08-13 08:25:39 +02:00
|
|
|
getReleaseUrl(releaseTag) {
|
|
|
|
throw new Error('Not implemented')
|
|
|
|
}
|
|
|
|
|
|
|
|
extractAssetUrl(assets, assetName) {
|
|
|
|
throw new Error('Not implemented')
|
|
|
|
}
|
|
|
|
|
|
|
|
getAssetName(binaryName, releaseTag) {
|
|
|
|
throw new Error('Not implemented')
|
|
|
|
}
|
|
|
|
|
|
|
|
getAssetFileName(binaryName) {
|
|
|
|
throw new Error('Not implemented')
|
|
|
|
}
|
|
|
|
|
2024-07-27 20:51:31 +02:00
|
|
|
async getAssetUrl(releaseTag, assetName) {
|
|
|
|
// Check if the assets information is already cached for the release tag
|
|
|
|
if (this.assetCache[releaseTag]) {
|
2024-08-13 08:25:39 +02:00
|
|
|
Logger.debug(`[ZippedAssetDownloader] release ${releaseTag}: assets found in cache.`)
|
2024-07-27 20:51:31 +02:00
|
|
|
} else {
|
|
|
|
// Get the release information
|
2024-08-13 08:25:39 +02:00
|
|
|
const releaseUrl = this.getReleaseUrl(releaseTag)
|
|
|
|
const releaseResponse = await axios.get(releaseUrl, { headers: { 'User-Agent': 'axios' } })
|
2024-07-27 20:51:31 +02:00
|
|
|
|
|
|
|
// Cache the assets information for the release tag
|
2024-08-13 08:25:39 +02:00
|
|
|
this.assetCache[releaseTag] = releaseResponse.data
|
|
|
|
Logger.debug(`[ZippedAssetDownloader] release ${releaseTag}: assets fetched from API.`)
|
2024-07-27 20:51:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const assets = this.assetCache[releaseTag]
|
2024-08-13 08:25:39 +02:00
|
|
|
const assetUrl = this.extractAssetUrl(assets, assetName)
|
2024-07-27 20:51:31 +02:00
|
|
|
|
2024-08-13 08:25:39 +02:00
|
|
|
return assetUrl
|
2024-07-27 20:51:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async downloadAsset(assetUrl, destDir) {
|
|
|
|
const zipPath = path.join(destDir, 'temp.zip')
|
|
|
|
const writer = fs.createWriteStream(zipPath)
|
|
|
|
|
2024-08-13 08:25:39 +02:00
|
|
|
const assetResponse = await axios({ url: assetUrl, responseType: 'stream' })
|
2024-07-27 20:51:31 +02:00
|
|
|
|
|
|
|
assetResponse.data.pipe(writer)
|
|
|
|
|
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
writer.on('finish', () => {
|
2024-08-13 08:25:39 +02:00
|
|
|
Logger.debug(`[ZippedAssetDownloader] Downloaded asset ${assetUrl} to ${zipPath}`)
|
2024-07-27 20:51:31 +02:00
|
|
|
resolve()
|
|
|
|
})
|
|
|
|
writer.on('error', (err) => {
|
2024-08-13 08:25:39 +02:00
|
|
|
Logger.error(`[ZippedAssetDownloader] Error downloading asset ${assetUrl}: ${err.message}`)
|
2024-07-27 20:51:31 +02:00
|
|
|
reject(err)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
return zipPath
|
|
|
|
}
|
|
|
|
|
|
|
|
async extractFiles(zipPath, filesToExtract, destDir) {
|
|
|
|
const zip = new StreamZip.async({ file: zipPath })
|
|
|
|
|
2024-09-29 08:22:39 +02:00
|
|
|
try {
|
|
|
|
for (const file of filesToExtract) {
|
|
|
|
const outputPath = path.join(destDir, file.outputFileName)
|
|
|
|
if (!(await zip.entry(file.pathInsideZip))) {
|
|
|
|
Logger.error(`[ZippedAssetDownloader] File ${file.pathInsideZip} not found in zip file ${zipPath}`)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
await zip.extract(file.pathInsideZip, outputPath)
|
|
|
|
Logger.debug(`[ZippedAssetDownloader] Extracted file ${file.pathInsideZip} to ${outputPath}`)
|
|
|
|
|
|
|
|
// Set executable permission for Linux
|
|
|
|
if (process.platform !== 'win32') {
|
|
|
|
await fs.chmod(outputPath, 0o755)
|
|
|
|
}
|
2024-07-27 22:03:40 +02:00
|
|
|
}
|
2024-09-29 08:22:39 +02:00
|
|
|
} catch (error) {
|
|
|
|
Logger.error('[ZippedAssetDownloader] Error extracting files:', error)
|
|
|
|
throw error
|
|
|
|
} finally {
|
|
|
|
await zip.close()
|
2024-07-27 20:51:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async downloadAndExtractFiles(releaseTag, assetName, filesToExtract, destDir) {
|
|
|
|
let zipPath
|
|
|
|
try {
|
|
|
|
await fs.ensureDir(destDir)
|
|
|
|
const assetUrl = await this.getAssetUrl(releaseTag, assetName)
|
|
|
|
zipPath = await this.downloadAsset(assetUrl, destDir)
|
|
|
|
await this.extractFiles(zipPath, filesToExtract, destDir)
|
|
|
|
} catch (error) {
|
2024-08-13 08:25:39 +02:00
|
|
|
Logger.error(`[ZippedAssetDownloader] Error downloading or extracting files: ${error.message}`)
|
2024-07-27 20:51:31 +02:00
|
|
|
} finally {
|
|
|
|
if (zipPath) await fs.remove(zipPath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async downloadBinary(binaryName, releaseTag, destDir) {
|
2024-08-13 08:25:39 +02:00
|
|
|
const assetName = this.getAssetName(binaryName, releaseTag)
|
|
|
|
const fileName = this.getAssetFileName(binaryName)
|
2024-07-27 20:51:31 +02:00
|
|
|
const filesToExtract = [{ pathInsideZip: fileName, outputFileName: fileName }]
|
|
|
|
|
|
|
|
await this.downloadAndExtractFiles(releaseTag, assetName, filesToExtract, destDir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-13 08:25:39 +02:00
|
|
|
class FFBinariesDownloader extends ZippedAssetDownloader {
|
2024-07-27 20:51:31 +02:00
|
|
|
constructor() {
|
2024-08-13 08:25:39 +02:00
|
|
|
super()
|
|
|
|
this.platformSuffix = this.getPlatformSuffix()
|
2024-07-27 20:51:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
getPlatformSuffix() {
|
2024-08-13 08:25:39 +02:00
|
|
|
var type = os.type().toLowerCase()
|
|
|
|
var arch = os.arch().toLowerCase()
|
|
|
|
|
|
|
|
if (type === 'darwin') {
|
|
|
|
return 'osx-64'
|
2024-07-27 20:51:31 +02:00
|
|
|
}
|
2024-08-13 08:25:39 +02:00
|
|
|
|
|
|
|
if (type === 'windows_nt') {
|
|
|
|
return arch === 'x64' ? 'windows-64' : 'windows-32'
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type === 'linux') {
|
|
|
|
if (arch === 'arm') return 'linux-armel'
|
|
|
|
if (arch === 'arm64') return 'linux-arm64'
|
|
|
|
return arch === 'x64' ? 'linux-64' : 'linux-32'
|
|
|
|
}
|
|
|
|
|
|
|
|
return null
|
2024-07-27 20:51:31 +02:00
|
|
|
}
|
|
|
|
|
2024-08-13 08:25:39 +02:00
|
|
|
getReleaseUrl(releaseTag) {
|
|
|
|
return `https://ffbinaries.com/api/v1/version/${releaseTag}`
|
|
|
|
}
|
2024-07-27 20:51:31 +02:00
|
|
|
|
2024-08-13 08:25:39 +02:00
|
|
|
extractAssetUrl(assets, assetName) {
|
|
|
|
const assetUrl = assets?.bin?.[this.platformSuffix]?.[assetName]
|
|
|
|
|
|
|
|
if (!assetUrl) {
|
|
|
|
throw new Error(`[FFBinariesDownloader] Asset ${assetName} not found for platform ${this.platformSuffix}`)
|
2024-07-27 20:51:31 +02:00
|
|
|
}
|
2024-08-13 08:25:39 +02:00
|
|
|
|
|
|
|
return assetUrl
|
2024-07-27 20:51:31 +02:00
|
|
|
}
|
|
|
|
|
2024-08-13 08:25:39 +02:00
|
|
|
getAssetName(binaryName, releaseTag) {
|
|
|
|
return binaryName
|
|
|
|
}
|
2024-07-27 20:51:31 +02:00
|
|
|
|
2024-08-13 08:25:39 +02:00
|
|
|
getAssetFileName(binaryName) {
|
|
|
|
return process.platform === 'win32' ? `${binaryName}.exe` : binaryName
|
2024-07-27 20:51:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-29 08:22:39 +02:00
|
|
|
class NunicodeDownloader extends ZippedAssetDownloader {
|
|
|
|
constructor() {
|
|
|
|
super()
|
|
|
|
this.platformSuffix = this.getPlatformSuffix()
|
|
|
|
}
|
|
|
|
|
|
|
|
getPlatformSuffix() {
|
|
|
|
const platform = process.platform
|
|
|
|
const arch = process.arch
|
|
|
|
|
|
|
|
if (platform === 'win32' && arch === 'x64') {
|
|
|
|
return 'win-x64'
|
|
|
|
} else if (platform === 'darwin' && (arch === 'x64' || arch === 'arm64')) {
|
|
|
|
return 'osx-arm64'
|
|
|
|
} else if (platform === 'linux' && arch === 'x64') {
|
|
|
|
return 'linux-x64'
|
|
|
|
} else if (platform === 'linux' && arch === 'arm64') {
|
|
|
|
return 'linux-arm64'
|
|
|
|
}
|
|
|
|
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
async getAssetUrl(releaseTag, assetName) {
|
|
|
|
return `https://github.com/mikiher/nunicode-sqlite/releases/download/v${releaseTag}/${assetName}`
|
|
|
|
}
|
|
|
|
|
|
|
|
getAssetName(binaryName, releaseTag) {
|
|
|
|
if (!this.platformSuffix) {
|
|
|
|
throw new Error(`[NunicodeDownloader] Platform ${process.platform}-${process.arch} not supported`)
|
|
|
|
}
|
|
|
|
return `${binaryName}-${this.platformSuffix}.zip`
|
|
|
|
}
|
|
|
|
|
|
|
|
getAssetFileName(binaryName) {
|
|
|
|
if (process.platform === 'win32') {
|
|
|
|
return `${binaryName}.dll`
|
|
|
|
} else if (process.platform === 'darwin') {
|
|
|
|
return `${binaryName}.dylib`
|
|
|
|
} else if (process.platform === 'linux') {
|
|
|
|
return `${binaryName}.so`
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error(`[NunicodeDownloader] Platform ${process.platform} not supported`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-27 20:51:31 +02:00
|
|
|
class Binary {
|
2024-09-29 08:22:39 +02:00
|
|
|
constructor(name, type, envVariable, validVersions, source, required = true) {
|
|
|
|
if (!name) throw new Error('Binary name is required')
|
2024-07-27 20:51:31 +02:00
|
|
|
this.name = name
|
2024-09-29 08:22:39 +02:00
|
|
|
if (!type) throw new Error('Binary type is required')
|
2024-07-27 20:51:31 +02:00
|
|
|
this.type = type
|
2024-09-29 08:22:39 +02:00
|
|
|
if (!envVariable) throw new Error('Binary environment variable name is required')
|
2024-07-27 20:51:31 +02:00
|
|
|
this.envVariable = envVariable
|
2024-09-29 08:22:39 +02:00
|
|
|
if (!validVersions || !validVersions.length) throw new Error(`No valid versions specified for ${type} ${name}. At least one version is required.`)
|
2024-07-27 20:51:31 +02:00
|
|
|
this.validVersions = validVersions
|
2024-09-29 08:22:39 +02:00
|
|
|
if (!source || !(source instanceof ZippedAssetDownloader)) throw new Error('Binary source is required, and must be an instance of ZippedAssetDownloader')
|
2024-07-27 20:51:31 +02:00
|
|
|
this.source = source
|
|
|
|
this.fileName = this.getFileName()
|
2024-09-29 08:22:39 +02:00
|
|
|
this.required = required
|
2024-07-27 20:51:31 +02:00
|
|
|
this.exec = exec
|
|
|
|
}
|
|
|
|
|
|
|
|
async find(mainInstallDir, altInstallDir) {
|
|
|
|
// 1. check path specified in environment variable
|
|
|
|
const defaultPath = process.env[this.envVariable]
|
|
|
|
if (await this.isGood(defaultPath)) return defaultPath
|
|
|
|
// 2. find the first instance of the binary in the PATH environment variable
|
|
|
|
if (this.type === 'executable') {
|
|
|
|
const whichPath = which.sync(this.fileName, { nothrow: true })
|
|
|
|
if (await this.isGood(whichPath)) return whichPath
|
|
|
|
}
|
|
|
|
// 3. check main install path (binary root dir)
|
|
|
|
const mainInstallPath = path.join(mainInstallDir, this.fileName)
|
|
|
|
if (await this.isGood(mainInstallPath)) return mainInstallPath
|
|
|
|
// 4. check alt install path (/config)
|
|
|
|
const altInstallPath = path.join(altInstallDir, this.fileName)
|
|
|
|
if (await this.isGood(altInstallPath)) return altInstallPath
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
getFileName() {
|
2024-08-04 23:51:07 +02:00
|
|
|
const platform = process.platform
|
|
|
|
|
2024-07-27 20:51:31 +02:00
|
|
|
if (this.type === 'executable') {
|
2024-08-04 23:51:07 +02:00
|
|
|
return this.name + (platform == 'win32' ? '.exe' : '')
|
2024-07-27 20:51:31 +02:00
|
|
|
} else if (this.type === 'library') {
|
2024-08-04 23:51:07 +02:00
|
|
|
return this.name + (platform == 'win32' ? '.dll' : platform == 'darwin' ? '.dylib' : '.so')
|
2024-07-27 20:51:31 +02:00
|
|
|
} else {
|
|
|
|
return this.name
|
|
|
|
}
|
|
|
|
}
|
2023-12-05 20:19:17 +01:00
|
|
|
|
2024-09-29 08:22:39 +02:00
|
|
|
async isLibraryVersionValid(libraryPath) {
|
2024-07-27 20:51:31 +02:00
|
|
|
try {
|
2024-09-29 08:22:39 +02:00
|
|
|
const versionFilePath = libraryPath + '.ver'
|
|
|
|
if (!(await fs.pathExists(versionFilePath))) return false
|
|
|
|
const version = (await fs.readFile(versionFilePath, 'utf8')).trim()
|
|
|
|
return this.validVersions.some((validVersion) => version.startsWith(validVersion))
|
|
|
|
} catch (err) {
|
|
|
|
Logger.error(`[Binary] Failed to check version of ${libraryPath}`, err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async isExecutableVersionValid(executablePath) {
|
|
|
|
try {
|
|
|
|
const { stdout } = await this.exec('"' + executablePath + '"' + ' -version')
|
2024-07-27 20:51:31 +02:00
|
|
|
const version = stdout.match(/version\s([\d\.]+)/)?.[1]
|
|
|
|
if (!version) return false
|
|
|
|
return this.validVersions.some((validVersion) => version.startsWith(validVersion))
|
|
|
|
} catch (err) {
|
2024-09-29 08:22:39 +02:00
|
|
|
Logger.error(`[Binary] Failed to check version of ${executablePath}`, err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async isGood(binaryPath) {
|
|
|
|
try {
|
|
|
|
if (!binaryPath || !(await fs.pathExists(binaryPath))) return false
|
|
|
|
if (this.type === 'library') return await this.isLibraryVersionValid(binaryPath)
|
|
|
|
else if (this.type === 'executable') return await this.isExecutableVersionValid(binaryPath)
|
|
|
|
else return true
|
|
|
|
} catch (err) {
|
|
|
|
Logger.error(`[Binary] Failed to check ${this.type} ${this.name} at ${binaryPath}`, err)
|
2024-07-27 20:51:31 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async download(destination) {
|
2024-09-29 08:22:39 +02:00
|
|
|
const version = this.validVersions[0]
|
|
|
|
try {
|
|
|
|
await this.source.downloadBinary(this.name, version, destination)
|
|
|
|
// if it's a library, write the version string to a file
|
|
|
|
if (this.type === 'library') {
|
|
|
|
const libraryPath = path.join(destination, this.fileName)
|
|
|
|
await fs.writeFile(libraryPath + '.ver', version)
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
Logger.error(`[Binary] Failed to download ${this.type} ${this.name} version ${version} to ${destination}`, err)
|
|
|
|
}
|
2024-07-27 20:51:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const ffbinaries = new FFBinariesDownloader()
|
2024-09-29 08:22:39 +02:00
|
|
|
const nunicode = new NunicodeDownloader()
|
2024-07-27 20:51:31 +02:00
|
|
|
|
|
|
|
class BinaryManager {
|
2023-12-06 00:35:15 +01:00
|
|
|
defaultRequiredBinaries = [
|
2024-07-27 20:51:31 +02:00
|
|
|
new Binary('ffmpeg', 'executable', 'FFMPEG_PATH', ['5.1'], ffbinaries), // ffmpeg executable
|
2024-09-29 08:22:39 +02:00
|
|
|
new Binary('ffprobe', 'executable', 'FFPROBE_PATH', ['5.1'], ffbinaries), // ffprobe executable
|
2024-10-07 19:48:52 +02:00
|
|
|
new Binary('libnusqlite3', 'library', 'NUSQLITE3_PATH', ['1.2'], nunicode, false) // nunicode sqlite3 extension
|
2023-12-05 20:19:17 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
constructor(requiredBinaries = this.defaultRequiredBinaries) {
|
|
|
|
this.requiredBinaries = requiredBinaries
|
2024-07-27 20:51:31 +02:00
|
|
|
this.mainInstallDir = process.pkg ? path.dirname(process.execPath) : global.appRoot
|
|
|
|
this.altInstallDir = global.ConfigPath
|
2024-02-18 00:40:33 +01:00
|
|
|
this.initialized = false
|
2023-12-05 20:19:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async init() {
|
2024-03-14 22:29:01 +01:00
|
|
|
// Optional skip binaries check
|
|
|
|
if (process.env.SKIP_BINARIES_CHECK === '1') {
|
2024-08-01 21:25:57 +02:00
|
|
|
for (const binary of this.requiredBinaries) {
|
2024-09-29 08:22:39 +02:00
|
|
|
if (!process.env[binary.envVariable] && binary.required) {
|
2024-08-01 21:25:57 +02:00
|
|
|
await Logger.fatal(`[BinaryManager] Environment variable ${binary.envVariable} must be set`)
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
}
|
2024-03-14 22:29:01 +01:00
|
|
|
Logger.info('[BinaryManager] Skipping check for binaries')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-12-05 20:19:17 +01:00
|
|
|
if (this.initialized) return
|
2024-03-14 22:29:01 +01:00
|
|
|
|
2023-12-05 20:19:17 +01:00
|
|
|
const missingBinaries = await this.findRequiredBinaries()
|
|
|
|
if (missingBinaries.length == 0) return
|
2024-01-25 16:51:06 +01:00
|
|
|
await this.removeOldBinaries(missingBinaries)
|
2023-12-05 20:19:17 +01:00
|
|
|
await this.install(missingBinaries)
|
|
|
|
const missingBinariesAfterInstall = await this.findRequiredBinaries()
|
2024-09-29 08:22:39 +02:00
|
|
|
const missingRequiredBinryNames = missingBinariesAfterInstall.filter((binary) => binary.required).map((binary) => binary.name)
|
|
|
|
if (missingRequiredBinryNames.length) {
|
|
|
|
Logger.error(`[BinaryManager] Failed to find or install required binaries: ${missingRequiredBinryNames.join(', ')}`)
|
2023-12-05 20:19:17 +01:00
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
this.initialized = true
|
|
|
|
}
|
|
|
|
|
2024-09-29 08:22:39 +02:00
|
|
|
/**
|
|
|
|
* Remove binary
|
|
|
|
*
|
|
|
|
* @param {string} destination
|
|
|
|
* @param {Binary} binary
|
|
|
|
*/
|
2024-07-27 20:51:31 +02:00
|
|
|
async removeBinary(destination, binary) {
|
2024-09-29 08:22:39 +02:00
|
|
|
try {
|
|
|
|
const binaryPath = path.join(destination, binary.fileName)
|
|
|
|
if (await fs.pathExists(binaryPath)) {
|
|
|
|
Logger.debug(`[BinaryManager] Removing binary: ${binaryPath}`)
|
|
|
|
await fs.remove(binaryPath)
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
Logger.error(`[BinaryManager] Error removing binary: ${binaryPath}`)
|
2024-07-27 20:51:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-29 08:22:39 +02:00
|
|
|
/**
|
|
|
|
* Remove old binaries
|
|
|
|
*
|
|
|
|
* @param {Binary[]} binaries
|
|
|
|
*/
|
2024-07-27 20:51:31 +02:00
|
|
|
async removeOldBinaries(binaries) {
|
|
|
|
for (const binary of binaries) {
|
|
|
|
await this.removeBinary(this.mainInstallDir, binary)
|
|
|
|
await this.removeBinary(this.altInstallDir, binary)
|
2024-01-25 16:51:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-18 00:40:33 +01:00
|
|
|
/**
|
|
|
|
* Find required binaries and return array of binary names that are missing
|
2024-07-27 20:51:31 +02:00
|
|
|
*
|
2024-09-29 08:22:39 +02:00
|
|
|
* @returns {Promise<Binary[]>} Array of missing binaries
|
2024-02-18 00:40:33 +01:00
|
|
|
*/
|
2023-12-05 20:19:17 +01:00
|
|
|
async findRequiredBinaries() {
|
|
|
|
const missingBinaries = []
|
|
|
|
for (const binary of this.requiredBinaries) {
|
2024-07-27 20:51:31 +02:00
|
|
|
const binaryPath = await binary.find(this.mainInstallDir, this.altInstallDir)
|
2023-12-05 20:19:17 +01:00
|
|
|
if (binaryPath) {
|
2024-09-29 08:22:39 +02:00
|
|
|
Logger.info(`[BinaryManager] Found valid ${binary.type} ${binary.name} at ${binaryPath}`)
|
2023-12-08 00:32:06 +01:00
|
|
|
if (process.env[binary.envVariable] !== binaryPath) {
|
|
|
|
Logger.info(`[BinaryManager] Updating process.env.${binary.envVariable}`)
|
|
|
|
process.env[binary.envVariable] = binaryPath
|
|
|
|
}
|
2023-12-05 20:19:17 +01:00
|
|
|
} else {
|
2024-09-29 08:22:39 +02:00
|
|
|
Logger.info(`[BinaryManager] ${binary.name} not found or not a valid version`)
|
2024-07-27 20:51:31 +02:00
|
|
|
missingBinaries.push(binary)
|
2023-12-05 20:19:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return missingBinaries
|
|
|
|
}
|
|
|
|
|
2024-09-29 08:22:39 +02:00
|
|
|
/**
|
|
|
|
* Install missing binaries
|
|
|
|
*
|
|
|
|
* @param {Binary[]} binaries
|
|
|
|
*/
|
2023-12-05 20:19:17 +01:00
|
|
|
async install(binaries) {
|
2024-02-18 00:40:33 +01:00
|
|
|
if (!binaries.length) return
|
2024-07-27 20:51:31 +02:00
|
|
|
Logger.info(`[BinaryManager] Installing binaries: ${binaries.map((binary) => binary.name).join(', ')}`)
|
|
|
|
let destination = (await fileUtils.isWritable(this.mainInstallDir)) ? this.mainInstallDir : this.altInstallDir
|
|
|
|
for (const binary of binaries) {
|
|
|
|
await binary.download(destination)
|
|
|
|
}
|
2023-12-05 20:19:17 +01:00
|
|
|
Logger.info(`[BinaryManager] Binaries installed to ${destination}`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-27 20:51:31 +02:00
|
|
|
module.exports = BinaryManager
|
|
|
|
module.exports.Binary = Binary // for testing
|
2024-09-29 08:22:39 +02:00
|
|
|
module.exports.ffbinaries = ffbinaries // for testing
|
|
|
|
module.exports.nunicode = nunicode // for testing
|