Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add script for checking lit.dev redirects #468

Merged
merged 3 commits into from
Sep 1, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ jobs:
- name: Build
run: npm run build

- name: Check for broken redirects
run: npm run test:links:redirects

- name: Check for broken links (internal)
run: npm run test:links:internal

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"format": "prettier \"**/*.{ts,js,json,html,css,md}\" --write",
"nuke": "rm -rf node_modules package-lock.json packages/*/node_modules packages/*/package-lock.json && npm install && npx lerna bootstrap",
"test": "npm run test:links",
"test:links": "npm run test:links:internal && npm run test:links:external",
"test:links": "npm run test:links:redirects && npm run test:links:internal && npm run test:links:external",
"test:links:redirects": "node packages/lit-dev-tools-esm/lib/check-redirects.js",
"test:links:internal": "run-p -r start check-links:internal",
"test:links:external": "run-p -r start check-links:external",
"check-links:internal": "wait-on tcp:8080 && blc http://localhost:8080 --recursive --exclude-external --ordered",
Expand Down
142 changes: 142 additions & 0 deletions packages/lit-dev-tools-esm/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions packages/lit-dev-tools-esm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "lit-dev-tools-esm",
"private": true,
"version": "0.0.0",
"description": "Misc tools for lit.dev (ES Modules)",
"author": "Google LLC",
"license": "BSD-3-Clause",
"type": "module",
"scripts": {
"build": "npm run build:ts",
"build:ts": "../../node_modules/.bin/tsc",
"format": "../../node_modules/.bin/prettier \"**/*.{ts,js,json,html,css,md}\" --write"
},
"dependencies": {
"@types/ansi-escape-sequences": "^4.0.0",
"ansi-escape-sequences": "^6.2.0",
"lit-dev-server": "^0.0.0",
"node-fetch": "^3.0.0"
}
}
124 changes: 124 additions & 0 deletions packages/lit-dev-tools-esm/src/check-redirects.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/**
* @license
* Copyright 2021 Google LLC
* SPDX-License-Identifier: BSD-3-Clause
*/

import * as pathLib from 'path';
import * as fs from 'fs/promises';
import ansi from 'ansi-escape-sequences';
import fetch from 'node-fetch';
import {pageRedirects} from 'lit-dev-server/redirects.js';
import {fileURLToPath} from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = pathLib.dirname(__filename);

const {red, green, yellow, bold, reset} = ansi.style;

const OK = Symbol();
type ErrorMessage = string;

const isUrl = (str: string) => {
try {
new URL(str);
return true;
} catch {
return false;
}
};

const trimTrailingSlash = (str: string) =>
str.endsWith('/') ? str.slice(0, str.length - 1) : str;

const siteOutputDir = pathLib.resolve(
__dirname,
'../',
'../',
'lit-dev-content',
'_site'
AndrewJakubowicz marked this conversation as resolved.
Show resolved Hide resolved
);

const checkRedirect = async (
redirect: string
): Promise<ErrorMessage | typeof OK> => {
if (isUrl(redirect)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non action thinking out loud: I initially thought "a relative url is a url" so had some confusion, since we are differentiating between absolute URLs and relative URLs with this check.
Comment on L46 helped.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, that is a little confusing. Renamed to isAbsoluteUrl.

// Remote URLs.
let res;
try {
res = await fetch(redirect);
} catch (e) {
return `Fetch error: ${(e as Error).message}`;
}
if (res.status !== 200) {
return `HTTP ${res.status} error`;
}
} else {
// Local paths. A bit hacky, but since we know how Eleventy works, we don't
// need to actually run the server, we can just look directly in the built
// HTML output directory.
const {pathname, hash} = new URL(redirect, 'http://lit.dev');
const diskPath = pathLib.relative(
process.cwd(),
pathLib.join(siteOutputDir, trimTrailingSlash(pathname), 'index.html')
);
let data;
try {
data = await fs.readFile(diskPath, {encoding: 'utf8'});
} catch {
return `Could not find file matching path ${pathname}
Searched for file ${diskPath}`;
}
if (hash) {
// Another hack. Just do a regexp search for e.g. id="somesection" instead
// of DOM parsing. Should be good enough, especially given how regular our
// Markdown generated HTML is.
const idAttrRegExp = new RegExp(`\\sid=["']?${hash.slice(1)}["']?[\\s>]`);
if (data.match(idAttrRegExp) === null) {
return `Could not find section matching hash ${hash}.
Searched in file ${diskPath}`;
}
}
}
return OK;
};

const checkAllRedirects = async () => {
console.log('==========================');
console.log('Checking lit.dev redirects');
console.log('==========================');
console.log();

let fail = false;
const promises = [];
for (const [from, to] of pageRedirects) {
promises.push(
(async () => {
const result = await checkRedirect(to);
if (result === OK) {
console.log(`${bold + green}OK${reset} ${from} -> ${to}`);
} else {
console.log();
console.log(
`${bold + red}BROKEN REDIRECT${reset} ${from} -> ${
yellow + to + reset
}`
);
console.log(result);
console.log();
fail = true;
}
})()
);
}
await Promise.all(promises);
console.log();
if (fail) {
console.log('Redirects were broken!');
process.exit(1);
} else {
console.error('All redirects OK!');
}
};

checkAllRedirects();
22 changes: 22 additions & 0 deletions packages/lit-dev-tools-esm/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"compilerOptions": {
"target": "es2020",
"module": "esnext",
"moduleResolution": "node",
"declaration": true,
"declarationMap": true,
"sourceMap": true,
"outDir": "./lib",
"rootDir": "./src",
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"esModuleInterop": true,
"experimentalDecorators": true,
"forceConsistentCasingInFileNames": true
},
"include": ["src/**/*.ts"],
"exclude": []
}
6 changes: 3 additions & 3 deletions packages/lit-dev-tools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "lit-dev-tools",
"private": true,
"version": "0.0.0",
"description": "Misc tools for lit.dev",
"description": "Misc tools for lit.dev (CommonJS)",
"author": "Google LLC",
"license": "BSD-3-Clause",
"scripts": {
Expand All @@ -12,8 +12,10 @@
"test": "npm run build && uvu ./lib \".spec.js$\""
},
"dependencies": {
"@types/jsdom": "^16.2.13",
"@types/markdown-it": "^12.0.1",
"@types/source-map": "^0.5.7",
"@types/strip-comments": "^2.0.1",
"@web/dev-server": "^0.1.6",
"@web/dev-server-core": "^0.3.5",
"fast-glob": "^3.2.5",
Expand All @@ -27,8 +29,6 @@
"strip-comments": "^2.0.1",
"striptags": "^3.2.0",
"typedoc": "^0.20.30",
"@types/jsdom": "^16.2.13",
"@types/strip-comments": "^2.0.1",
"uvu": "^0.5.1"
}
}