Skip to content

Instantly share code, notes, and snippets.

@pdehaan
Created March 21, 2022 00:17
Show Gist options
  • Save pdehaan/ddc3bd53e88dca261ee802512c27aca6 to your computer and use it in GitHub Desktop.
Save pdehaan/ddc3bd53e88dca261ee802512c27aca6 to your computer and use it in GitHub Desktop.
Best guess at injecting GitHub usernames into src/_data/sites/*.json files.
import cp from "node:child_process";
import fse from "fs-extra";
import fetch from "node-fetch";
import glob from "fast-glob";
const files = await glob("*.json");
for (const file of files) {
// Pipe through `tail -1` to get original commit, set encoding to "utf-8" instead of default "buffer".
const firstCommit = cp.execSync(`git log --follow --format=oneline ${file} | tail -1`, {encoding: "utf-8"});
// Pluck the first row item which is the Git SHA of the commit.
const sha = firstCommit.split(/\s/, 1)[0];
// Fetch commit from repo using GitHub API so we can get the GitHub username of original PR author.
const res = await fetch(`https://api.github.com/repos/11ty/11ty-website/commits/${sha}`);
const body = await res.json();
const githubUsername = body.author.login;
const site = await fse.readJson(file);
site.github = githubUsername;
console.log({file, site});
// await fse.writeJson(file, site);
}

NOTE: I added this 11ty-website-sites-to-github.mjs file in the ./src/_data/sites/ directory, so it's looking for the *.json files in the current directory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment