download-previous-build.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. const GITHUB_CODELOAD_URL = 'https://codeload.github.com/sukkalab/ruleset.skk.moe/tar.gz/master';
  11. const GITLAB_CODELOAD_URL = 'https://gitlab.com/SukkaW/ruleset.skk.moe/-/archive/master/ruleset.skk.moe-master.tar.gz';
  12. export const downloadPreviousBuild = task(require.main === module, __filename)(async (span) => {
  13. const publicDir = path.resolve(__dirname, '..', 'public');
  14. if (fs.existsSync(publicDir)) {
  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. publicDir,
  57. {
  58. ignore: tarOnIgnore,
  59. map(header) {
  60. header.name = header.name.replace(pathPrefix, '');
  61. return header;
  62. }
  63. }
  64. );
  65. return pipeline(
  66. respBody,
  67. gunzip,
  68. extract
  69. );
  70. });
  71. });
  72. function tarOnIgnore(_: string, header?: TarEntryHeaders) {
  73. if (header) {
  74. if (header.type !== 'file' && header.type !== 'directory') {
  75. return true;
  76. }
  77. const extname = path.extname(header.name);
  78. if (extname === '.ts') {
  79. return true;
  80. }
  81. }
  82. return false;
  83. }