Skip to content

Commit

Permalink
Create download-release.js
Browse files Browse the repository at this point in the history
  • Loading branch information
praveenperera committed Nov 26, 2024
1 parent d9de1d8 commit aa24856
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions npm/lib/download-release.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// @ts-check
"use strict";

const { REPO } = require("./constants");
const get = require("./get");

/**
* @param {string} repo
* @param {string} tag
*/
function getApiUrl(repo, tag) {
return `https://api.github.com/repos/${repo}/releases/tags/${tag}`;
}

/**
* @param {{ token?: string; version: string; }} opts
*/
async function getReleaseFromGitHubApi(opts) {
const downloadOpts = {
headers: {
"user-agent": "rustywind",
},
};

if (opts.token) {
downloadOpts.headers.authorization = `token ${opts.token}`;
}

console.log(`Finding rustywind ${opts.version} release`);
const release = await get(getApiUrl(REPO, opts.version), downloadOpts);
let jsonRelease;
try {
jsonRelease = JSON.parse(release);
} catch (e) {
throw new Error("Malformed API response: " + e.stack);
}

if (!jsonRelease.assets) {
throw new Error("Bad API response: " + JSON.stringify(release));
}

return jsonRelease;
}

/**
* @param {{ token?: string; version: string; }} opts
*/
module.exports = async (opts) => {
if (!opts.version) {
return Promise.reject(new Error("Missing version"));
}

return getReleaseFromGitHubApi(opts);
};

0 comments on commit aa24856

Please sign in to comment.