const fs = require("fs"); const emoji = require("github-emoji"); const jsdom = require("jsdom").JSDOM, options = { resources: "usable" }; const { getConfig, outDir } = require("./utils"); const { getRepos, getUser } = require("./api"); function convertToEmoji(text) { if (text == null) return; text = text.toString(); var pattern = /(?<=:\s*).*?(?=\s*:)/gs; if (text.match(pattern) != null) { var str = text.match(pattern); str = str.filter(function(arr) { return /\S/.test(arr); }); for (i = 0; i < str.length; i++) { if (emoji.URLS[str[i]] != undefined) { text = text.replace( `:${str[i]}:`, `` ); } } return text; } else { return text; } } module.exports.updateHTML = (username, opts) => { const { includeFork, codepen, dev, dribbble, email, instagram, twitter } = opts; //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..."); const repos = await getRepos(username, opts); for (var i = 0; i < repos.length; i++) { let element; if (repos[i].fork == false) { element = document.getElementById("work_section"); } else if (includeFork == true) { document.getElementById("forks").style.display = "block"; element = document.getElementById("forks_section"); } else { continue; } element.innerHTML += `
${repos[i].name}
${convertToEmoji(repos[i].description)}
  ${ repos[i].language }   ${ repos[i].stargazers_count }   ${ repos[i].forks_count }
`; } const user = await getUser(username); 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.getElementsByTagName("head")[0].innerHTML += ` `; 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.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; await fs.writeFile( `${outDir}/config.json`, JSON.stringify(data, null, " "), function(err) { if (err) throw err; console.log("Config file updated."); } ); await fs.writeFile( `${outDir}/index.html`, "" + window.document.documentElement.outerHTML, function(error) { if (error) throw error; console.log(`Build Complete, Files can be Found @ ${outDir}\n`); } ); } catch (error) { console.log(error); } })(); }) .catch(function(error) { console.log(error); }); };