xref: /expo/docs/scripts/test-links.js (revision e330c216)
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/expokit/eject/', // https://github.com/expo/expo-cli/blob/main/packages/expo-cli/src/commands/eject/Eject.js
23  '/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
24];
25
26(async () => {
27  try {
28    const notFound = [];
29    const redirectsFailed = [];
30
31    const browser = await puppeteer.launch();
32    const page = await browser.newPage();
33
34    for (const link of externalLinks) {
35      const response = await page.goto(`${url}${link}`);
36      if (response.status() === 404) {
37        await page.waitForFunction(
38          () => document.querySelector('#redirect-link') || document.querySelector('#__not_found'),
39          { timeout: 500 }
40        );
41        if (await page.$('#redirect-link')) {
42          await Promise.all([page.waitForNavigation(), page.click('#redirect-link')]);
43          console.info(`Redirected from ${link} to ${page.url()}`);
44          try {
45            await page.waitForFunction(
46              () =>
47                document.querySelector('#__redirect_failed') ||
48                document.querySelector('#__not_found'),
49              { timeout: 500 }
50            );
51            console.debug(`Redirect failed`);
52            redirectsFailed.push(link);
53          } catch {
54            console.debug(`Redirect successful`);
55          }
56        } else {
57          console.debug(`Couldn't find ${link}`);
58          notFound.push(link);
59        }
60      }
61    }
62    await browser.close();
63
64    if (notFound.length || redirectsFailed.length) {
65      if (notFound.length) {
66        console.error(`Pages not found for links: ${notFound.join(',')}`);
67      } else if (redirectsFailed.length) {
68        console.error(`Redirects failed for links: ${redirectsFailed.join(',')}`);
69      }
70      process.exit(-1);
71    }
72  } catch (e) {
73    console.error(e);
74    process.exit(-1);
75  }
76})();
77