forked from lunanting/cangku
64 lines
2.1 KiB
JavaScript
64 lines
2.1 KiB
JavaScript
import { readFile } from 'node:fs/promises';
|
|
import path from 'node:path';
|
|
import process from 'node:process';
|
|
import { pathToFileURL } from 'node:url';
|
|
import { extractEntryAssetPath } from './verify-public-build.shared.mjs';
|
|
|
|
export async function verifyPlanningEntry(distDir = path.resolve(process.cwd(), 'dist')) {
|
|
const indexHtmlPath = path.join(distDir, 'index.html');
|
|
const indexHtml = await readFile(indexHtmlPath, 'utf8');
|
|
const entryAssetPath = extractEntryAssetPath(indexHtml);
|
|
|
|
if (!entryAssetPath) {
|
|
throw new Error(`No module entry asset found in ${indexHtmlPath}`);
|
|
}
|
|
|
|
const entryChunkPath = path.join(distDir, entryAssetPath.replace(/^\//, ''));
|
|
const entryChunk = await readFile(entryChunkPath, 'utf8');
|
|
|
|
const planningRouteIndex = entryChunk.indexOf('/planning');
|
|
if (planningRouteIndex === -1) {
|
|
throw new Error(`The entry chunk ${path.basename(entryChunkPath)} does not contain a /planning route.`);
|
|
}
|
|
|
|
const planningWindow = entryChunk.slice(
|
|
Math.max(0, planningRouteIndex - 600),
|
|
Math.min(entryChunk.length, planningRouteIndex + 1400),
|
|
);
|
|
|
|
const planningRouteRedirectPattern =
|
|
/path:\s*[`'"]\/planning[`'"]\s*,\s*element:\s*[^{}]{0,220}?to:\s*[`'"]\/planning\//;
|
|
|
|
if (planningRouteRedirectPattern.test(planningWindow)) {
|
|
throw new Error(
|
|
`The built entry chunk ${path.basename(entryChunkPath)} still redirects /planning into a child page.`,
|
|
);
|
|
}
|
|
|
|
return {
|
|
distDir,
|
|
entryAssetPath,
|
|
entryChunkPath,
|
|
};
|
|
}
|
|
|
|
async function main() {
|
|
const distArg = process.argv[2];
|
|
const distDir = distArg ? path.resolve(process.cwd(), distArg) : path.resolve(process.cwd(), 'dist');
|
|
const result = await verifyPlanningEntry(distDir);
|
|
process.stdout.write(
|
|
`Verified planning entry in ${result.entryAssetPath} (${path.relative(process.cwd(), result.distDir)})\n`,
|
|
);
|
|
}
|
|
|
|
const isDirectExecution = process.argv[1]
|
|
? pathToFileURL(path.resolve(process.argv[1])).href === import.meta.url
|
|
: false;
|
|
|
|
if (isDirectExecution) {
|
|
main().catch((error) => {
|
|
process.stderr.write(`${error.message}\n`);
|
|
process.exitCode = 1;
|
|
});
|
|
}
|