Skip to content

Commit

Permalink
fix(puppets): fixed failing scrape of Apple splash screen specs
Browse files Browse the repository at this point in the history
Fixed failing scrape of Apple splash screen specs

fix #168
  • Loading branch information
onderceylan committed Feb 24, 2020
1 parent 19f8cf6 commit cff635b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 54 deletions.
2 changes: 1 addition & 1 deletion src/config/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export default {
EMULATED_USER_AGENT:
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3641.0 Safari/537.36',
APPLE_HIG_SPLASH_SCR_SPECS_URL:
'https://developer.apple.com/design/human-interface-guidelines/ios/icons-and-images/launch-screen/',
'https://developer.apple.com/design/human-interface-guidelines/ios/visual-design/adaptivity-and-layout/',
APPLE_HIG_DEVICE_SCALE_FACTOR_SPECS_URL:
'https://developer.apple.com/design/human-interface-guidelines/ios/icons-and-images/image-size-and-resolution/',

Expand Down
105 changes: 52 additions & 53 deletions src/helpers/puppets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,66 +42,65 @@ const getAppleSplashScreenData = async (
throw e;
}

const splashScreenData = await page.evaluate(
({ selector }) => {
// TypeScript doesn't recognize the page context within callback
/* eslint-disable @typescript-eslint/ban-ts-ignore */
const scrapeSplashScreenDataFromHIGPage = (): LaunchScreenSpec[] =>
Array.from(document.querySelectorAll(selector)).map(tr => {
return Array.from(tr.querySelectorAll('td')).reduce(
const splashScreenData = await page.evaluate(() => {
// TypeScript doesn't recognize the page context within callback
/* eslint-disable @typescript-eslint/ban-ts-ignore */
const scrapeSplashScreenDataFromHIGPage = (): LaunchScreenSpec[] =>
Array.from(
document.querySelectorAll('table')?.[0].querySelectorAll('tbody tr'),
).map(tr => {
return Array.from(tr.querySelectorAll('td')).reduce(
// @ts-ignore
(acc, curr, index) => {
const appleLaunchScreenTableColumnOrder = [
'device',
'portrait',
'landscape',
];
const dimensionRegex = new RegExp(/(\d*)[^\d]+(\d*)[^\d]+/gm);
// @ts-ignore
(acc, curr, index) => {
const appleLaunchScreenTableColumnOrder = [
'device',
'portrait',
'landscape',
];
const dimensionRegex = new RegExp(/(\d*)[^\d]+(\d*)[^\d]+/gm);
// @ts-ignore
const keyToUpdate = appleLaunchScreenTableColumnOrder[index];
const execDimensionRegex = (
val: string,
): RegExpExecArray | null => {
return dimensionRegex.exec(val);
};

// @ts-ignore
const getDimensions = (val: string): Dimension => {
const regexMatch = execDimensionRegex(val);
const keyToUpdate = appleLaunchScreenTableColumnOrder[index];
const execDimensionRegex = (
val: string,
): RegExpExecArray | null => {
return dimensionRegex.exec(val);
};

if (regexMatch && regexMatch.length) {
return {
width: parseInt(regexMatch[1], 10),
height: parseInt(regexMatch[2], 10),
};
}
// @ts-ignore
const getDimensions = (val: string): Dimension => {
const regexMatch = execDimensionRegex(val);

if (regexMatch && regexMatch.length) {
return {
width: 1,
height: 1,
width: parseInt(regexMatch[1], 10),
height: parseInt(regexMatch[2], 10),
};
};
}

return {
// @ts-ignore
...acc,
[keyToUpdate]:
index > 0
? getDimensions((curr as HTMLElement).innerText)
: (curr as HTMLElement).innerText,
width: 1,
height: 1,
};
},
{
device: '',
portrait: { width: 0, height: 0 },
landscape: { width: 0, height: 0 },
},
) as LaunchScreenSpec;
});
/* eslint-enable @typescript-eslint/ban-ts-ignore */
return scrapeSplashScreenDataFromHIGPage();
},
{ selector: constants.APPLE_HIG_SPLASH_SCR_SPECS_DATA_GRID_SELECTOR },
);
};
return {
// @ts-ignore
...acc,
[keyToUpdate]:
index > 0
? getDimensions((curr as HTMLElement).innerText)
: (curr as HTMLElement).innerText,
};
},
{
device: '',
portrait: { width: 0, height: 0 },
landscape: { width: 0, height: 0 },
},
) as LaunchScreenSpec;
});
/* eslint-enable @typescript-eslint/ban-ts-ignore */
return scrapeSplashScreenDataFromHIGPage();
});

if (!splashScreenData.length) {
const err = `Failed scraping the data on web page ${constants.APPLE_HIG_SPLASH_SCR_SPECS_URL}`;
Expand Down

0 comments on commit cff635b

Please sign in to comment.