const fs = require('fs'); const got = require('got'); const emoji = require('github-emoji'); const jsdom = require('jsdom').JSDOM, options = { resources: "usable" }; const { getConfig, outDir } = require('./utils'); function convertToEmoji(text) { if (text == null) return; text = text.toString(); if (text.match(/(?<=:\s*).*?(?=\s*:)/gs) != null) { var str = text.match(/(?<=:\s*).*?(?=\s*:)/gs); str = str.filter(function (arr) { return /\S/.test(arr); }); for (i = 0; i < str.length; i++) { if (emoji.URLS[str[i]] != undefined) { var output = emoji.of(str[i]); var emojiImage = output.url.replace("assets-cdn.github", "github.githubassets"); text = text.replace(`:${str[i]}:`, ``); } } return text; } else { return text; } } module.exports.updateHTML = (username, sort, order, includeFork) => { //add data to assets/index.html jsdom.fromFile(`${__dirname}/assets/index.html`, options).then(function (dom) { let window = dom.window, document = window.document; (async () => { try { console.log("Building HTML/CSS..."); var repos = []; var tempRepos; var page = 1; if(sort == "star"){ do{ tempRepos = await got(`https://api.github.com/users/${username}/repos?per_page=100&page=${page++}`); tempRepos = JSON.parse(tempRepos.body); repos = repos.concat(tempRepos); } while(tempRepos.length == 100); if(order == "desc"){ repos = repos.sort(function(a, b) { return b.stargazers_count - a.stargazers_count; }); }else{ repos = repos.sort(function(a, b) { return a.stargazers_count - b.stargazers_count; }); } }else{ do{ tempRepos = await got(`https://api.github.com/users/${username}/repos?sort=${sort}&order=${order}&per_page=100&page=${page++}`); tempRepos = JSON.parse(tempRepos.body); repos = repos.concat(tempRepos); } while(tempRepos.length == 100); } for (var i = 0; i < repos.length; i++) { if(repos[i].fork == false){ document.getElementById("work_section").innerHTML += `
${repos[i].name}
${convertToEmoji(repos[i].description)}
  ${repos[i].language}   ${repos[i].stargazers_count}   ${repos[i].forks_count}
`; }else{ if(includeFork == true){ document.getElementById("forks").style.display = "block"; document.getElementById("forks_section").innerHTML += `
${repos[i].name}
${convertToEmoji(repos[i].description)}
  ${repos[i].language}   ${repos[i].stargazers_count}   ${repos[i].forks_count}
`; } } } var user = await got(`https://api.github.com/users/${username}`); user = JSON.parse(user.body); document.title = user.login; var icon = document.createElement("link"); icon.setAttribute("rel", "icon"); icon.setAttribute("href", user.avatar_url); icon.setAttribute("type", "image/png"); document.getElementsByTagName("head")[0].appendChild(icon); document.getElementById("profile_img").style.background = `url('${user.avatar_url}') center center` document.getElementById("username").innerHTML = `${user.name}@${user.login}`; //document.getElementById("github_link").href = `https://github.com/${user.login}`; document.getElementById("userbio").innerHTML = convertToEmoji(user.bio); document.getElementById("userbio").style.display = user.bio == null || !user.bio ? 'none' : 'block'; document.getElementById("about").innerHTML = `   ${user.company}   ${user.email}   ${user.blog}    ${user.location}    Available for hire`; //add data to config.json const data = await getConfig(); data[0].username = user.login; data[0].name = user.name; data[0].userimg = user.avatar_url; fs.writeFile(`${outDir}/config.json`, JSON.stringify(data, null, ' '), function (err) { if (err) throw err; console.log("Config file updated."); }); fs.writeFile(`${outDir}/index.html`, '' + window.document.documentElement.outerHTML, function (error) { if (error) throw error; console.log(`Build Complete, Files can be Found @ ${outDir}`); }); } catch (error) { console.log(error); } })(); }).catch(function (error) { console.log(error); }); }