sea-playwright-assets.mjs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { createHash } from "node:crypto";
  2. import { existsSync } from "node:fs";
  3. import { createRequire } from "node:module";
  4. import { mkdir, readdir, readFile, rm, stat, writeFile } from "node:fs/promises";
  5. import { dirname, join, relative, resolve, sep } from "node:path";
  6. export const seaPlaywrightAssetPrefix = "zcode-playwright-runtime/";
  7. export const seaPlaywrightManifestAssetKey = `${seaPlaywrightAssetPrefix}manifest.json`;
  8. export const collectSeaPlaywrightAssets = async ({ root, stagingDirectory, target }) => {
  9. const packageRoot = resolvePlaywrightPackageRoot(root);
  10. const packageJson = JSON.parse(await readFile(join(packageRoot, "package.json"), "utf8"));
  11. const assets = {};
  12. const files = [];
  13. await rm(stagingDirectory, { force: true, recursive: true });
  14. for await (const sourcePath of walkFiles(packageRoot)) {
  15. const relativePath = toPosixPath(relative(packageRoot, sourcePath));
  16. const assetPath = `node_modules/playwright-core/${relativePath}`;
  17. const bytes = await readFile(sourcePath);
  18. const sourceStats = await stat(sourcePath);
  19. assets[`${seaPlaywrightAssetPrefix}${assetPath}`] = sourcePath;
  20. files.push({
  21. mode: modeForFile(sourceStats.mode),
  22. path: assetPath,
  23. sha256: createHash("sha256").update(bytes).digest("hex"),
  24. });
  25. }
  26. files.sort((left, right) => left.path.localeCompare(right.path));
  27. const hash = createHash("sha256")
  28. .update(JSON.stringify(files.map(({ path, sha256 }) => [path, sha256])))
  29. .digest("hex");
  30. const manifest = {
  31. files,
  32. hash,
  33. packageVersion: packageJson.version,
  34. target,
  35. version: 1,
  36. };
  37. const manifestPath = resolve(stagingDirectory, "playwright-manifest.json");
  38. await mkdir(stagingDirectory, { recursive: true });
  39. await writeFile(manifestPath, JSON.stringify(manifest, null, 2));
  40. assets[seaPlaywrightManifestAssetKey] = manifestPath;
  41. return { assets, manifest };
  42. };
  43. const resolvePlaywrightPackageRoot = (root) => {
  44. const cliPackageJson = resolve(root, "packages", "cli", "package.json");
  45. const require = createRequire(cliPackageJson);
  46. try {
  47. return dirname(require.resolve("playwright-core/package.json"));
  48. } catch (error) {
  49. const fallback = resolve(root, "..", "..", "node_modules", "playwright-core");
  50. if (existsSync(join(fallback, "package.json"))) return fallback;
  51. throw new Error("Missing playwright-core runtime. Run `pnpm install` before `pnpm sea`.", {
  52. cause: error,
  53. });
  54. }
  55. };
  56. async function* walkFiles(directory) {
  57. const entries = await readdir(directory, { withFileTypes: true });
  58. for (const entry of entries) {
  59. if (entry.name === "node_modules") continue;
  60. const fullPath = resolve(directory, entry.name);
  61. if (entry.isDirectory()) yield* walkFiles(fullPath);
  62. else if (entry.isFile()) yield fullPath;
  63. }
  64. }
  65. const modeForFile = (sourceMode) => ((sourceMode & 0o111) !== 0 ? 0o755 : 0o644);
  66. const toPosixPath = (value) => value.split(sep).join("/");