Files
portfolio-photo/scripts/check-seo.mjs
T

138 lines
4.2 KiB
JavaScript

import assert from 'node:assert/strict';
import { execFileSync } from 'node:child_process';
import { readFileSync, readdirSync, writeFileSync } from 'node:fs';
import path from 'node:path';
import vm from 'node:vm';
import { fileURLToPath } from 'node:url';
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
const PUBLIC = path.join(ROOT, 'public');
const requested = new Set(process.argv.slice(2));
const wants = (suite) => requested.size === 0 || requested.has('all') || requested.has(suite);
const failures = [];
function read(relativePath) {
return readFileSync(path.join(ROOT, relativePath), 'utf8');
}
function loadConfig() {
const context = { window: {} };
vm.createContext(context);
vm.runInContext(read('public/custom/seo-config.js'), context);
return context.window.SEO_CONFIG;
}
function count(text, pattern) {
return (text.match(pattern) || []).length;
}
function htmlEscape(value) {
return String(value)
.replace(/&/g, '&')
.replace(/"/g, '"')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;');
}
function check(name, fn) {
try {
fn();
console.log(`PASS ${name}`);
} catch (error) {
failures.push(`${name}: ${error.message}`);
console.error(`FAIL ${name}: ${error.message}`);
}
}
function bakeSeo() {
return execFileSync(process.execPath, ['scripts/bake-seo.mjs'], {
cwd: ROOT,
encoding: 'utf8',
stdio: 'pipe'
});
}
function snapshotTopLevelHtml() {
return readdirSync(PUBLIC)
.filter((name) => name.endsWith('.html'))
.sort()
.map((name) => [name, readFileSync(path.join(PUBLIC, name), 'utf8')]);
}
if (wants('core')) {
const config = loadConfig();
const cities = ['Hamburg', 'Hannover', 'Wolfsburg', 'Uelzen', 'Celle', 'Lüneburg'];
check('Geschäftsort ist Ebstorf', () => {
assert.equal(config.BUSINESS.addressLocality, 'Ebstorf');
});
check('alle sechs Sportregionen sind zentral gepflegt', () => {
assert.deepEqual(Array.from(config.BUSINESS.areaServed), cities);
});
check('Sportseite hat seitenspezifische Service-Daten', () => {
const page = config.PAGES['/sportfotografie'];
assert.equal(page.service.serviceType, 'Sportfotografie');
assert.deepEqual(
Array.from(page.service.areaServed, (place) => place.name),
cities
);
assert.ok(page.jsonld.includes('service'));
});
check('Eventseite hat Hamburg und Deutschland als Einsatzgebiet', () => {
const page = config.PAGES['/eventfotografie'];
assert.deepEqual(
JSON.parse(JSON.stringify(page.service.areaServed)),
[
{ type: 'City', name: 'Hamburg' },
{ type: 'Country', name: 'Deutschland' }
]
);
assert.ok(page.jsonld.includes('service'));
});
check('gebackener Event-Head entspricht der Konfiguration', () => {
const page = config.PAGES['/eventfotografie'];
const html = read('public/eventfotografie.html');
assert.equal(count(html, /<title>/g), 1);
assert.equal(count(html, /<meta name="description"/g), 1);
assert.ok(html.includes(`<title>${htmlEscape(page.title)}</title>`));
assert.ok(html.includes(`content="${htmlEscape(page.desc)}"`));
assert.ok(html.includes('"serviceType":"Eventfotografie"'));
assert.ok(html.includes('"@type":"Country","name":"Deutschland"'));
});
check('erneutes SEO-Backen verändert keine Top-Level-HTML-Seite', () => {
const before = snapshotTopLevelHtml();
bakeSeo();
assert.deepEqual(snapshotTopLevelHtml(), before);
});
check('SEO-Bake meldet übersprungene HTML-Seiten als Fehler', () => {
const file = path.join(PUBLIC, 'blog.html');
const original = readFileSync(file, 'utf8');
let failure;
try {
writeFileSync(file, original.replace('</head>', ''));
try {
bakeSeo();
} catch (error) {
failure = error;
}
} finally {
writeFileSync(file, original);
}
assert.ok(failure, 'das Backen muss bei übersprungenen HTML-Seiten fehlschlagen');
assert.match(String(failure.stderr), /kein <\/head> in blog\.html/);
});
}
if (failures.length) {
console.error(`\n${failures.length} Prüfung(en) fehlgeschlagen.`);
process.exit(1);
}
console.log('\nAlle angeforderten SEO-Prüfungen sind grün.');