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. We are constantly working to
improve this repository, so please feel free to contribute if
you notice any omissions or errors.
Thanks!
Supported Node.js Environments
- v4:Argon (Ar)
- v6:Boron (B)
- v8:Carbon (C)
- v10:Dubnium (Db)
- v12:Erbium (Er)
- v14:Fermium (Fm)
- v16:Gallium (Ga)
- v18:Hydrogen (H)
@openinf/gh-file-importer runs on
supported versions of Node.js and is available via
npm, pnpm, or yarn.
Using the npm CLI
See the official documentation for this command for more information.
npm i @openinf/gh-file-importerUsing the pnpm CLI
See the official documentation for this command for more information.
pnpm add @openinf/gh-file-importerUsing the Yarn 1 CLI (Classic)
See the official documentation for this command for more information.
yarn add @openinf/gh-file-importerImport the GhFileImporter constructor based on your platform.
import { GhFileImporter } from '@openinf/gh-file-importer';Now instantiate 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.
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
}
};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" }),
});Kind: global class
- GhFileImporter
- new GhFileImporter(options)
- .fetchMetadata(owner, repo, path, ref) ⇒
Promise<any> - .fetchFileContents(owner, repo, path, ref) ⇒
Promise<string> - .fetchFileContentsFromUrl(url) ⇒
Promise<string> - .importContents(url) ⇒
Promise<string> - .importContentsFromUrl(url) ⇒
Promise<string>
Creates an instance of GhFileImporter.
Throws:
InvalidArgTypeErrorInvalidArgValueErrorInvalidPropertyValueErrorMissingArgsErrorMissingOptionError
| Param | Type | Description |
|---|---|---|
| options | GhFileImporterOptions | undefined |
The options object. |
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:
InvalidArgTypeErrorInvalidArgValueErrorInvalidArgsNumberError
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. |
Retrieves a path's contents.
Kind: instance method of GhFileImporter
Returns: Promise<string> - The file contents.
Throws:
InvalidArgTypeErrorInvalidArgValueErrorInvalidArgsNumberError
| 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. |
Retrieves the file contents from the URL provided.
Kind: instance method of GhFileImporter
Returns: Promise<string> - The file contents.
Throws:
InvalidArgTypeErrorInvalidArgValueError
| Param | Type | Description |
|---|---|---|
| url | string |
The string representation of a remote file URL. |
Imports a file into the directory provided for the destDir option.
Kind: instance method of GhFileImporter
Returns: Promise<string> - The file contents.
Throws:
InvalidArgTypeErrorInvalidArgValueError
| Param | Type | Description |
|---|---|---|
| url | string |
The string representation of a remote file URL. |
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:
InvalidArgTypeErrorInvalidArgValueError
| Param | Type | Description |
|---|---|---|
| url | string |
The string representation of a remote file URL. |
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. If for whatever reason you spot something to fix but cannot patch it yourself, please open an issue.
This project is licensed under either of
at your option.
The SPDX license identifier for this project is
MIT OR Apache-2.0.
If you like the project (or want to bookmark it) —
— give it a star ⭐️ — it will greatly encourage
us.