Solved, question with improved code
I've created a simple npm module and CLI program to get the latest GitHub and Bitbucket release from their APIs using Node.js. Please tell me if there's anything to improve!
Module code:
#!/usr/bin/env node
const fetch = require("node-fetch");
/**
* @param {string} string
* @return {string}
*/
const normalize = (string) => {
try {
return string.toUpperCase().trim()
} catch (e) {
return string
}
}
/**
* @param {string} provider
* @param {string} user
* @param {string} repo
* @param {string} part
*/
module.exports.getRelease = async ({provider, user, repo, part = ""}) => {
if (normalize(provider) === normalize("github")) {
let json = (await (await fetch(`https://api.github.com/repos/${user}/${repo}/releases/latest`)).json())
if (json.message === "Not Found") throw "Invalid repository"
if (!("assets" in json)) throw "Rate limit exceeded"
let browser_download_urls = json.assets.map(asset => asset.browser_download_url)
return browser_download_urls.filter(url => url.includes(part))
} else if (normalize(provider) === normalize("bitbucket")) {
let json = (await (await fetch(`https://api.bitbucket.org/2.0/repositories/${user}/${repo}/downloads/`)).json())
if (json.type === "error") throw "Invalid repository"
let links = json.values.map(value => value.links.self.href)
return links.filter(url => url.includes(part))
} else {
throw "Invalid provider"
}
}
const usage = _ => {
console.log(
`Usage: get-release (github|bitbucket) user repo [partofreleasefile]
Ex: get-release github phhusson treble_experimentations
get-release github phhusson treble_experimentations arm64-ab-gapps
get-release bitbucket JesusFreke smali
get-release bitbucket JesusFreke smali baksmali`
)
process.exit(1)
}
// If via CLI
if (require.main === module) {
let args = process.argv.slice(2)
if (args.length !== 3 && args.length !== 4) {
usage()
}
module.exports.getRelease({
provider: args[0],
user: args[1],
repo: args[2],
part: args[3]
}).then(result => {
if (result.length !== 1) {
console.log(result)
} else {
console.log(result[0])
}
}).catch(error => {
console.log(error)
usage()
process.exit(1)
})
}
Called using:
const { getRelease } = require("get-release")
;(async _ => {
let url = await getRelease(
{
provider: "github",
user: "phhusson",
repo: "treble_experimentations",
part: "arm64-ab-gapps"
}
)
console.log(url[0])
})()
await
in(await (await fetch(
, does it not work with a singleawait
? \$\endgroup\$