Last active
December 14, 2020 23:09
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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