Created
June 19, 2018 12:13
-
-
Save leomelzer/2c195c87a3bc85806fdbc4879fa7d117 to your computer and use it in GitHub Desktop.
metatag-diff: Compare (diff) metatags of prod and stage host version given an article slug.
This file contains hidden or 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
const fetch = require("node-fetch"); | |
const cheerio = require("cheerio"); | |
const diff = require("diff"); | |
const colors = require("colors"); | |
const inputSlug = process.argv[2]; | |
const notImplemented = [ | |
"[rel=amphtml]", | |
"[property='og:image']", | |
"[property='og:description']" | |
]; | |
const PROD_HOST = "https://some-prod-host.example/"; | |
const DEV_HOST = "https://basic:[email protected]/"; | |
function fetchHtml(slug) { | |
async function getHtml(url) { | |
const response = await fetch(url); | |
return await response.text(); | |
} | |
return Promise.all([ | |
getHtml(`${PROD_HOST}${slug}`), | |
getHtml(`${DEV_HOST}${slug}`) | |
]); | |
} | |
function extractMetatags(html) { | |
const $ = cheerio.load(html); | |
return $("meta, link, title") | |
.not(notImplemented.join(", ")) | |
.map((i, tag) => $.html(tag).trim()) | |
.get(); | |
} | |
function compare([prodTags, stageTags]) { | |
prodTags.forEach((check, index) => { | |
const parts = diff.diffChars(check, stageTags[index] || ""); | |
parts.forEach(part => { | |
const color = part.added ? "green" : part.removed ? "red" : "grey"; | |
// This uses the extended String prototype (see `colors` package) | |
process.stderr.write(part.value[color]); | |
}); | |
console.log(); | |
}); | |
} | |
function main(slug) { | |
fetchHtml(slug) | |
.then(responses => responses.map(extractMetatags)) | |
.then(metatags => metatags.map(metatag => metatag.sort())) | |
.then(compare) | |
.catch(err => console.log(err)); | |
} | |
main(inputSlug); |
This file contains hidden or 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
{ | |
"name": "metatag-diff", | |
"version": "1.0.0", | |
"description": "Compare (diff) metatags of prod and stage host version given an article slug.", | |
"main": "index.js", | |
"scripts": { | |
"start": "node index.js" | |
}, | |
"author": "leomelzer", | |
"license": "UNLICENSED", | |
"dependencies": { | |
"cheerio": "^1.0.0-rc.2", | |
"colors": "^1.3.0", | |
"diff": "^3.5.0", | |
"node-fetch": "^2.1.2" | |
}, | |
"devDependencies": { | |
"eslint": "^4.19.1", | |
"prettier": "^1.13.5" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment