-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d9de1d8
commit aa24856
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |