Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@acdvorak
Created August 28, 2018 22:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acdvorak/eed231f361dc4e97e1ccd4862525fb86 to your computer and use it in GitHub Desktop.
Save acdvorak/eed231f361dc4e97e1ccd4862525fb86 to your computer and use it in GitHub Desktop.
Update all HTML file URLs in golden.json to the latest test pages
/**
* @fileoverview https://github.com/material-components/material-components-web/pull/3458
*/
const stableStringify = require('json-stable-stringify');
const LocalStorage = require('../infra/lib/local-storage');
const localStorage = new LocalStorage();
async function runAsync() {
const goldenJson = require('../golden.json');
for (const [htmlFilePath, htmlFileData] of Object.entries(goldenJson)) {
let goldenTimestamp = getTimestamp(htmlFileData.public_url);
for (const [userAgentAlias, imageFileUrl] of Object.entries(htmlFileData.screenshots)) {
const curHtmlFileUrl = getHtmlFileUrl(imageFileUrl);
const curTimestamp = getTimestamp(curHtmlFileUrl);
if (curTimestamp > goldenTimestamp) {
htmlFileData.public_url = curHtmlFileUrl;
goldenTimestamp = curTimestamp;
}
}
}
await localStorage.writeTextFile('test/screenshot/golden.json', stringify(goldenJson));
}
function stringify(object) {
return stableStringify(object, {space: ' '}) + '\n';
}
function getHtmlFileUrl(imageFileUrl) {
return imageFileUrl.replace(/(\.html).+$/, '$1');
}
function getTimestamp(htmlFileUrl) {
const regExp = new RegExp('/(201[78])/([0-9]{2})/([0-9]{2})/([0-9]{2})_([0-9]{2})_([0-9]{2})_([0-9]{3})/');
const [, year, month, day, hour, minute, second, ms] = (regExp.exec(htmlFileUrl) || []);
if (!year) {
throw new Error(`htmlFileUrl does not match timestamp regex: "${htmlFileUrl}"`);
}
return new Date(Date.UTC(year, month - 1, day, hour, minute, second, ms));
}
runAsync().catch((err) => console.error('Caught error:', err));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment