Introduction
If you want to interact with a single GitHub project or collection using JavaScript, you can use the GithubProject class. This class allows you to read, write, and delete files from a GitHub repository using the isomorphic-git library.
To use the GithubProject class, you need to provide several parameters to the constructor, including:
- File system
- Repository information
- Branch name
- GitHub access token
Additionally, the GithubProject class includes a default corsProxy parameter to handle any CORS-related issues that may arise.
Installation
npm i slimplate/github-gitUsage
import GithubProject from '@slimplate/github-git' import fs from 'fs'
import GithubProject from '@slimplate/github-git'
const repo = {
full_name: 'my-org/my-repo'
}
const branch = 'main'
const token = 'my-github-access-token'
const user = 'my-github-username'
const repoUrl = `https://github.com/${repo.full_name}.git`
const corsProxy = 'https://cors.isomorphic-git.org'
const git = new GithubProject(fs, repo, branch, token, user, repoUrl, corsProxy)
await git.init()Check out the GithubProject code here (opens in a new tab)
git handlers
init- Pulls or clones a repo to local filesystemclone- Clone a repofetch- Fetches updates to remotepush- Git push from local filesystempull- Git pull to local filesystemwrite: (filename, contents)- Write a single file to filesystemrm: (filepath, opts)- Remove a file or dir, recursively, from filesystemlog- Get a git logstatus- Get a status for the projectlistServerRefs- Get a remote list of git refsread: (filename, encoding)- Get a single file from filesystemdiff: (commitHash1, commitHash2)- Get diff between two commitsglob: (glob)- Get all the filenames that match a glob in local filesystemallFiles: (rootDir, existing)- Get a list of all files in local filesystemgetAuthUrl- Get the URL to this repo, but with username/password (for basic auth)exists: (filename)- Returns true if the file/dir exists in filesystyemcheckStatus- Check the status of the entire projectadd: (filepath, opts)- Git add a filecommit: (message, opts)- Git commit local filesystem, message is a string-templatemkdirp: (dirname)- Recursively makes a directoryfixCollection: (collection, name)- Fill in any missing fields with reasonable defaultsparseCollection: (collection, name)- Parse all content in a collection (from local filesystem)