| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- #!/usr/bin/env node
- import fs from 'fs/promises';
- const arg = process.argv[2];
- const agreementUrl = arg || process.env.USER_AGREEMENT_URL;
- const privacyUrl = process.env.USER_PRIVACY_URL || process.argv[3];
- async function fetchAndSave(url, outRelative) {
- if (!url) return false;
- console.log('Fetching from:', url);
- const res = await fetch(url);
- if (!res.ok) {
- console.error('Failed to fetch:', url, res.status, res.statusText);
- return false;
- }
- let html = await res.text();
- // Inject <base> to preserve relative asset links
- try {
- const origin = new URL(url).origin;
- if (!/\<base\s+/i.test(html)) {
- html = html.replace(/<head(.*?)>/i, `<head$1><base href="${origin}">`);
- }
- } catch (e) {
- // ignore
- }
- const outPath = new URL(`../public/${outRelative}`, import.meta.url);
- await fs.writeFile(outPath, html, 'utf8');
- console.log(`Saved original HTML to public/${outRelative}`);
- return true;
- }
- (async () => {
- try {
- let any = false;
- if (agreementUrl) {
- const ok = await fetchAndSave(agreementUrl, 'user-agreement-raw.html');
- any = any || ok;
- }
- if (privacyUrl) {
- const ok = await fetchAndSave(privacyUrl, 'privacy-policy-raw.html');
- any = any || ok;
- }
- if (!any) {
- 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.');
- process.exit(0);
- }
- } catch (err) {
- console.error('Error fetching or saving:', err);
- process.exit(2);
- }
- })();
|