download-mock-assets.ts 1.2 KB

123456789101112131415161718192021222324252627
  1. import picocolors from 'picocolors';
  2. import { task } from './lib/trace-runner';
  3. import path from 'path';
  4. import { fetchWithRetry } from './lib/fetch-retry';
  5. const ASSETS_LIST = {
  6. 'www-google-analytics-com_ga.js': 'https://unpkg.com/@adguard/scriptlets@1/dist/redirect-files/google-analytics-ga.js',
  7. 'www-googletagservices-com_gpt.js': 'https://unpkg.com/@adguard/scriptlets@1/dist/redirect-files/googletagservices-gpt.js',
  8. 'www-google-analytics-com_analytics.js': 'https://unpkg.com/@adguard/scriptlets@1/dist/redirect-files/google-analytics.js',
  9. 'www-googlesyndication-com_adsbygoogle.js': 'https://unpkg.com/@adguard/scriptlets@1/dist/redirect-files/googlesyndication-adsbygoogle.js'
  10. } as const;
  11. const mockDir = path.resolve(import.meta.dir, '../Mock');
  12. export const downloadMockAssets = task(import.meta.path, () => Promise.all(Object.entries(ASSETS_LIST).map(async ([filename, url]) => {
  13. const targetPath = path.join(mockDir, filename);
  14. const key = picocolors.gray(`Download ${filename}`);
  15. console.time(key);
  16. const res = await fetchWithRetry(url);
  17. await Bun.write(targetPath, res);
  18. console.timeEnd(key);
  19. })));
  20. if (import.meta.main) {
  21. downloadMockAssets();
  22. }