prepare-electron-browsers.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/usr/bin/env node
  2. import fs from 'fs';
  3. import os from 'os';
  4. import path from 'path';
  5. import { fileURLToPath } from 'url';
  6. const __dirname = path.dirname(fileURLToPath(import.meta.url));
  7. const projectRoot = path.resolve(__dirname, '..');
  8. const outputDir = path.join(projectRoot, '.electron-build', 'ms-playwright');
  9. function findBrowserCache() {
  10. const envPath = process.env.PLAYWRIGHT_BROWSERS_PATH;
  11. if (envPath && envPath !== '0' && fs.existsSync(envPath)) return envPath;
  12. const candidates = [
  13. path.join(os.homedir(), 'Library', 'Caches', 'ms-playwright'),
  14. path.join(os.homedir(), '.cache', 'ms-playwright'),
  15. path.join(process.env.LOCALAPPDATA || '', 'ms-playwright'),
  16. ].filter(Boolean);
  17. return candidates.find((candidate) => fs.existsSync(candidate)) || '';
  18. }
  19. function copyDirectoryContents(sourceDir, targetDir) {
  20. const entries = fs.readdirSync(sourceDir, { withFileTypes: true });
  21. for (const entry of entries) {
  22. const sourcePath = path.join(sourceDir, entry.name);
  23. const targetPath = path.join(targetDir, entry.name);
  24. if (entry.isDirectory()) {
  25. fs.cpSync(sourcePath, targetPath, { recursive: true, force: true });
  26. } else if (entry.isFile()) {
  27. fs.copyFileSync(sourcePath, targetPath);
  28. }
  29. }
  30. }
  31. fs.rmSync(outputDir, { recursive: true, force: true });
  32. fs.mkdirSync(outputDir, { recursive: true });
  33. const browserCache = findBrowserCache();
  34. if (!browserCache) {
  35. console.log('[prepare-electron-browsers] No Playwright browser cache found. Packaging will continue without bundled browsers.');
  36. process.exit(0);
  37. }
  38. copyDirectoryContents(browserCache, outputDir);
  39. console.log(`[prepare-electron-browsers] Copied browsers from ${browserCache} -> ${outputDir}`);