import { lstat, readFile, readdir } from "node:fs/promises"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { verifyPcre2Pack } from "./pcre2-engine-pack.mjs"; const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), ".."); const arguments_ = process.argv.slice(2); if (arguments_.length > 0) { if (arguments_.length !== 2 || arguments_[0] !== "--pcre2-pack") { throw new Error( "Usage: node scripts/verify-engine-assets.mjs [--pcre2-pack PATH]", ); } const pack = path.resolve(root, arguments_[1]); const metadata = await verifyPcre2Pack(pack, root); console.log( `Verified staged PCRE2 ${metadata.engineVersion} pack and WebAssembly bridge smoke test.`, ); process.exit(0); } const engineDirectory = path.join(root, "dist", "engines"); const details = await lstat(engineDirectory); if (details.isSymbolicLink() || !details.isDirectory()) { throw new Error("dist/engines must be a real directory."); } const entries = (await readdir(engineDirectory, { withFileTypes: true })).sort( (left, right) => left.name < right.name ? -1 : left.name > right.name ? 1 : 0, ); if ( entries.length !== 2 || !entries[0]?.isFile() || entries[0].name !== "README.md" || !entries[1]?.isDirectory() || entries[1].name !== "pcre2" ) { throw new Error( "The production build must contain only README.md and the declared PCRE2 pack.", ); } const notice = await readFile(path.join(engineDirectory, "README.md"), "utf8"); if (!notice.includes("native `RegExp`") || !notice.includes("PCRE2 10.47")) { throw new Error("The engine asset notice does not describe this release."); } await verifyPcre2Pack(path.join(engineDirectory, "pcre2"), root); console.log("Verified native ECMAScript and bundled PCRE2 engine assets.");