|
| 1 | +// Load in modules |
| 2 | +const fetch = require("node-fetch") |
| 3 | +const yaml = require("js-yaml") |
| 4 | +const cheerio = require("cheerio") |
| 5 | +const chalk = require("chalk") |
| 6 | + |
| 7 | +async function run() { |
| 8 | + // Grab down sites.yml |
| 9 | + let url = |
| 10 | + "https://raw.githubusercontent.com/gatsbyjs/gatsby/master/docs/sites.yml" |
| 11 | + |
| 12 | + let yamlStr |
| 13 | + |
| 14 | + try { |
| 15 | + yamlStr = await fetch(url).then(resp => resp.text()) |
| 16 | + } catch (err) { |
| 17 | + console.log(`[Err]: ${err.message}`) |
| 18 | + process.exit(1) |
| 19 | + } |
| 20 | + |
| 21 | + // Parse YAML |
| 22 | + let parsedYaml = yaml.safeLoad(yamlStr, "utf8") |
| 23 | + |
| 24 | + let sitesVisited = 0 |
| 25 | + let nonGatsbySiteCount = 0 |
| 26 | + let erroredOut = 0 |
| 27 | + let totalSitesCount = parsedYaml.length |
| 28 | + |
| 29 | + // Loop over each site |
| 30 | + for (let site of parsedYaml) { |
| 31 | + let siteUrl = site.main_url |
| 32 | + |
| 33 | + let siteHtml |
| 34 | + |
| 35 | + // Fetch site |
| 36 | + try { |
| 37 | + siteHtml = await fetch(siteUrl).then(resp => resp.text()) |
| 38 | + } catch (err) { |
| 39 | + console.log( |
| 40 | + `${chalk.red(`[Err]`)}: ${site.title} (${siteUrl}) ran into an error: ${ |
| 41 | + err.message |
| 42 | + }` |
| 43 | + ) |
| 44 | + sitesVisited++ |
| 45 | + erroredOut++ |
| 46 | + continue // Skip the rest of the check for this particular site |
| 47 | + } |
| 48 | + |
| 49 | + // Pass html into a parser |
| 50 | + let $ = cheerio.load(siteHtml) |
| 51 | + |
| 52 | + // Most Gatsby sites have an id of "___gatsby" |
| 53 | + let gatsbyContainer = $("#___gatsby") |
| 54 | + |
| 55 | + if (gatsbyContainer.length !== 0) { |
| 56 | + // The site is a gatsby site don't do anything |
| 57 | + sitesVisited++ |
| 58 | + } else { |
| 59 | + // The site is not a gatsby site, print out some info |
| 60 | + console.log( |
| 61 | + `${chalk.yellow(`[Notice]`)}: ${ |
| 62 | + site.title |
| 63 | + } (${siteUrl}) is not a Gatsby site` |
| 64 | + ) |
| 65 | + sitesVisited++ |
| 66 | + nonGatsbySiteCount++ |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + console.log( |
| 71 | + chalk.green( |
| 72 | + `We visited ${sitesVisited}/${totalSitesCount} sites. Out of them, ${nonGatsbySiteCount} sites were not a Gatsby site and ${erroredOut} errored out when visiting it.` |
| 73 | + ) |
| 74 | + ) |
| 75 | + |
| 76 | + // If there are any non Gatsby sites, fail (non-zero exit code) |
| 77 | + process.exit(nonGatsbySiteCount > 0 ? 1 : 0) |
| 78 | +} |
| 79 | + |
| 80 | +run() |
0 commit comments