|
1 | 1 | import { endianness } from "node:os"; |
2 | | -import { fileURLToPath } from 'node:url'; |
3 | | -import { dirname, join, parse } from 'node:path'; |
| 2 | +import { fileURLToPath } from "node:url"; |
| 3 | +import { dirname, join, parse } from "node:path"; |
4 | 4 | import { platform, arch } from "node:process"; |
5 | 5 | import { mkdir } from "node:fs/promises"; |
6 | 6 | import { existsSync } from "node:fs"; |
7 | 7 |
|
8 | | -import decompress from 'decompress'; |
9 | | -import decompressUnzip from 'decompress-unzip'; |
10 | | -import decompressTar from 'decompress-tar'; |
11 | | -import xz from '@napi-rs/lzma/xz'; |
| 8 | +import decompress from "decompress"; |
| 9 | +import decompressUnzip from "decompress-unzip"; |
| 10 | +import decompressTar from "decompress-tar"; |
| 11 | +import xz from "@napi-rs/lzma/xz"; |
12 | 12 |
|
13 | 13 | const __dirname = dirname(fileURLToPath(import.meta.url)); |
14 | 14 |
|
15 | 15 | const TAG = "v0.3.2"; |
16 | 16 |
|
17 | 17 | async function getWeval() { |
18 | | - const knownPlatforms = { |
19 | | - "win32 x64 LE": "x86_64-windows", |
20 | | - "darwin arm64 LE": "aarch64-macos", |
21 | | - "darwin x64 LE": "x86_64-macos", |
22 | | - "linux x64 LE": "x86_64-linux", |
23 | | - "linux arm64 LE": "aarch64-linux", |
24 | | - }; |
| 18 | + const knownPlatforms = { |
| 19 | + "win32 x64 LE": "x86_64-windows", |
| 20 | + "darwin arm64 LE": "aarch64-macos", |
| 21 | + "darwin x64 LE": "x86_64-macos", |
| 22 | + "linux x64 LE": "x86_64-linux", |
| 23 | + "linux arm64 LE": "aarch64-linux", |
| 24 | + }; |
25 | 25 |
|
26 | | - function getPlatformName() { |
27 | | - let platformKey = `${platform} ${arch} ${endianness()}`; |
| 26 | + function getPlatformName() { |
| 27 | + let platformKey = `${platform} ${arch} ${endianness()}`; |
28 | 28 |
|
29 | | - if (platformKey in knownPlatforms) { |
30 | | - return knownPlatforms[platformKey]; |
31 | | - } |
32 | | - throw new Error(`Unsupported platform: "${platformKey}". "weval does not have a precompiled binary for the platform/architecture you are using. You can open an issue on https://github.com/bytecodealliance/weval/issues to request for your platform/architecture to be included."`); |
| 29 | + if (platformKey in knownPlatforms) { |
| 30 | + return knownPlatforms[platformKey]; |
33 | 31 | } |
| 32 | + throw new Error( |
| 33 | + `Unsupported platform: "${platformKey}". "weval does not have a precompiled binary for the platform/architecture you are using. You can open an issue on https://github.com/bytecodealliance/weval/issues to request for your platform/architecture to be included."` |
| 34 | + ); |
| 35 | + } |
34 | 36 |
|
35 | | - async function getJSON(url) { |
36 | | - let resp; |
37 | | - try { |
38 | | - resp = await fetch(url); |
39 | | - if (!resp.ok) { |
40 | | - throw new Error("non 2xx response code"); |
41 | | - } |
42 | | - return resp.json(); |
43 | | - } catch (err) { |
44 | | - const errMsg = err?.toString() ?? 'unknown error'; |
45 | | - console.error(`failed to fetch JSON from URL [${url}] (status ${resp?.status}): ${errMsg}`); |
46 | | - process.exit(1); |
47 | | - } |
48 | | - } |
49 | | - |
50 | | - const platformName = getPlatformName(); |
51 | | - const assetSuffix = (platform == 'win32') ? 'zip' : 'tar.xz'; |
52 | | - const exeSuffix = (platform == 'win32') ? '.exe' : ''; |
| 37 | + const platformName = getPlatformName(); |
| 38 | + const assetSuffix = platform == "win32" ? "zip" : "tar.xz"; |
| 39 | + const exeSuffix = platform == "win32" ? ".exe" : ""; |
53 | 40 |
|
54 | | - const exeDir = join(__dirname, platformName); |
55 | | - const exe = join(exeDir, `weval${exeSuffix}`); |
| 41 | + const exeDir = join(__dirname, platformName); |
| 42 | + const exe = join(exeDir, `weval${exeSuffix}`); |
56 | 43 |
|
57 | | - // If we already have the executable installed, then return it |
58 | | - if (existsSync(exe)) { |
59 | | - return exe; |
60 | | - } |
| 44 | + // If we already have the executable installed, then return it |
| 45 | + if (existsSync(exe)) { |
| 46 | + return exe; |
| 47 | + } |
61 | 48 |
|
62 | | - await mkdir(exeDir, { recursive: true }); |
63 | | - let repoBaseURL = `https://api.github.com/repos/bytecodealliance/weval`; |
64 | | - let response = await getJSON(`${repoBaseURL}/releases/tags/${TAG}`); |
65 | | - let id = response.id; |
66 | | - let assets = await getJSON(`${repoBaseURL}/releases/${id}/assets`); |
67 | | - let releaseAsset = `weval-${TAG}-${platformName}.${assetSuffix}`; |
68 | | - let asset = assets.find(asset => asset.name === releaseAsset); |
69 | | - if (!asset) { |
70 | | - console.error(`Can't find an asset named ${releaseAsset}`); |
71 | | - process.exit(1); |
72 | | - } |
73 | | - let data = await fetch(asset.browser_download_url); |
74 | | - if (!data.ok) { |
75 | | - console.error(`Error downloading ${asset.browser_download_url}`); |
76 | | - process.exit(1); |
77 | | - } |
78 | | - let buf = await data.arrayBuffer(); |
| 49 | + await mkdir(exeDir, { recursive: true }); |
| 50 | + const downloadUrl = `https://github.com/bytecodealliance/weval/releases/download/${TAG}/weval-${TAG}-${platformName}.${assetSuffix}`; |
| 51 | + let data = await fetch(downloadUrl); |
| 52 | + if (!data.ok) { |
| 53 | + console.error(`Error downloading ${downloadUrl}`); |
| 54 | + process.exit(1); |
| 55 | + } |
| 56 | + let buf = await data.arrayBuffer(); |
79 | 57 |
|
80 | | - if (releaseAsset.endsWith('.xz')) { |
81 | | - buf = await xz.decompress(new Uint8Array(buf)); |
82 | | - } |
83 | | - await decompress(Buffer.from(buf), exeDir, { |
84 | | - // Remove the leading directory from the extracted file. |
85 | | - strip: 1, |
86 | | - plugins: [ |
87 | | - decompressUnzip(), |
88 | | - decompressTar() |
89 | | - ], |
90 | | - // Only extract the binary file and nothing else |
91 | | - filter: file => parse(file.path).base === `weval${exeSuffix}`, |
92 | | - }); |
| 58 | + if (downloadUrl.endsWith(".xz")) { |
| 59 | + buf = await xz.decompress(new Uint8Array(buf)); |
| 60 | + } |
| 61 | + await decompress(Buffer.from(buf), exeDir, { |
| 62 | + // Remove the leading directory from the extracted file. |
| 63 | + strip: 1, |
| 64 | + plugins: [decompressUnzip(), decompressTar()], |
| 65 | + // Only extract the binary file and nothing else |
| 66 | + filter: (file) => parse(file.path).base === `weval${exeSuffix}`, |
| 67 | + }); |
93 | 68 |
|
94 | | - return exe; |
| 69 | + return exe; |
95 | 70 | } |
96 | 71 |
|
97 | 72 | export default getWeval; |
0 commit comments