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

Change javy-cli to download a specific version of Javy instead of the latest version #703

Merged
merged 3 commits into from
Aug 13, 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
4 changes: 4 additions & 0 deletions npm/javy-cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Changed

- Download version 3.0.1 of Javy instead of the latest released version of Javy.

## [0.2.0] - 2023-08-17

### Removed
Expand Down
20 changes: 4 additions & 16 deletions npm/javy-cli/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Javy npm package
# javy-cli npm package

**This package is deprecated. Please download the appropriate release from [Javy's release page](https://github.com/bytecodealliance/javy/releases).**

This is the npm package for Javy. The package contains a small Node script
that downloads the appropriate Javy binary on demand and invokes it with the
parameters given.
parameters given.

## Usage

Expand All @@ -13,17 +15,3 @@ $ npm install -g javy-cli
# Directly invoke it via npm
$ npx javy-cli@latest
```

## Updating javy

The npm package will automatically download the newest version of Javy if a
newer version is available.

## Using a specific version of javy

To use a specific version of Javy, set the environment variable
`FORCE_RELEASE` to the version you would like to use.

```
FORCE_RELEASE=v1.1.0 npx javy-cli@latest
```
37 changes: 5 additions & 32 deletions npm/javy-cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import cachedir from "cachedir";

const REPO = "bytecodealliance/javy";
const NAME = "javy";
const VERSION = "v3.0.1";

async function main() {
try {
const version = await getDesiredVersionNumber();
if (!(await isBinaryDownloaded(version))) {
await downloadBinary(version);
if (!(await isBinaryDownloaded(VERSION))) {
await downloadBinary(VERSION);
}
const result = childProcess.spawnSync(binaryPath(version), getArgs(), {
const result = childProcess.spawnSync(binaryPath(VERSION), getArgs(), {
stdio: "inherit",
});
process.exitCode = result.status === null ? 1 : result.status;
Expand All @@ -30,7 +30,7 @@ async function main() {
// We delete the cached binary because that cached binary will never run successfully and
// stops `javy-cli` from redownloading the binary.
console.error(`${NAME} was not downloaded correctly. Please retry.`);
fs.unlinkSync(binaryPath(version));
fs.unlinkSync(binaryPath(VERSION));
}
} catch (e) {
console.error(e);
Expand Down Expand Up @@ -80,33 +80,6 @@ async function downloadBinary(version) {
await fs.promises.chmod(binaryPath(version), 0o775);
}

/**
* getDesiredVersionNumber returns the version number of the release that
* should be downloaded and launched. If the FORCE_RELEASE env variable is set,
* that will be used as the desired version number, if not, we determine the
* latest release available on GitHub.
*
* GitHub has a public Release API, but rate limits it per IP, so that the
* CLI can end up breaking. Instead, we use a little trick. You can download
* artifacts from the latest release by using `latest` as your version number.
* The server will respond with a 302 redirect to the artifact's URL. That URL
* contains the actual release version number, which we can extract.
*/
async function getDesiredVersionNumber() {
if (process.env.FORCE_RELEASE) return process.env.FORCE_RELEASE;
const resp = await fetch(
`https://github.com/${REPO}/releases/latest/download/lol`,
{ redirect: "manual" }
);
if (resp.status != 302) {
throw Error(
`Could not determine latest release using the GitHub (Status code ${resp.status
}): ${await resp.text().catch(() => "<No error message>")}`
);
}
return resp.headers.get("location").split("/").at(-2);
}

function binaryUrl(version) {
return `https://github.com/${REPO}/releases/download/${version}/${NAME}-${platarch()}-${version}.gz`;
}
Expand Down
Loading