npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@openinf/gh-file-importer

v2.0.1

Published

Utility that imports arbitrary files from remote GitHub repositories

Downloads

59

Readme

The high-level goal of @openinf/gh-file-importer is to serve as a Node.js package containing a utility for importing arbitrary files from remote GitHub repos allowing users to make use of them locally. As is the case with any software project in continuous development, omissions and errors may exist, for which contributions are welcome.


Installation

@openinf/gh-file-importer runs on Node.js and is available via npm.

npm install @openinf/gh-file-importer

Usage

Import the GhFileImporter constructor based on your platform.

Node.js

Install with npm install @openinf/gh-file-importer

import { GhFileImporter } from '@openinf/gh-file-importer';

Options

Now instantiate your your API. All options are optional except for destDir, which is the location where your files will be stored.

import { GhFileImporter } from '@openinf/gh-file-importer';

const DIR_TEMP = './tmp';

const ghFileImporter = new GhFileImporter({ destDir: DIR_TEMP });

await ghFileImporter.importContents('tc39', 'proposals', 'README.md');

Note: if needing to circumvent exceeding the GitHub API rate limit, be sure to have an environment variable called GITHUB_TOKEN containing a GitHub person access token.

Logging

For custom logging, pass an object with debug, info, warn, and error methods as the log option.

const ghFileImporter = new GhFileImporter({
  destDir: DIR_TEMP,
  log: {
    debug: () => {},
    info: () => {},
    warn: console.warn,
    error: console.error
  }
};

Debug

The simplest way to receive debug information is to set the log client option to console.

const ghFileImporter = new GhFileImporter({
  destDir: DIR_TEMP,
  log: console,
});

If you like to support a configurable log level, we recommend using the console-log-level module.

const ghFileImporter = new GhFileImporter({
  destDir: DIR_TEMP,
  log: require("console-log-level")({ level: "info" }),
});

GhFileImporter

Kind: global class

new GhFileImporter(options)

Creates an instance of GhFileImporter.

Throws:

  • InvalidArgTypeError
  • InvalidArgValueError
  • InvalidPropertyValueError
  • MissingArgsError
  • MissingOptionError

| Param | Type | Description | | --- | --- | --- | | options | GhFileImporterOptions | undefined | The options object. |

ghFileImporter.fetchMetadata(owner, repo, path, ref) ⇒ Promise.<any>

Retrieves a repo or path's metadata.

Kind: instance method of GhFileImporter
Returns: Promise.<any> - An object containing the metadata repo or path's metadata.
Throws:

  • InvalidArgTypeError
  • InvalidArgValueError
  • InvalidArgsNumberError

See: https://docs.github.com/en/rest/reference/repos#get-repository-content

| Param | Type | Description | | --- | --- | --- | | owner | string | The username associated with the repository. | | repo | string | The repository name. | | path | string | undefined | The path to the file or folder. | | ref | string | undefined | The name of the commit/branch/tag. |

ghFileImporter.fetchFileContents(owner, repo, path, ref) ⇒ Promise.<string>

Retrieves a path's contents.

Kind: instance method of GhFileImporter
Returns: Promise.<string> - The file contents.
Throws:

  • InvalidArgTypeError
  • InvalidArgValueError
  • InvalidArgsNumberError

| Param | Type | Description | | --- | --- | --- | | owner | string | The username associated with the repository. | | repo | string | The repository name. | | path | string | The path to the file or folder. | | ref | string | undefined | The name of the commit/branch/tag. |

ghFileImporter.fetchFileContentsFromUrl(url) ⇒ Promise.<string>

Retrieves the file contents from the URL provided.

Kind: instance method of GhFileImporter
Returns: Promise.<string> - The file contents.
Throws:

  • InvalidArgTypeError
  • InvalidArgValueError

| Param | Type | Description | | --- | --- | --- | | url | string | The string representation of a remote file URL. |

ghFileImporter.importContents(url) ⇒ Promise.<string>

Imports a file into the directory provided for the destDir option.

Kind: instance method of GhFileImporter
Returns: Promise.<string> - The file contents.
Throws:

  • InvalidArgTypeError
  • InvalidArgValueError

| Param | Type | Description | | --- | --- | --- | | url | string | The string representation of a remote file URL. |

ghFileImporter.importContentsFromUrl(url) ⇒ Promise.<string>

Imports a file located at the supplied URL into the directory provided for the destDir option.

Kind: instance method of GhFileImporter
Returns: Promise.<string> - The file contents.
Throws:

  • InvalidArgTypeError
  • InvalidArgValueError

| Param | Type | Description | | --- | --- | --- | | url | string | The string representation of a remote file URL. |