download-previous-build.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import { existsSync, createWriteStream } from 'node:fs';
  2. import { mkdir } from 'node:fs/promises';
  3. import path from 'node:path';
  4. import { pipeline } from 'node:stream/promises';
  5. import { readFileByLine } from './lib/fetch-text-by-line';
  6. import { isCI } from 'ci-info';
  7. import { task } from './trace';
  8. import { defaultRequestInit, fetchWithRetry } from './lib/fetch-retry';
  9. import tarStream from 'tar-stream';
  10. import zlib from 'node:zlib';
  11. import { Readable } from 'node:stream';
  12. const IS_READING_BUILD_OUTPUT = 1 << 2;
  13. const ALL_FILES_EXISTS = 1 << 3;
  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. const buildOutputList: string[] = [];
  18. let flag = 1 | ALL_FILES_EXISTS;
  19. await span
  20. .traceChild('read .gitignore')
  21. .traceAsyncFn(async () => {
  22. for await (const line of readFileByLine(path.resolve(__dirname, '../.gitignore'))) {
  23. if (line === '# $ build output') {
  24. flag = flag | IS_READING_BUILD_OUTPUT;
  25. continue;
  26. }
  27. if (!(flag & IS_READING_BUILD_OUTPUT)) {
  28. continue;
  29. }
  30. buildOutputList.push(line);
  31. if (!isCI && !existsSync(path.join(__dirname, '..', line))) {
  32. flag = flag & ~ALL_FILES_EXISTS;
  33. }
  34. }
  35. });
  36. if (isCI) {
  37. flag = flag & ~ALL_FILES_EXISTS;
  38. }
  39. if (flag & ALL_FILES_EXISTS) {
  40. console.log('All files exists, skip download.');
  41. return;
  42. }
  43. const filesList = buildOutputList.map(f => path.join('ruleset.skk.moe-master', f));
  44. const tarGzUrl = await span.traceChildAsync('get tar.gz url', async () => {
  45. const resp = await fetchWithRetry(GITHUB_CODELOAD_URL, {
  46. ...defaultRequestInit,
  47. method: 'HEAD',
  48. retry: {
  49. retryOnNon2xx: false
  50. }
  51. });
  52. if (resp.status !== 200) {
  53. console.warn('Download previous build from GitHub failed! Status:', resp.status);
  54. console.warn('Switch to GitLab');
  55. return GITLAB_CODELOAD_URL;
  56. }
  57. return GITHUB_CODELOAD_URL;
  58. });
  59. return span.traceChildAsync('download & extract previoud build', async () => {
  60. const resp = await fetchWithRetry(tarGzUrl, {
  61. headers: {
  62. 'User-Agent': 'curl/8.9.1',
  63. // https://github.com/unjs/giget/issues/97
  64. // https://gitlab.com/gitlab-org/gitlab/-/commit/50c11f278d18fe1f3fb12eb595067216bb58ade2
  65. 'sec-fetch-mode': 'same-origin'
  66. },
  67. // https://github.com/unjs/giget/issues/97
  68. // https://gitlab.com/gitlab-org/gitlab/-/commit/50c11f278d18fe1f3fb12eb595067216bb58ade2
  69. mode: 'same-origin',
  70. retry: {
  71. retryOnNon2xx: false
  72. }
  73. });
  74. if (resp.status !== 200) {
  75. console.warn('Download previous build failed! Status:', resp.status);
  76. if (resp.status === 404) {
  77. return;
  78. }
  79. }
  80. if (!resp.body) {
  81. throw new Error('Download previous build failed! No body found');
  82. }
  83. const gunzip = zlib.createGunzip();
  84. const extract = tarStream.extract();
  85. pipeline(
  86. Readable.fromWeb(resp.body),
  87. gunzip,
  88. extract
  89. );
  90. const pathPrefix = 'ruleset.skk.moe-master/';
  91. for await (const entry of extract) {
  92. if (entry.header.type !== 'file') {
  93. entry.resume(); // Drain the entry
  94. continue;
  95. }
  96. // filter entry
  97. if (!filesList.some(f => entry.header.name.startsWith(f))) {
  98. entry.resume(); // Drain the entry
  99. continue;
  100. }
  101. const relativeEntryPath = entry.header.name.replace(pathPrefix, '');
  102. const targetPath = path.join(__dirname, '..', relativeEntryPath);
  103. await mkdir(path.dirname(targetPath), { recursive: true });
  104. await pipeline(entry, createWriteStream(targetPath));
  105. }
  106. });
  107. });