download-previous-build.ts 2.8 KB

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