download-previous-build.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import fs from 'fs';
  2. import fsp from 'fs/promises';
  3. import path from 'path';
  4. import os from 'os';
  5. import { Readable } from 'stream';
  6. import { pipeline } from 'stream/promises';
  7. import { readFileByLine } from './lib/fetch-text-by-line';
  8. import { isCI } from 'ci-info';
  9. import { task, traceAsync } from './lib/trace-runner';
  10. import { defaultRequestInit, fetchWithRetry } from './lib/fetch-retry';
  11. import tarStream from 'tar-stream';
  12. import zlib from 'zlib';
  13. const IS_READING_BUILD_OUTPUT = 1 << 2;
  14. const ALL_FILES_EXISTS = 1 << 3;
  15. export const downloadPreviousBuild = task(import.meta.path, async () => {
  16. const buildOutputList: string[] = [];
  17. let flag = 1 | ALL_FILES_EXISTS;
  18. for await (const line of readFileByLine(path.resolve(import.meta.dir, '../.gitignore'))) {
  19. if (line === '# $ build output') {
  20. flag = flag | IS_READING_BUILD_OUTPUT;
  21. continue;
  22. }
  23. if (!(flag & IS_READING_BUILD_OUTPUT)) {
  24. continue;
  25. }
  26. buildOutputList.push(line);
  27. if (!isCI) {
  28. // Bun.file().exists() doesn't check directory
  29. if (!fs.existsSync(path.join(import.meta.dir, '..', line))) {
  30. flag = flag & ~ALL_FILES_EXISTS;
  31. }
  32. }
  33. }
  34. if (isCI) {
  35. flag = flag & ~ALL_FILES_EXISTS;
  36. }
  37. if (flag & ALL_FILES_EXISTS) {
  38. console.log('All files exists, skip download.');
  39. return;
  40. }
  41. const filesList = buildOutputList.map(f => path.join('ruleset.skk.moe-master', f));
  42. await traceAsync(
  43. 'Download and extract previous build',
  44. async () => {
  45. const resp = await fetchWithRetry('https://codeload.github.com/sukkalab/ruleset.skk.moe/tar.gz/master', defaultRequestInit);
  46. if (!resp.body) {
  47. throw new Error('Download previous build failed! No body found');
  48. }
  49. const extract = tarStream.extract();
  50. Readable.fromWeb(resp.body).pipe(zlib.createGunzip()).pipe(extract);
  51. for await (const entry of extract) {
  52. if (entry.header.type !== 'file') {
  53. entry.resume(); // Drain the entry
  54. continue;
  55. }
  56. // filter entry
  57. if (!filesList.some(f => entry.header.name.startsWith(f))) {
  58. entry.resume(); // Drain the entry
  59. continue;
  60. }
  61. const relativeEntryPath = entry.header.name.replace(`ruleset.skk.moe-master${path.sep}`, '');
  62. const targetPath = path.join(import.meta.dir, '..', relativeEntryPath);
  63. await fsp.mkdir(path.dirname(targetPath), { recursive: true });
  64. await pipeline(
  65. entry,
  66. fs.createWriteStream(targetPath)
  67. );
  68. }
  69. }
  70. );
  71. });
  72. export const downloadPublicSuffixList = task(import.meta.path, async () => {
  73. const publicSuffixPath = path.resolve(import.meta.dir, '../node_modules/.cache/public_suffix_list_dat.txt');
  74. const resp = await fetchWithRetry('https://publicsuffix.org/list/public_suffix_list.dat', defaultRequestInit);
  75. return Bun.write(publicSuffixPath, resp as Response);
  76. }, 'download-publicsuffixlist');
  77. if (import.meta.main) {
  78. Promise.all([
  79. downloadPreviousBuild(),
  80. downloadPublicSuffixList()
  81. ]);
  82. }