Skip to content

Instantly share code, notes, and snippets.

@sabicalija
Created August 22, 2018 13:31
Show Gist options
  • Save sabicalija/378204c89f3ce6fc70f653aaa6f6c055 to your computer and use it in GitHub Desktop.
Save sabicalija/378204c89f3ce6fc70f653aaa6f6c055 to your computer and use it in GitHub Desktop.
const fetch = require("node-fetch");
let api = "https://api.github.com/repos/asterics/AsTeRICS/";
let raw = "https://raw.githubusercontent.com/asterics/AsTeRICS/";
let path = "Documentation/ACS-Help/HTML/";
let tags = "https://api.github.com/repos/asterics/AsTeRICS/tags/";
let sections = ["Plugins", "ACS", "ARE"];
let link = `${api}contents/${path}Plugins`;
async function asyncForEach(array, cb) {
for (let i = 0; i < array.length; i++) [await cb(array[i], i, array)];
}
const getContent = async l => {
return await getS(l);
};
const getS = async (url, data = []) => {
let response = await fetch(url);
let ret = await response.json();
let hasChilds = false;
let n = [];
ret.forEach(v => {
if (v.type == "dir") hasChilds = true;
let c = { node: v, contents: [] };
n.push(c);
});
if (hasChilds) {
await asyncForEach(n, async v => {
if (v.node.type == "dir") {
let c = await getS(v.node.url);
v.contents = [...c];
let ftw = 0;
}
});
}
return n;
};
console.log(link);
getContent(link).then(r => {
console.log(r);
});
@sabicalija
Copy link
Author

const fetch = require("node-fetch");

let api = "https://api.github.com/repos/asterics/AsTeRICS/";
let raw = "https://raw.githubusercontent.com/asterics/AsTeRICS/";
let path = "Documentation/ACS-Help/HTML/";
let tags = "https://api.github.com/repos/asterics/AsTeRICS/tags/";
let sections = ["Plugins", "ACS", "ARE"];

let link = `${api}contents/${path}Plugins`;

async function asyncForEach(array, cb) {
  for (let i = 0; i < array.length; i++) [await cb(array[i], i, array)];
}

const getContent = async l => {
  return await getS(l);
};

const getS = async (url, data = []) => {
  let response = await fetch(url);
  let ret = await response.json();

  let hasChilds = false;
  let n = [];
  ret.forEach(v => {
    if (v.type == "dir") hasChilds = true;

    if ((v.type == "file" && new RegExp("html?$").test(v.name)) || v.type == "dir") {
      let c = { node: v, contents: [] };
      n.push(c);
    }
  });

  if (hasChilds) {
    await asyncForEach(n, async v => {
      if (v.node.type == "dir" && v.node.name != "img") {
        let c = await getS(v.node.url);
        v.contents = [...c];
        let ftw = 0;
      }
    });
  }
  return n;
};

console.log(link);
getContent(link).then(r => {
  console.log(r);
});

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