download-previous-build.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import path from 'node:path';
  2. import fs from 'node:fs';
  3. import { pipeline } from 'node:stream/promises';
  4. import { task } from './trace';
  5. import { defaultRequestInit, fetchWithRetry } from './lib/fetch-retry';
  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 { Readable } from 'node:stream';
  10. import picocolors from 'picocolors';
  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, {
  21. ...defaultRequestInit,
  22. method: 'HEAD',
  23. retry: {
  24. retryOnNon2xx: false
  25. }
  26. });
  27. if (resp.status !== 200) {
  28. console.warn('Download previous build from GitHub failed! Status:', resp.status);
  29. console.warn('Switch to GitLab');
  30. return GITLAB_CODELOAD_URL;
  31. }
  32. return GITHUB_CODELOAD_URL;
  33. });
  34. return span.traceChildAsync('download & extract previoud build', async () => {
  35. const resp = await fetchWithRetry(tarGzUrl, {
  36. headers: {
  37. 'User-Agent': 'curl/8.9.1',
  38. // https://github.com/unjs/giget/issues/97
  39. // https://gitlab.com/gitlab-org/gitlab/-/commit/50c11f278d18fe1f3fb12eb595067216bb58ade2
  40. 'sec-fetch-mode': 'same-origin'
  41. },
  42. // https://github.com/unjs/giget/issues/97
  43. // https://gitlab.com/gitlab-org/gitlab/-/commit/50c11f278d18fe1f3fb12eb595067216bb58ade2
  44. mode: 'same-origin',
  45. retry: {
  46. retryOnNon2xx: false
  47. }
  48. });
  49. if (resp.status !== 200) {
  50. console.warn('Download previous build failed! Status:', resp.status);
  51. if (resp.status === 404) {
  52. return;
  53. }
  54. }
  55. if (!resp.body) {
  56. throw new Error('Download previous build failed! No body found');
  57. }
  58. const pathPrefix = 'ruleset.skk.moe-master/';
  59. const gunzip = zlib.createGunzip();
  60. const extract = tarExtract(
  61. publicDir,
  62. {
  63. ignore: tarOnIgnore,
  64. map(header) {
  65. header.name = header.name.replace(pathPrefix, '');
  66. return header;
  67. }
  68. }
  69. );
  70. return pipeline(
  71. Readable.fromWeb(resp.body),
  72. gunzip,
  73. extract
  74. );
  75. });
  76. });
  77. function tarOnIgnore(_: string, header?: TarEntryHeaders) {
  78. if (header) {
  79. if (header.type !== 'file' && header.type !== 'directory') {
  80. return true;
  81. }
  82. const extname = path.extname(header.name);
  83. if (extname === '.ts') {
  84. return true;
  85. }
  86. }
  87. return false;
  88. }