Détail du package

grab-github-release

prantlf72MIT2.1.0

Downloads and optionally unpacks an archive from GitHub release assets for the current platform.

github, github-release, download, release

readme

Grab GitHub Release

Latest version Dependency status Coverage

Downloads and optionally unpacks an archive from GitHub release assets for the current platform.

If used to install a binary executable to a NPM package, link-bin-executable can help.

Synopsis

import { grab } from 'grab-github-release'

try {
  const repository = 'prantlf/v-jsonlint'
  // downloads and unpacks the jsonlint executable to the current directory
  await grab({ repository, unpackExecutable: true })
} catch(err) {
  console.error(err.message)
  process.exitCode = 1
}

The archive with the executable is expected to be:

{name}-{platform}-{architecture}.zip

where:

  • {name} is the name of the tool (executable)
  • {platform} is the name of the target platform, by default: linux, darwin (also macos) and win32 (also windows)
  • {architecture} is the name of the target architecture, by default arm64 (also aarch64), riscv64 and x64 (also amd64, x86_64 or x86)

Installation

This package can be installed globally, if you want to use the grab-github-release script (or the ggr alias). You can install it during the first usage with npx too:

$ npm i -g grab-github-release
$ npx grab-github-release ...

This package can be installed locally too, if you want to use it programmatically:

$ npm i -g grab-github-release
$ npx grab-github-release ...

Make sure, that you use Node.js version 18 or newer.

Command-line Usage

Usage: [options]

Options:
  --clear-cache                 clears the cache, optionally for a "name"
  -r|--repository <repository>  GitHub repository formatted "owner/name"
  -i|--version-spec <semver>    semantic version specifier or "latest"
  -n|--name <file-name>         archive name without the platform suffix
  -p|--platform-suffixes <map>  platform name mapping
  -a|--arch-suffixes <map>      architecture name mapping
  -t|--target-dir <dir-name>    directory to write the output files to
  -e|--unpack-exe               unpack the executable and remove the archive
  -c|--cache                    use ~/.cache/grabghr as cache
  --force-cache                 use the cache to discover the latest version
  -g|--gh-token <token>         GitHub authentication token
  -v|--verbose                  prints extra information on the console
  -V|--version                  print version number and exit
  -h|--help                     print usage instructions and exit

The version specifier is "latest" by default. The file name will be inferred
from the first archive asset found for the current platform, if not specified.
If GitHub token is not specified, variables GITHUB_TOKEN and GH_TOKEN in the
process environment will be checked too.

Examples:
  $ grab-github-release -r prantlf/v-jsonlint -p darwin=macos,win32=windows:win64 -u
  $ grab-github-release -r prantlf/v-jsonlint -i >=0.0.6

API

// map where keys are Node.js platform names and values are their replacements
// to be used in names of archive looked for among  GitHub release assets
type ArchiveSuffixes = Record<string, string[]>

interface GrabOptions {
  // GitHub repository formatted "owner/name", mandatory
  repository: string
  // semantic version specifier or "latest"; defaults to "latest", if unspecified
  version?: string
  // archive name without the platform and architecture suffix
  // and without the ".zip" extension as well
  name?: string
  // recognised platforms organised by the Node.js platform name; defaults:
  // - darwin: darwin, macos
  // - linux: linux
  // - win32: win32, windows
  platformSuffixes?: ArchiveSuffixes
  // recognised architectures organised by the Node.js platform name; defaults:
  // - arm64: aarch64, arm64
  // - riscv64: riscv64
  // - x64: amd64, x86_64, x64, x86
  archSuffixes?: ArchiveSuffixes
  // directory to write the archive or executable to; if not specified,
  // files will be written to the current directory
  targetDirectory?: string
  // unpack the executable and remove the archive
  unpackExecutable?: boolean
  // store the downloaded archives from GitHub releases to the cache
  // in ~/.cache/grabghr; `true` is the default
  cache?: boolean
  // force using the cache for getting the last available version and avoid
  // connecting to GitHub if the cache isn't empty
  forceCache?: boolean
  // GitHub authentication token, overrides the environment variables
  // GITHUB_TOKEN or GH_TOKEN
  token?: string
  // print details about the program execution
  verbose?: boolean
}

interface GrabResult {
  // actual version number as specified or picked from the list of releases
  version: string
  // downloaded archive name, if not removed (and the executable not unpacked)
  archive?: string
  // executable file name, if it was unpacked (and the archive removed)
  executable?: string
}

interface ClearCacheOptions {
  // GitHub repository formatted "owner/name", mandatory
  repository: string
  // print details about the program execution
  verbose?: boolean
}

// downloads and optionally unpacks an archive from GitHub release assets
// for the current platform
export function grab(options: GrabOptions): Promise<GrabResult>

// clears the cache used for downloading archives from GitHub releases
export function clearCache(options?: ClearCacheOptions): Promise<void>

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.

License

Copyright (c) 2023-2024 Ferdinand Prantl

Licensed under the MIT license.

changelog

Changes

2.1.0 (2024-08-11)

Features

  • Add support for riscv64 architecture (ccdd729)

2.0.0 (2024-04-01)

Features

  • Allow forcing only cache to discover the latest version (6610088)
  • Clear cache by repository path, not by executable name (c25f03e)

BREAKING CHANGES

The mandatory parameter in options for clearCache is repository instead of name. Directories in the cache are named by the relative repository path instead of by the executable name. Replace name with repository in your calls to clearCache.

The cache contains directories with relative repository paths now. Not executable names any more. This invalidates the cache populated so far. You can clear the cache by running grab-github-release --clear-cache once and start populating it again.

1.0.0 (2024-03-31)

Bug Fixes

  • Upgrade depednencies (4af2502)
  • Use command-line args targetDirectory and token (edf3d57)

Features

  • Add function and command-line option for clearing cache (0d5ba64)
  • Save downloaded zip files to cache for being used next (6c3bf05)

BREAKING CHANGES

  • The package exports only named exports from now on. If you imported the function grab as a default export, import it by the name grab as a named export from now on. The command-line tool works as it did with no breaking change.
  • Although caching the archives downloaded from GitHub releases to ~/.cache/grabghr by default should be transparent and should not affect any usage scenario, it might influence the speed or disk usage of a particular application. That is why this is formally declared as a breaking change.

0.2.3 (2023-12-13)

Bug Fixes

  • Log if the GitHub API call was authorized (1ab57ea)

0.2.2 (2023-12-13)

Bug Fixes

  • Add GitHub API version header (66c2ab8)
  • Wait until the GitHub API rate limit allows the next request (f995aaa)

0.2.1 (2023-12-13)

Bug Fixes

  • Authorize fetches to overccome GitHub API rate limit (061abf8)

0.2.0 (2023-12-12)

Features

  • Allow mapping of architectures too (8d61855)

0.1.1 (2023-10-27)

Bug Fixes

  • Do not try processing failed requests (674a735)
  • Wait 5-10s between failing network requet attempts (054e377)

0.1.0 (2023-10-26)

Features

  • Allow setting the target directory to write ouptut files to (88b2f14)

0.0.1 (2023-10-26)

Initial release