-
Notifications
You must be signed in to change notification settings - Fork 202
Add script for checking lit.dev redirects #468
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 isAbsoluteUrl = (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' | ||
); | ||
|
||
const checkRedirect = async ( | ||
redirect: string | ||
): Promise<ErrorMessage | typeof OK> => { | ||
if (isAbsoluteUrl(redirect)) { | ||
// 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(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": [] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.