Skip to content
Merged
Changes from all 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
14 changes: 13 additions & 1 deletion scripts/check-deleted-pages.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Default base branch: dev
*/
import { execSync } from 'child_process';
import { readFileSync } from 'fs';
import { existsSync, readFileSync } from 'fs';

const baseBranch = process.argv[2] || 'dev';

Expand Down Expand Up @@ -48,6 +48,14 @@ function fileToPath(file) {
.replace(/\/index$/, '') || '/';
}

// A deleted file needs no redirect when its route is still served by another
// page file (e.g. deleting foo.mdx while foo/index.mdx exists)
function routeStillServed(urlPath) {
const base = `src/pages${urlPath === '/' ? '' : urlPath}`;
return ['.mdx', '.astro', '.md', '/index.mdx', '/index.astro', '/index.md']
.some(suffix => existsSync(`${base}${suffix}`));
}

// Load redirects map
const redirectsRaw = readFileSync('src/lib/redirects.ts', 'utf-8');
const redirectEntries = [...redirectsRaw.matchAll(/["']([^"']+)["']:\s*["']([^"']+)["']/g)];
Expand All @@ -57,6 +65,10 @@ const redirectMap = new Set(redirectEntries.map(([, from]) => from));
const missing = [];
for (const file of deletedPages) {
const urlPath = fileToPath(file);
if (routeStillServed(urlPath)) {
console.log(` ${urlPath} still served by another page file, no redirect needed. 鉁揱);
continue;
}
if (!redirectMap.has(urlPath)) {
missing.push({ file, urlPath });
}
Expand Down
Loading