xref: /expo/docs/scripts/test-links.js (revision 33efd9c7)
1import puppeteer from 'puppeteer';
2
3const url = process.argv[2];
4
5if (!url) {
6  console.error(`You need to provide the base URL for links as a parameter.`);
7  process.exit(-1);
8}
9
10const externalLinks = [
11  '/versions/latest/workflow/expo-cli/', // https://github.com/expo/expo-cli/blob/main/packages/expo-cli/README.md and https://github.com/expo/expo-cli/blob/main/README.md
12  '/versions/latest/workflow/configuration/', // https://github.com/expo/expo-cli/blob/main/CONTRIBUTING.md and https://github.com/expo/expo-cli/blob/main/packages/expo-cli/src/commands/init.js and https://github.com/expo/expo-cli/blob/main/packages/xdl/src/project/Doctor.js
13  // https://github.com/expo/expo-cli/blob/4e16a55e98e0612f71685ed16b3b5f8405219d4a/packages/xdl/README.md#xdl [Documentation](https://docs.expo.dev/versions/devdocs/index.html)
14  '/versions/latest/distribution/building-standalone-apps/#switch-to-push-notification-key-on-ios', // https://github.com/expo/expo-cli/blob/main/packages/expo-cli/src/commands/build/ios/credentials/constants.js
15  '/versions/latest/distribution/building-standalone-apps/#2-configure-appjson', // https://github.com/expo/expo-cli/blob/main/packages/expo-cli/src/commands/build/AndroidBuilder.js
16  '/versions/latest/distribution/building-standalone-apps/#if-you-choose-to-build-for-android', // https://github.com/expo/expo-cli/blob/main/packages/expo-cli/src/commands/build/AndroidBuilder.js
17  '/versions/latest/workflow/linking/', // https://github.com/expo/expo-cli/blob/main/packages/xdl/src/detach/Detach.ts
18  '/versions/latest/workflow/configuration/#ios', // https://github.com/expo/expo-cli/blob/main/packages/xdl/src/detach/Detach.js
19  '/versions/latest/guides/splash-screens/#differences-between-environments---android', // https://github.com/expo/expo-cli/blob/main/packages/xdl/src/Android.js
20  '/versions/latest/sdk/overview/', // https://github.com/expo/expo-cli/blob/main/packages/xdl/src/project/Convert.js
21  '/versions/latest/distribution/building-standalone-apps/#2-configure-appjson', // https://github.com/expo/expo-cli/blob/main/packages/expo-cli/src/commands/build/ios/IOSBuilder.js
22  '/versions/latest/introduction/faq/#can-i-use-nodejs-packages-with-expo', // https://github.com/expo/expo-cli/blob/main/packages/xdl/src/logs/PackagerLogsStream.js
23];
24
25(async () => {
26  try {
27    const notFound = [];
28    const redirectsFailed = [];
29
30    const browser = await puppeteer.launch();
31    const page = await browser.newPage();
32
33    for (const link of externalLinks) {
34      const response = await page.goto(`${url}${link}`);
35      if (response.status() === 404) {
36        await page.waitForFunction(
37          () => document.querySelector('#redirect-link') || document.querySelector('#__not_found'),
38          { timeout: 500 }
39        );
40        if (await page.$('#redirect-link')) {
41          await Promise.all([page.waitForNavigation(), page.click('#redirect-link')]);
42          console.info(`Redirected from ${link} to ${page.url()}`);
43          try {
44            await page.waitForFunction(
45              () =>
46                document.querySelector('#__redirect_failed') ||
47                document.querySelector('#__not_found'),
48              { timeout: 500 }
49            );
50            console.debug(`Redirect failed`);
51            redirectsFailed.push(link);
52          } catch {
53            console.debug(`Redirect successful`);
54          }
55        } else {
56          console.debug(`Couldn't find ${link}`);
57          notFound.push(link);
58        }
59      }
60    }
61    await browser.close();
62
63    if (notFound.length || redirectsFailed.length) {
64      if (notFound.length) {
65        console.error(`Pages not found for links: ${notFound.join(',')}`);
66      } else if (redirectsFailed.length) {
67        console.error(`Redirects failed for links: ${redirectsFailed.join(',')}`);
68      }
69      process.exit(-1);
70    }
71  } catch (e) {
72    console.error(e);
73    process.exit(-1);
74  }
75})();
76