Newer
Older
bugtrail / packages / extension / scripts / build-manifest.mjs
/**
 * Emits manifest.json into the build output for the given target.
 * Usage: node scripts/build-manifest.mjs chrome|firefox
 *        (or import { writeManifest } from another script)
 */
import { readFileSync, writeFileSync, existsSync } from "node:fs";
import { join, dirname } from "node:path";
import { fileURLToPath } from "node:url";

const root = dirname(fileURLToPath(import.meta.url));

export function writeManifest(target) {
  const dist = join(root, "..", "dist", target);

  const template = JSON.parse(readFileSync(join(root, "..", "manifest.template.json"), "utf8"));

  const manifest = structuredClone(template);
  manifest.version = "0.1.0";

  if (target === "chrome") {
    // bundles are IIFE (self-contained), so the worker stays classic
    manifest.background = {
      service_worker: "background.js",
    };
  } else if (target === "firefox") {
    manifest.background = {
      scripts: ["background.js"],
    };
    manifest.browser_specific_settings = {
      gecko: {
        id: "live-testing-tool@gnexus.space",
        strict_min_version: "128.0",
      },
    };
  }

  // sanity check: referenced files must exist in dist
  for (const file of ["background.js", "content.js"]) {
    if (!existsSync(join(dist, file))) {
      console.error(`build-manifest: missing ${file} in ${dist} — run vite build first`);
      process.exit(1);
    }
  }

  writeFileSync(join(dist, "manifest.json"), JSON.stringify(manifest, null, 2));
  console.log(`manifest.json written for ${target} -> ${dist}`);
}

if (import.meta.url === `file://${process.argv[1]}`) {
  writeManifest(process.argv[2] ?? "chrome");
}