From cff635b940ef6ae6233224fcdff297b068985d54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96nder=20Ceylan?= Date: Sun, 23 Feb 2020 19:13:43 +0100 Subject: [PATCH] fix(puppets): fixed failing scrape of Apple splash screen specs Fixed failing scrape of Apple splash screen specs fix #168 --- src/config/constants.ts | 2 +- src/helpers/puppets.ts | 105 ++++++++++++++++++++-------------------- 2 files changed, 53 insertions(+), 54 deletions(-) diff --git a/src/config/constants.ts b/src/config/constants.ts index 0e3e55c4..8473775e 100644 --- a/src/config/constants.ts +++ b/src/config/constants.ts @@ -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/', diff --git a/src/helpers/puppets.ts b/src/helpers/puppets.ts index 9f76e6eb..d43f1bb7 100644 --- a/src/helpers/puppets.ts +++ b/src/helpers/puppets.ts @@ -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}`;