download-previous-build.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { existsSync, createWriteStream } from 'node:fs';
  2. import { mkdir } from 'node:fs/promises';
  3. import path from 'node:path';
  4. import { pipeline } from 'node: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 'node:zlib';
  11. import { Readable } from 'node:stream';
  12. const IS_READING_BUILD_OUTPUT = 1 << 2;
  13. const ALL_FILES_EXISTS = 1 << 3;
  14. export const downloadPreviousBuild = task(require.main === module, __filename)(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(__dirname, '../.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 && !existsSync(path.join(__dirname, '..', 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. return span
  43. .traceChild('download & extract previoud build')
  44. .traceAsyncFn(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 gunzip = zlib.createGunzip();
  50. const extract = tarStream.extract();
  51. pipeline(
  52. Readable.fromWeb(resp.body),
  53. gunzip,
  54. extract
  55. );
  56. const pathPrefix = `ruleset.skk.moe-master${path.sep}`;
  57. for await (const entry of extract) {
  58. if (entry.header.type !== 'file') {
  59. entry.resume(); // Drain the entry
  60. continue;
  61. }
  62. // filter entry
  63. if (!filesList.some(f => entry.header.name.startsWith(f))) {
  64. entry.resume(); // Drain the entry
  65. continue;
  66. }
  67. const relativeEntryPath = entry.header.name.replace(pathPrefix, '');
  68. const targetPath = path.join(__dirname, '..', relativeEntryPath);
  69. await mkdir(path.dirname(targetPath), { recursive: true });
  70. await pipeline(entry, createWriteStream(targetPath));
  71. }
  72. });
  73. });