Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deps: inline binary-install, with updated axios version #1819

Merged
merged 5 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 121 additions & 3 deletions installers/npm/binary.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
const { Binary } = require("binary-install");
const os = require("os");
"use strict";

const axios = require("axios");
const cTable = require("console.table");
const libc = require("detect-libc");
const os = require("os");
const tar = require("tar");
const { configureProxy } = require("axios-proxy-builder");
const { existsSync, mkdirSync, rmSync } = require("fs");
const { join } = require("path");
const { spawnSync } = require("child_process");

const error = (msg) => {
console.error(msg);
Expand Down Expand Up @@ -49,7 +55,7 @@ const getPlatform = () => {
const type = os.type();
const architecture = os.arch();

for (supportedPlatform of supportedPlatforms) {
for (let supportedPlatform of supportedPlatforms) {
if (
type === supportedPlatform.TYPE &&
architecture === supportedPlatform.ARCHITECTURE
Expand Down Expand Up @@ -93,6 +99,118 @@ const getPlatform = () => {
);
};

/*! Copyright (c) 2019 Avery Harnish - MIT License */
class Binary {
constructor(name, url, config) {
let errors = [];
if (typeof url !== "string") {
errors.push("url must be a string");
} else {
try {
new URL(url);
} catch (e) {
errors.push(e);
}
}
if (name && typeof name !== "string") {
errors.push("name must be a string");
}

if (!name) {
errors.push("You must specify the name of your binary");
}
if (errors.length > 0) {
let errorMsg =
"One or more of the parameters you passed to the Binary constructor are invalid:\n";
errors.forEach(error => {
errorMsg += error;
});
errorMsg +=
'\n\nCorrect usage: new Binary("my-binary", "https://example.com/binary/download.tar.gz")';
error(errorMsg);
}
this.url = url;
this.name = name;
this.installDirectory =
config?.installDirectory || join(__dirname, "node_modules", ".bin");

if (!existsSync(this.installDirectory)) {
mkdirSync(this.installDirectory, { recursive: true });
}

this.binaryPath = join(this.installDirectory, this.name);
}

exists() {
return existsSync(this.binaryPath);
}

install(fetchOptions, suppressLogs = false) {
if (this.exists()) {
if (!suppressLogs) {
console.error(
`${this.name} is already installed, skipping installation.`
);
}
return Promise.resolve();
}

if (existsSync(this.installDirectory)) {
rmSync(this.installDirectory, { recursive: true });
}

mkdirSync(this.installDirectory, { recursive: true });

if (!suppressLogs) {
console.error(`Downloading release from ${this.url}`);
}

return axios({ ...fetchOptions, url: this.url, responseType: "stream" })
.then(res => {
return new Promise((resolve, reject) => {
const sink = res.data.pipe(
tar.x({ strip: 1, C: this.installDirectory })
);
sink.on("finish", () => resolve());
sink.on("error", err => reject(err));
});
})
.then(() => {
if (!suppressLogs) {
console.error(`${this.name} has been installed!`);
}
})
.catch(e => {
error(`Error fetching release: ${e.message}`);
});
}

run(fetchOptions) {
const promise = !this.exists()
? this.install(fetchOptions, true)
: Promise.resolve();

promise
.then(() => {
const [, , ...args] = process.argv;

const options = { cwd: process.cwd(), stdio: "inherit" };

const result = spawnSync(this.binaryPath, args, options);

if (result.error) {
error(result.error);
}

process.exit(result.status);
})
.catch(e => {
error(e.message);
process.exit(1);
});
}
}

const getBinary = () => {
const platform = getPlatform();
const download_host = process.env.npm_config_apollo_rover_download_host || process.env.APOLLO_ROVER_DOWNLOAD_HOST || 'https://rover.apollo.dev'
Expand Down
172 changes: 60 additions & 112 deletions installers/npm/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading