fetch_user_agreement.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #!/usr/bin/env node
  2. import fs from 'fs/promises';
  3. const arg = process.argv[2];
  4. const agreementUrl = arg || process.env.USER_AGREEMENT_URL;
  5. const privacyUrl = process.env.USER_PRIVACY_URL || process.argv[3];
  6. async function fetchAndSave(url, outRelative) {
  7. if (!url) return false;
  8. console.log('Fetching from:', url);
  9. const res = await fetch(url);
  10. if (!res.ok) {
  11. console.error('Failed to fetch:', url, res.status, res.statusText);
  12. return false;
  13. }
  14. let html = await res.text();
  15. // Inject <base> to preserve relative asset links
  16. try {
  17. const origin = new URL(url).origin;
  18. if (!/\<base\s+/i.test(html)) {
  19. html = html.replace(/<head(.*?)>/i, `<head$1><base href="${origin}">`);
  20. }
  21. } catch (e) {
  22. // ignore
  23. }
  24. const outPath = new URL(`../public/${outRelative}`, import.meta.url);
  25. await fs.writeFile(outPath, html, 'utf8');
  26. console.log(`Saved original HTML to public/${outRelative}`);
  27. return true;
  28. }
  29. (async () => {
  30. try {
  31. let any = false;
  32. if (agreementUrl) {
  33. const ok = await fetchAndSave(agreementUrl, 'user-agreement-raw.html');
  34. any = any || ok;
  35. }
  36. if (privacyUrl) {
  37. const ok = await fetchAndSave(privacyUrl, 'privacy-policy-raw.html');
  38. any = any || ok;
  39. }
  40. if (!any) {
  41. console.log('No URL provided or all fetches failed. To fetch, set USER_AGREEMENT_URL and/or USER_PRIVACY_URL, or pass URLs as args.');
  42. process.exit(0);
  43. }
  44. } catch (err) {
  45. console.error('Error fetching or saving:', err);
  46. process.exit(2);
  47. }
  48. })();