#ghdl
ghdl
(github download) is a node module that searches github repositories using the github api for a given repository name and returns download info of all tags of the given repo name if it is a real github repo with valid release(s) that have some file(s) to download because otherwise the api returns empty assets
for the repo.
You don't need to know the name of the user or the owner; just provide the name of a repository. For the most cases that ghdl
finds only one match, it returns an object
with download info of all tags of the given repo (see details below); however, in cases where there is more than one repo with the same name and with real releases (tags), it returns an array
of objects that each object contain download info of each repo.
If you are looking for a convenient front-end to get download info af a github release on your terminal and don't want to deal with the api, you can use ghdl-cli.
###install
npm install ghdl
###api
ghdl(repo_name, opts, callback)
repo_name
{String} -- repository name to get its download infoopts
{Object} -- gh-got options
- the most important one is
opts.token
, which is the GitHub access token and can be set globally with GITHUB_TOKEN
environment variable
callback
{Function} -- gets two arguments (err, repo)
err
is an Error object (you get the error message by err.message)repo
is
object
if the given name matches only one real repo with valid tagsarray
of objects if the name matches more than one repo with real tagsnull
if no matches found
In order to make authenticated requests to github api, the GitHub access token can be used that works the same as OAuth tokens and can easily be generated on GitHub. It can be set globally with GITHUB_TOKEN
environment variable.
each result object has these properties:
{
url: '',
tags: [{
tagname: '',
published_at: '',
files: [{
filename: '',
updated_at: '',
download: 0
}]
}]
}
The latest tag
is the first element of result object's tags array; that is, repo.tags[0]
.
###api usage
Based on the search result, ghdl
returns:
null
-- if no matches found.object
-- if only one repo foundarray
of objects -- if more than one repo with valid tags found
get download info of all tags
const ghdl = require('ghdl');
const opts = {
token: 'foo',
headers: {
'user-agent': 'https://github.com/mawni/ghdl'
}
};
var repo_name = 'githubRepoName';
ghdl(repo_name, opts, (err, result) => {
if (err) {
console.error(err.message);
}
if (result && !Array.isArray(result)) {
console.log(result.url);
console.log(result.tags);
} else if (result && Array.isArray(result)) {
result.forEach((repo) => {
console.log(repo.url);
console.log(repo.tags);
});
} else {
console.log(`no github release found for ${repo_name}.`);
}
});
get download info of only the latest tag
const ghdl = require('ghdl');
ghdl('githubRepoName', {token: 'foo'}, (err, result) => {
if (err) {
console.error(err.message);
}
if (result && !Array.isArray(result)) {
var latest_tag = result.tags[0];
console.log(result.url);
console.log(latest_tag.tagname);
console.log(latest_tag.published_at);
console.log(latest_tag.files);
} else if (result && Array.isArray(result)) {
result.forEach((repo) => {
var latest_tag = repo.tags[0];
console.log(repo.url);
console.log(latest_tag.tagname);
console.log(latest_tag.published_at);
console.log(latest_tag.files);
});
} else {
console.log(`no github release found for ${repo_name}.`);
}
});
Enjoy using ghdl
.