download-previous-build.ts 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. import { requestWithLog } from './lib/fetch-retry';
  12. const GITHUB_CODELOAD_URL = 'https://codeload.github.com/sukkalab/ruleset.skk.moe/tar.gz/master';
  13. const GITLAB_CODELOAD_URL = 'https://gitlab.com/SukkaW/ruleset.skk.moe/-/archive/master/ruleset.skk.moe-master.tar.gz';
  14. export const downloadPreviousBuild = task(require.main === module, __filename)(async (span) => {
  15. if (fs.existsSync(PUBLIC_DIR)) {
  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 requestWithLog(GITHUB_CODELOAD_URL, { method: 'HEAD' });
  21. if (resp.statusCode !== 200) {
  22. console.warn('Download previous build from GitHub failed! Status:', resp.statusCode);
  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 respBody = undici.pipeline(
  30. tarGzUrl,
  31. {
  32. method: 'GET',
  33. headers: {
  34. 'User-Agent': 'curl/8.9.1',
  35. // https://github.com/unjs/giget/issues/97
  36. // https://gitlab.com/gitlab-org/gitlab/-/commit/50c11f278d18fe1f3fb12eb595067216bb58ade2
  37. 'sec-fetch-mode': 'same-origin'
  38. },
  39. // Allow redirects by default
  40. maxRedirections: 5
  41. },
  42. ({ statusCode, body }) => {
  43. if (statusCode !== 200) {
  44. console.warn('Download previous build failed! Status:', statusCode);
  45. if (statusCode === 404) {
  46. throw new Error('Download previous build failed! 404');
  47. }
  48. }
  49. return body;
  50. }
  51. // by default, undici.pipeline returns a duplex stream (for POST/PUT)
  52. // Since we are using GET, we need to end the write immediately
  53. ).end();
  54. const pathPrefix = 'ruleset.skk.moe-master/';
  55. const gunzip = zlib.createGunzip();
  56. const extract = tarExtract(
  57. PUBLIC_DIR,
  58. {
  59. ignore(_: string, header?: TarEntryHeaders) {
  60. if (header) {
  61. if (header.type !== 'file' && header.type !== 'directory') {
  62. return true;
  63. }
  64. if (header.type === 'file' && path.extname(header.name) === '.ts') {
  65. return true;
  66. }
  67. }
  68. return false;
  69. },
  70. map(header) {
  71. header.name = header.name.replace(pathPrefix, '');
  72. return header;
  73. }
  74. }
  75. );
  76. return pipeline(
  77. respBody,
  78. gunzip,
  79. extract
  80. );
  81. });
  82. });