download-previous-build.ts 2.8 KB

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