download-previous-build.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import path from 'node:path';
  2. import fs from 'node:fs';
  3. import { pipeline } from 'node:stream/promises';
  4. import picocolors from 'picocolors';
  5. import { task } from './trace';
  6. import { extract as tarExtract } from 'tar-fs';
  7. import type { Headers as TarEntryHeaders } from 'tar-fs';
  8. import zlib from 'node:zlib';
  9. import { fetchWithRetry } from './lib/fetch-retry';
  10. import { Readable } from 'node:stream';
  11. const GITHUB_CODELOAD_URL = 'https://codeload.github.com/sukkalab/ruleset.skk.moe/tar.gz/master';
  12. const GITLAB_CODELOAD_URL = 'https://gitlab.com/SukkaW/ruleset.skk.moe/-/archive/master/ruleset.skk.moe-master.tar.gz';
  13. export const downloadPreviousBuild = task(require.main === module, __filename)(async (span) => {
  14. const publicDir = path.resolve(__dirname, '..', 'public');
  15. if (fs.existsSync(publicDir)) {
  16. console.log(picocolors.blue('Public directory exists, skip downloading previous build'));
  17. return;
  18. }
  19. const tarGzUrl = await span.traceChildAsync('get tar.gz url', async () => {
  20. const resp = await fetchWithRetry(GITHUB_CODELOAD_URL, { method: 'HEAD' });
  21. if (resp.status !== 200) {
  22. console.warn('Download previous build from GitHub failed! Status:', resp.status);
  23. console.warn('Switch to GitLab');
  24. return GITLAB_CODELOAD_URL;
  25. }
  26. return GITHUB_CODELOAD_URL;
  27. });
  28. return span.traceChildAsync('download & extract previoud build', async () => {
  29. const resp = await fetchWithRetry(tarGzUrl, {
  30. headers: {
  31. 'User-Agent': 'curl/8.9.1',
  32. // https://github.com/unjs/giget/issues/97
  33. // https://gitlab.com/gitlab-org/gitlab/-/commit/50c11f278d18fe1f3fb12eb595067216bb58ade2
  34. 'sec-fetch-mode': 'same-origin'
  35. },
  36. mode: 'same-origin'
  37. });
  38. if (resp.status !== 200) {
  39. console.warn('Download previous build failed! Status:', resp.status);
  40. if (resp.status === 404) {
  41. return;
  42. }
  43. }
  44. if (!resp.body) {
  45. throw new Error('Download previous build failed! No body found');
  46. }
  47. const pathPrefix = 'ruleset.skk.moe-master/';
  48. const gunzip = zlib.createGunzip();
  49. const extract = tarExtract(
  50. publicDir,
  51. {
  52. ignore: tarOnIgnore,
  53. map(header) {
  54. header.name = header.name.replace(pathPrefix, '');
  55. return header;
  56. }
  57. }
  58. );
  59. return pipeline(
  60. Readable.fromWeb(resp.body),
  61. gunzip,
  62. extract
  63. );
  64. });
  65. });
  66. function tarOnIgnore(_: string, header?: TarEntryHeaders) {
  67. if (header) {
  68. if (header.type !== 'file' && header.type !== 'directory') {
  69. return true;
  70. }
  71. const extname = path.extname(header.name);
  72. if (extname === '.ts') {
  73. return true;
  74. }
  75. }
  76. return false;
  77. }