download-previous-build.ts 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. import { isDirectoryEmptySync } from './lib/misc';
  13. import { isCI } from 'ci-info';
  14. const GITHUB_CODELOAD_URL = 'https://codeload.github.com/sukkalab/ruleset.skk.moe/tar.gz/master';
  15. const GITLAB_CODELOAD_URL = 'https://gitlab.com/SukkaW/ruleset.skk.moe/-/archive/master/ruleset.skk.moe-master.tar.gz';
  16. export const downloadPreviousBuild = task(require.main === module, __filename)(async (span) => {
  17. if (fs.existsSync(PUBLIC_DIR) && !isDirectoryEmptySync(PUBLIC_DIR)) {
  18. console.log(picocolors.blue('Public directory exists, skip downloading previous build'));
  19. return;
  20. }
  21. // we uses actions/checkout to download the previous build now, so we should throw if the directory is empty
  22. if (isCI) {
  23. throw new Error('CI environment detected, but public directory is empty');
  24. }
  25. const tarGzUrl = await span.traceChildAsync('get tar.gz url', async () => {
  26. const resp = await requestWithLog(GITHUB_CODELOAD_URL, { method: 'HEAD' });
  27. if (resp.statusCode !== 200) {
  28. console.warn('Download previous build from GitHub failed! Status:', resp.statusCode);
  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 respBody = undici.pipeline(
  36. tarGzUrl,
  37. {
  38. method: 'GET',
  39. headers: {
  40. 'User-Agent': 'curl/8.9.1',
  41. // https://github.com/unjs/giget/issues/97
  42. // https://gitlab.com/gitlab-org/gitlab/-/commit/50c11f278d18fe1f3fb12eb595067216bb58ade2
  43. 'sec-fetch-mode': 'same-origin'
  44. },
  45. // Allow redirects by default
  46. maxRedirections: 5
  47. },
  48. ({ statusCode, body }) => {
  49. if (statusCode !== 200) {
  50. console.warn('Download previous build failed! Status:', statusCode);
  51. if (statusCode === 404) {
  52. throw new Error('Download previous build failed! 404');
  53. }
  54. }
  55. return body;
  56. }
  57. // by default, undici.pipeline returns a duplex stream (for POST/PUT)
  58. // Since we are using GET, we need to end the write immediately
  59. ).end();
  60. const pathPrefix = 'ruleset.skk.moe-master/';
  61. const gunzip = zlib.createGunzip();
  62. const extract = tarExtract(
  63. PUBLIC_DIR,
  64. {
  65. ignore(_: string, header?: TarEntryHeaders) {
  66. if (header) {
  67. if (header.type !== 'file' && header.type !== 'directory') {
  68. return true;
  69. }
  70. if (header.type === 'file' && path.extname(header.name) === '.ts') {
  71. return true;
  72. }
  73. }
  74. return false;
  75. },
  76. map(header) {
  77. header.name = header.name.replace(pathPrefix, '');
  78. return header;
  79. }
  80. }
  81. );
  82. return pipeline(
  83. respBody,
  84. gunzip,
  85. extract
  86. );
  87. });
  88. });