|
| 1 | +import {configurePlatformBinaries, getPkg, allToolInfo} from './lib/tools.js'; |
| 2 | +import {fixPerms, allFilesExist, downloadFile, downloadProgress, |
| 3 | + checkMakeFolder, unzipAndDelete} from './lib/download.js'; |
| 4 | +import * as path from 'path'; |
| 5 | + |
| 6 | +const fatalError = (err) => { |
| 7 | + console.error(err); |
| 8 | + console.error(`Installing required binary tools failed.`); |
| 9 | + console.error(`\n** Manual Khronos tool installation required **\n\n${allToolInfo()}`); |
| 10 | + process.exit(0); // Allow npm install to proceed |
| 11 | +}; |
| 12 | + |
| 13 | +(async function main() { |
| 14 | + const binSource = configurePlatformBinaries(); |
| 15 | + |
| 16 | + if (!binSource || !binSource.tag) { |
| 17 | + fatalError('Prebuilt binaries not available for this platform'); |
| 18 | + } |
| 19 | + |
| 20 | + if (allFilesExist(binSource.fileList)) { |
| 21 | + console.info('All binaries already installed'); |
| 22 | + fixPerms(binSource.fileList); |
| 23 | + process.exit(0); |
| 24 | + } |
| 25 | + |
| 26 | + const downloadBaseURL = getPkg()?.installBinaries?.url; |
| 27 | + const downloadTag = getPkg()?.installBinaries?.tag; |
| 28 | + |
| 29 | + if (!downloadBaseURL || !downloadTag) { |
| 30 | + fatalError('No configured download URL'); |
| 31 | + } |
| 32 | + |
| 33 | + try { |
| 34 | + checkMakeFolder(binSource.folderPath); |
| 35 | + } catch (err) { |
| 36 | + fatalError('Failed to create output folder'); |
| 37 | + } |
| 38 | + |
| 39 | + const downloadArchiveName = `${binSource.tag}.zip`; |
| 40 | + const downloadURL = new URL(`${downloadTag}/${downloadArchiveName}`, downloadBaseURL).toString(); |
| 41 | + |
| 42 | + const downloadArchivePath = path.join(binSource.folderPath, downloadArchiveName); |
| 43 | + |
| 44 | + try { |
| 45 | + await downloadFile(downloadURL, downloadArchivePath, downloadProgress(`binaries for ${binSource.tag}`)); |
| 46 | + |
| 47 | + const unzippedFiles = unzipAndDelete(downloadArchivePath, binSource.folderPath); |
| 48 | + |
| 49 | + console.log(unzippedFiles.join('\n')); |
| 50 | + |
| 51 | + if (allFilesExist(binSource.fileList)) { |
| 52 | + fixPerms(binSource.fileList); |
| 53 | + } else { |
| 54 | + fatalError('Downloaded archive did not include the expected binaries'); |
| 55 | + } |
| 56 | + |
| 57 | + } catch (err) { |
| 58 | + console.log(err); |
| 59 | + fatalError(`Failed to download binaries for ${binSource.tag}\n${err.message}`); |
| 60 | + } |
| 61 | + |
| 62 | + console.log('Install completed'); |
| 63 | + process.exit(0); |
| 64 | +})(); |
0 commit comments