Skip to content

Downloading GitHub repository using API

This tutorial is part of package functions documentation in Iconify Tools.

Function downloadGitHubRepo() downloads files from GitHub repository using GitHub API.

Usage

Function has the following parameter:

  • options, object. Options, see below.

Function returns:

  • DownloadGitHubRepoResult object on success.
  • "not_modified" string if repository has not been updated since last run (can be returned only if ifModifiedSince option is set).

In case of error, function might throw an exception, which you can catch using try and catch.

Function is asynchronous. That means you need to handle it as Promise instance, usually by adding await before function call.

Options

Options object has the following mandatory properties:

  • target, string. Target directory. If directory is missing, it will be created. Value can contain "{hash}" that will be replaced with latest commit hash.
  • user, string. GitHub user or organisation, such as "iconify" for "git@github.com:iconify/tools.git".
  • repo, string. Repository name, such as "tools" for "git@github.com:iconify/tools.git".
  • branch, string. Branch, such as "master".
  • token, string. GitHub API token. See below.

and the following optional properties:

  • cleanup, boolean. If true, target directory will be emptied before exporting icons. Default is false.
  • ifModifiedSince, string|DownloadGitHubRepoResult. If set, function will check if repository has been updated.

Function downloads archive, puts it in target directory, then unpacks it in sub-directory. There are two optional properties that allow you remove outdated files without removing everything. Options do not work if cleanup is enabled because cleanup removes everything.

  • cleanupOldFiles, boolean. If true, old zip files in target directory will be removed. Default is false.
  • cleanupOldDirectories, boolean. If true, old unpacked archives in target directory will be removed. Default is true.

token

You can get GitHub API token from developer settings on GitHub.

Token does not need write or admin access, access to workflows, it requires only read access to repository you want to clone.

If you are accessing public repositories, checking "repo:status" and "public_repo" is enough.

If you are accessing private repositories, check "repo" section.

Never commit token to a repository or publish it anywhere! Keep it secret. There are many ways to keep token secret, most common is using environmental variables to pass token to script. There are plenty of tutorials available that explain different methods.

ifModifiedSince

Option ifModifiedSince is used when you want to retrieve data only if repository has been updated.

Value can be one of the following:

  • Commit hash as string. You can get it from hash property of result of previous run.
  • DownloadGitHubRepoResult value from previous run.

If repository has not been modified, function will return string "not_modified".

If option is not set, function cannot return "not_modified".

Result

Result object has the following properties:

  • downloadType = "github".
  • rootDir, string. Target directory. It is normalized version of target option, without trailing "/" and with "{hash}" replaced with commit hash.
  • contentsDir, string. Directory where archive was unpacked.
  • hash, string. Last commit hash.

Value is contentsDir always contains rootDir because archives are unpacked in sub-directory of rootDir. For example:

json{
   "downloadType": "github",
   "rootDir": "output/github-test",
   "contentsDir": "output/github-test/iconify-collections-json-4049946",
   "hash": "40499460e21011478a64c1cb1212f3308168462c"
}

Example

download-jam-icons.ts
tsimport { downloadGitHubRepo } from '@iconify/tools';

// GITHUB_TOKEN=ghp_12345 node example.js
const token = process.env.GITHUB_TOKEN || '';

(async () => {
   console.log(
       await downloadGitHubRepo({
           target: 'downloads/jam',
           user: 'michaelampr',
           repo: 'jam',
           branch: 'master',
           token,
       })
   );
})();

Released under the Apache 2.0 License.