download-previous-build.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 { extract as tarExtract } from 'tar-fs';
  6. import type { Headers as TarEntryHeaders } from 'tar-fs';
  7. import zlib from 'node:zlib';
  8. import undici from 'undici';
  9. import picocolors from 'picocolors';
  10. import { PUBLIC_DIR } from './constants/dir';
  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. if (fs.existsSync(PUBLIC_DIR)) {
  15. console.log(picocolors.blue('Public directory exists, skip downloading previous build'));
  16. return;
  17. }
  18. const tarGzUrl = await span.traceChildAsync('get tar.gz url', async () => {
  19. const resp = await undici.request(GITHUB_CODELOAD_URL, { method: 'HEAD' });
  20. if (resp.statusCode !== 200) {
  21. console.warn('Download previous build from GitHub failed! Status:', resp.statusCode);
  22. console.warn('Switch to GitLab');
  23. return GITLAB_CODELOAD_URL;
  24. }
  25. return GITHUB_CODELOAD_URL;
  26. });
  27. return span.traceChildAsync('download & extract previoud build', async () => {
  28. const respBody = undici.pipeline(
  29. tarGzUrl,
  30. {
  31. method: 'GET',
  32. headers: {
  33. 'User-Agent': 'curl/8.9.1',
  34. // https://github.com/unjs/giget/issues/97
  35. // https://gitlab.com/gitlab-org/gitlab/-/commit/50c11f278d18fe1f3fb12eb595067216bb58ade2
  36. 'sec-fetch-mode': 'same-origin'
  37. },
  38. // Allow redirects by default
  39. maxRedirections: 5
  40. },
  41. ({ statusCode, body }) => {
  42. if (statusCode !== 200) {
  43. console.warn('Download previous build failed! Status:', statusCode);
  44. if (statusCode === 404) {
  45. throw new Error('Download previous build failed! 404');
  46. }
  47. }
  48. return body;
  49. }
  50. // by default, undici.pipeline returns a duplex stream (for POST/PUT)
  51. // Since we are using GET, we need to end the write immediately
  52. ).end();
  53. const pathPrefix = 'ruleset.skk.moe-master/';
  54. const gunzip = zlib.createGunzip();
  55. const extract = tarExtract(
  56. PUBLIC_DIR,
  57. {
  58. ignore(_: string, header?: TarEntryHeaders) {
  59. if (header) {
  60. if (header.type !== 'file' && header.type !== 'directory') {
  61. return true;
  62. }
  63. if (header.type === 'file' && path.extname(header.name) === '.ts') {
  64. return true;
  65. }
  66. }
  67. return false;
  68. },
  69. map(header) {
  70. header.name = header.name.replace(pathPrefix, '');
  71. return header;
  72. }
  73. }
  74. );
  75. return pipeline(
  76. respBody,
  77. gunzip,
  78. extract
  79. );
  80. });
  81. });