download-previous-build.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import path from 'node:path';
  2. import { pipeline } from 'node:stream/promises';
  3. import { task } from './trace';
  4. import { extract as tarExtract } from 'tar-fs';
  5. import type { Headers as TarEntryHeaders } from 'tar-fs';
  6. import zlib from 'node:zlib';
  7. import undici from 'undici';
  8. const GITHUB_CODELOAD_URL = 'https://codeload.github.com/sukkalab/ruleset.skk.moe/tar.gz/master';
  9. const GITLAB_CODELOAD_URL = 'https://gitlab.com/SukkaW/ruleset.skk.moe/-/archive/master/ruleset.skk.moe-master.tar.gz';
  10. export const downloadPreviousBuild = task(require.main === module, __filename)(async (span) => {
  11. const publicDir = path.resolve(__dirname, '..', 'public');
  12. // if (fs.existsSync(publicDir)) {
  13. // console.log(picocolors.blue('Public directory exists, skip downloading previous build'));
  14. // return;
  15. // }
  16. const tarGzUrl = await span.traceChildAsync('get tar.gz url', async () => {
  17. const resp = await undici.request(GITHUB_CODELOAD_URL, { method: 'HEAD' });
  18. if (resp.statusCode !== 200) {
  19. console.warn('Download previous build from GitHub failed! Status:', resp.statusCode);
  20. console.warn('Switch to GitLab');
  21. return GITLAB_CODELOAD_URL;
  22. }
  23. return GITHUB_CODELOAD_URL;
  24. });
  25. return span.traceChildAsync('download & extract previoud build', async () => {
  26. const respBody = undici.pipeline(
  27. tarGzUrl,
  28. {
  29. method: 'GET',
  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. // Allow redirects by default
  37. maxRedirections: 5
  38. },
  39. ({ statusCode, body }) => {
  40. if (statusCode !== 200) {
  41. console.warn('Download previous build failed! Status:', statusCode);
  42. if (statusCode === 404) {
  43. throw new Error('Download previous build failed! 404');
  44. }
  45. }
  46. return body;
  47. }
  48. // by default, undici.pipeline returns a duplex stream (for POST/PUT)
  49. // Since we are using GET, we need to end the write immediately
  50. ).end();
  51. const pathPrefix = 'ruleset.skk.moe-master/';
  52. const gunzip = zlib.createGunzip();
  53. const extract = tarExtract(
  54. publicDir,
  55. {
  56. ignore: tarOnIgnore,
  57. map(header) {
  58. header.name = header.name.replace(pathPrefix, '');
  59. return header;
  60. }
  61. }
  62. );
  63. return pipeline(
  64. respBody,
  65. gunzip,
  66. extract
  67. );
  68. });
  69. });
  70. function tarOnIgnore(_: string, header?: TarEntryHeaders) {
  71. if (header) {
  72. if (header.type !== 'file' && header.type !== 'directory') {
  73. return true;
  74. }
  75. const extname = path.extname(header.name);
  76. if (extname === '.ts') {
  77. return true;
  78. }
  79. }
  80. return false;
  81. }