import { execFileSync } from "node:child_process"; import { mkdtemp, readFile, rm } from "node:fs/promises"; import { createRequire } from "node:module"; import { tmpdir } from "node:os"; import path from "node:path"; import { pathToFileURL } from "node:url"; const vendor = import.meta.dirname; const source = await mkdtemp(path.join(tmpdir(), "avril-webstreams-build-")); const commit = "12b4fedec6c01503a137d837204ca0b85992461e"; const run = (command, args) => execFileSync(command, args, { cwd: source, stdio: "inherit" }); const previousDirectory = process.cwd(); try { run("git", ["init", "-q"]); run("git", [ "fetch", "--depth=1", "https://github.com/MattiasBuelens/web-streams-polyfill.git", commit, ]); run("git", ["checkout", "--detach", "-q", "FETCH_HEAD"]); run("git", ["apply", path.join(vendor, "avril.patch")]); run("npm", [ "ci", "--include=dev", "--ignore-scripts", "--no-audit", "--no-fund", ]); process.chdir(source); const { default: configs } = await import( pathToFileURL(path.join(source, "rollup.config.mjs")) ); const config = configs.find((config) => config.input === "src/polyfill.ts" && config.output[0].file === "dist/polyfill.js" ); if (!config) throw new Error("Upstream polyfill build configuration changed"); const original = await readFile(path.join(vendor, "polyfill.js"), "utf8"); const banner = original.match(/^\/\*\*[\s\S]*?\*\//)?.[0]; if (!banner) throw new Error("Missing upstream license banner"); const { rollup } = createRequire(path.join(source, "package.json"))("rollup"); const { output, ...input } = config; const bundle = await rollup(input); try { await bundle.write({ ...output[0], banner, file: path.join(vendor, "polyfill.avril.js"), }); } finally { await bundle.close(); } } finally { process.chdir(previousDirectory); await rm(source, { recursive: true, force: true }); }