Skip to content

Instantly share code, notes, and snippets.

@one19
Last active December 14, 2020 23:09
Show Gist options
  • Save one19/3309bb12b5531c65f2737a2f15801327 to your computer and use it in GitHub Desktop.
Save one19/3309bb12b5531c65f2737a2f15801327 to your computer and use it in GitHub Desktop.
recursively grab all JSON and give them id that won't change on changed content
import axios from 'axios';
const token = process.env.GIT_TOKEN = '';
const gitAxios = axios.create({
headers: { 'authorization': `Token ${token}` },
});
const parseContent = (base64, id) => {
const buff = Buffer.from(base64, 'base64');
const raw = JSON.parse(buff.toString());
return { id, ...raw };
}
const getJSON = async gitGetter => {
const repoFiles = await gitGetter.get(`https://api.github.com/repos/compono/courseplayer-modules/git/trees/master`, { recursive: 1 });
const moduleFolders = repoFiles.data.tree.filter(e => e.type !== 'blob' && !e.path.match(/.vscode/gi));
const moduleFiles = await Promise.all(moduleFolders.map(e => gitGetter.get(e.url, { recursive: 1 })));
return moduleFiles.map((modules, i) => modules.map(module => parseContent(module.data.content, `${moduleFolders[i].path}${module.path}`))).flat();
}
const jsonBlobs = await getJSON(gitAxios);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment