dx: comment on PRs when fixtures have been added/changed (#192)
The goal here is to provide some sort of relatively easy preview for the PR reviewer to see if the fixture looks good, if the parsing is working, and to make suggestions easily.pull/194/head
parent
96640e3564
commit
d35f7bd5bf
@ -0,0 +1,28 @@
|
||||
/* eslint-disable */
|
||||
const bot = require('circle-github-bot').create();
|
||||
const Mercury = require('../dist/mercury.js');
|
||||
const fs = require("fs");
|
||||
|
||||
const run = async () => {
|
||||
const screenshot = process.argv[2];
|
||||
|
||||
const fixture = screenshot.split('tmp/artifacts/')[1].slice(0, -4);
|
||||
|
||||
const html = fs.readFileSync(`${fixture}`);
|
||||
const json = await Mercury.parse('http://example.com', html, { fallback: false });
|
||||
|
||||
const jsonPath = `${screenshot}-parsed.json`;
|
||||
const fixtureArtifactPath = "tmp/artifacts/" + fixture;
|
||||
|
||||
fs.writeFileSync(jsonPath, JSON.stringify(json));
|
||||
fs.writeFileSync(fixtureArtifactPath, html);
|
||||
|
||||
bot.comment(`
|
||||
<h3>${bot.env.commitMessage}</h3>
|
||||
Page: <strong>${bot.artifactLink(ffixtureArtifactPath, 'Page')}</strong>
|
||||
Screenshot: <strong>${bot.artifactLink(screenshot, 'Screenshot')}</strong>
|
||||
JSON: <strong>${bot.artifactLink(jsonPath, 'JSON')}</strong>
|
||||
`);
|
||||
}
|
||||
|
||||
run();
|
@ -0,0 +1,41 @@
|
||||
/* eslint-disable */
|
||||
const page = require('webpage').create();
|
||||
const system = require('system');
|
||||
const args = system.args;
|
||||
|
||||
const fixtures = args.slice(1)[0].slice(0, -1).split(',');
|
||||
const totalRenders = fixtures.length;
|
||||
|
||||
var renderCount = 0;
|
||||
function pageRenderComplete() {
|
||||
renderCount++;
|
||||
if (renderCount === totalRenders) {
|
||||
phantom.exit();
|
||||
} else {
|
||||
capturePage();
|
||||
}
|
||||
}
|
||||
|
||||
function capturePage() {
|
||||
const fixturePath = fixtures[renderCount]
|
||||
page.viewportSize = { width: 1366, height: 768 };
|
||||
|
||||
page.open(fixturePath, function() {
|
||||
// set default background to white (otherwise can sometimes get transparent bg in png
|
||||
const script = "function() { \
|
||||
var style = document.createElement('style'); \
|
||||
var text = document.createTextNode('body { background: #fff }'); \
|
||||
style.setAttribute('type', 'text/css'); \
|
||||
style.appendChild(text); \
|
||||
document.head.insertBefore(style, document.head.firstChild); \
|
||||
}";
|
||||
|
||||
page.evaluateJavaScript(script);
|
||||
|
||||
var filename = new Date();
|
||||
page.render('tmp/artifacts/' + fixturePath + '.png');
|
||||
pageRenderComplete();
|
||||
});
|
||||
}
|
||||
|
||||
capturePage();
|
@ -0,0 +1,27 @@
|
||||
#!/bin/bash
|
||||
|
||||
changes=( `git diff master --name-only` )
|
||||
|
||||
for fixture in "${changes[@]}"
|
||||
do
|
||||
# If one of the changed files is a fixture, hold onto it
|
||||
if [[ $fixture == "fixtures/"* ]]; then
|
||||
fixtures=$fixture,$fixtures
|
||||
fi
|
||||
done
|
||||
|
||||
if [[ $fixtures ]]; then
|
||||
# Take a screenshot of the fixture
|
||||
yarn phantomjs scripts/generate-fixture-preview.js $fixtures
|
||||
|
||||
screenshots=( `find tmp/artifacts -type f | grep ".html.png"` )
|
||||
|
||||
for screenshot in "${screenshots[@]}"
|
||||
do
|
||||
# Create a comment with a link to the screenshot
|
||||
# and json output for the fixture
|
||||
node scripts/comment-on-pr.js $screenshot
|
||||
done
|
||||
else
|
||||
echo "No fixtures added in this PR, so no preview needed"
|
||||
fi
|
Loading…
Reference in New Issue