53 lines
1.3 KiB
JavaScript
53 lines
1.3 KiB
JavaScript
import { readdir, readFile, stat } from 'node:fs/promises';
|
|
import path from 'node:path';
|
|
|
|
const dist = path.resolve('dist');
|
|
const failures = [];
|
|
|
|
async function walk(dir) {
|
|
const entries = await readdir(dir, { withFileTypes: true });
|
|
const files = [];
|
|
|
|
for (const entry of entries) {
|
|
const fullPath = path.join(dir, entry.name);
|
|
if (entry.isDirectory()) {
|
|
files.push(...(await walk(fullPath)));
|
|
} else {
|
|
files.push(fullPath);
|
|
}
|
|
}
|
|
|
|
return files;
|
|
}
|
|
|
|
try {
|
|
await stat(dist);
|
|
} catch {
|
|
throw new Error('dist/ does not exist. Run npm run build first.');
|
|
}
|
|
|
|
const files = await walk(dist);
|
|
const jsFiles = files.filter((file) => file.endsWith('.js'));
|
|
|
|
if (jsFiles.length > 0) {
|
|
failures.push(
|
|
`Unexpected JavaScript assets:\n${jsFiles.map((file) => `- ${file}`).join('\n')}`
|
|
);
|
|
}
|
|
|
|
for (const file of files.filter((candidate) => candidate.endsWith('.html'))) {
|
|
const html = await readFile(file, 'utf8');
|
|
const scripts = (
|
|
html.match(/<script\b(?![^>]*type=["']application\/ld\+json["'])[^>]*>/gi) ?? []
|
|
).filter((script) => !script.includes('data-theme-script'));
|
|
if (scripts?.length) {
|
|
failures.push(`Unexpected script tag in ${file}:\n${scripts.join('\n')}`);
|
|
}
|
|
}
|
|
|
|
if (failures.length > 0) {
|
|
console.error(failures.join('\n\n'));
|
|
process.exit(1);
|
|
}
|
|
|
|
console.log('No unexpected JavaScript found in dist/.');
|