|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const assert = require('assert'); |
| 4 | +const execa = require('execa'); |
| 5 | +const { join } = require('path'); |
| 6 | +const fetch = require('node-fetch'); |
| 7 | +const { copyFileSync, chmodSync, existsSync, mkdirSync } = require('fs'); |
| 8 | +const Installer = require('../installer'); |
| 9 | +const { platform } = require('../common'); |
| 10 | + |
| 11 | +const binaryName = platform.startsWith('win') ? 'boa.exe' : 'boa'; |
| 12 | + |
| 13 | +function getFilename() { |
| 14 | + switch (platform) { |
| 15 | + case 'darwin-x64': |
| 16 | + return 'boa-macos-amd64'; |
| 17 | + case 'linux-x64': |
| 18 | + return 'boa-linux-amd64'; |
| 19 | + case 'win32-x64': |
| 20 | + return 'boa-windows-amd64'; |
| 21 | + default: |
| 22 | + throw new Error(`No Boa builds available for ${platform}`); |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +class BoaInstaller extends Installer { |
| 27 | + constructor(...args) { |
| 28 | + super(...args); |
| 29 | + this.binPath = undefined; |
| 30 | + } |
| 31 | + |
| 32 | + static resolveVersion(version) { |
| 33 | + if (version === 'latest') { |
| 34 | + return fetch('https://api.github.com/repos/boa-dev/boa/releases/latest') |
| 35 | + .then((r) => r.json()) |
| 36 | + .then((r) => r.tag_name); |
| 37 | + } |
| 38 | + return version; |
| 39 | + } |
| 40 | + |
| 41 | + getDownloadURL(version) { |
| 42 | + return `https://github.com/boa-dev/boa/releases/download/${version}/${getFilename()}`; |
| 43 | + } |
| 44 | + |
| 45 | + async extract() { |
| 46 | + // The file is not zipped so we don't need to do any extraction here |
| 47 | + // The file doesn't seem to be executable so we need to set it manually |
| 48 | + chmodSync(this.downloadPath, '755'); |
| 49 | + // Windows will fail if the extractedPath doesn't exist |
| 50 | + if (!existsSync(this.extractedPath)) { |
| 51 | + mkdirSync(this.extractedPath); |
| 52 | + } |
| 53 | + return copyFileSync(this.downloadPath, join(this.extractedPath, binaryName)); |
| 54 | + } |
| 55 | + |
| 56 | + async install() { |
| 57 | + this.binPath = await this.registerBinary(binaryName); |
| 58 | + } |
| 59 | + |
| 60 | + async test() { |
| 61 | + const program = 'console.log("42");'; |
| 62 | + const output = '42\nundefined'; |
| 63 | + |
| 64 | + assert.strictEqual( |
| 65 | + (await execa(this.binPath, [], { input: program })).stdout, |
| 66 | + output, |
| 67 | + ); |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +BoaInstaller.config = { |
| 72 | + name: 'Boa', |
| 73 | + id: 'boa', |
| 74 | + supported: [ |
| 75 | + 'linux-x64', |
| 76 | + 'win32-x64', |
| 77 | + 'darwin-x64', |
| 78 | + ], |
| 79 | +}; |
| 80 | + |
| 81 | +module.exports = BoaInstaller; |
0 commit comments