download-previous-build.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. Accept: 'application/octet-stream'
  64. },
  65. retry: {
  66. retryOnNon2xx: false
  67. }
  68. });
  69. if (resp.status !== 200) {
  70. console.warn('Download previous build failed! Status:', resp.status);
  71. if (resp.status === 404) {
  72. return;
  73. }
  74. }
  75. if (!resp.body) {
  76. throw new Error('Download previous build failed! No body found');
  77. }
  78. const gunzip = zlib.createGunzip();
  79. const extract = tarStream.extract();
  80. pipeline(
  81. Readable.fromWeb(resp.body),
  82. gunzip,
  83. extract
  84. );
  85. const pathPrefix = 'ruleset.skk.moe-master/';
  86. for await (const entry of extract) {
  87. if (entry.header.type !== 'file') {
  88. entry.resume(); // Drain the entry
  89. continue;
  90. }
  91. // filter entry
  92. if (!filesList.some(f => entry.header.name.startsWith(f))) {
  93. entry.resume(); // Drain the entry
  94. continue;
  95. }
  96. const relativeEntryPath = entry.header.name.replace(pathPrefix, '');
  97. const targetPath = path.join(__dirname, '..', relativeEntryPath);
  98. await mkdir(path.dirname(targetPath), { recursive: true });
  99. await pipeline(entry, createWriteStream(targetPath));
  100. }
  101. });
  102. });