github-git

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-git

Usage

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 filesystem
  • clone - Clone a repo
  • fetch - Fetches updates to remote
  • push - Git push from local filesystem
  • pull - Git pull to local filesystem
  • write: (filename, contents) - Write a single file to filesystem
  • rm: (filepath, opts) - Remove a file or dir, recursively, from filesystem
  • log - Get a git log
  • status - Get a status for the project
  • listServerRefs - Get a remote list of git refs
  • read: (filename, encoding) - Get a single file from filesystem
  • diff: (commitHash1, commitHash2) - Get diff between two commits
  • glob: (glob) - Get all the filenames that match a glob in local filesystem
  • allFiles: (rootDir, existing) - Get a list of all files in local filesystem
  • getAuthUrl - Get the URL to this repo, but with username/password (for basic auth)
  • exists: (filename) - Returns true if the file/dir exists in filesystyem
  • checkStatus - Check the status of the entire project
  • add: (filepath, opts) - Git add a file
  • commit: (message, opts) - Git commit local filesystem, message is a string-template
  • mkdirp: (dirname) - Recursively makes a directory
  • fixCollection: (collection, name) - Fill in any missing fields with reasonable defaults
  • parseCollection: (collection, name) - Parse all content in a collection (from local filesystem)