download-previous-build.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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', {
  46. ...defaultRequestInit,
  47. retry: {
  48. retryOnNon2xx: false
  49. }
  50. });
  51. if (resp.status !== 200) {
  52. console.warn('Download previous build failed! Status:', resp.status);
  53. if (resp.status === 404) {
  54. return;
  55. }
  56. }
  57. if (!resp.body) {
  58. throw new Error('Download previous build failed! No body found');
  59. }
  60. const gunzip = zlib.createGunzip();
  61. const extract = tarStream.extract();
  62. pipeline(
  63. Readable.fromWeb(resp.body),
  64. gunzip,
  65. extract
  66. );
  67. const pathPrefix = `ruleset.skk.moe-master${path.sep}`;
  68. for await (const entry of extract) {
  69. if (entry.header.type !== 'file') {
  70. entry.resume(); // Drain the entry
  71. continue;
  72. }
  73. // filter entry
  74. if (!filesList.some(f => entry.header.name.startsWith(f))) {
  75. entry.resume(); // Drain the entry
  76. continue;
  77. }
  78. const relativeEntryPath = entry.header.name.replace(pathPrefix, '');
  79. const targetPath = path.join(__dirname, '..', relativeEntryPath);
  80. await mkdir(path.dirname(targetPath), { recursive: true });
  81. await pipeline(entry, createWriteStream(targetPath));
  82. }
  83. });
  84. });