download-previous-build.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { existsSync, createWriteStream } from 'fs';
  2. import { mkdir } from 'fs/promises';
  3. import path from 'path';
  4. import { pipeline } from '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 'zlib';
  11. import { Readable } from 'stream';
  12. const IS_READING_BUILD_OUTPUT = 1 << 2;
  13. const ALL_FILES_EXISTS = 1 << 3;
  14. export const downloadPreviousBuild = task(require.main === module, __filename)(async (span) => {
  15. const buildOutputList: string[] = [];
  16. let flag = 1 | ALL_FILES_EXISTS;
  17. await span
  18. .traceChild('read .gitignore')
  19. .traceAsyncFn(async () => {
  20. for await (const line of readFileByLine(path.resolve(__dirname, '../.gitignore'))) {
  21. if (line === '# $ build output') {
  22. flag = flag | IS_READING_BUILD_OUTPUT;
  23. continue;
  24. }
  25. if (!(flag & IS_READING_BUILD_OUTPUT)) {
  26. continue;
  27. }
  28. buildOutputList.push(line);
  29. if (!isCI) {
  30. if (!existsSync(path.join(__dirname, '..', line))) {
  31. flag = flag & ~ALL_FILES_EXISTS;
  32. }
  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. return span
  45. .traceChild('download & extract previoud build')
  46. .traceAsyncFn(async () => {
  47. const resp = await fetchWithRetry('https://codeload.github.com/sukkalab/ruleset.skk.moe/tar.gz/master', defaultRequestInit);
  48. if (!resp.body) {
  49. throw new Error('Download previous build failed! No body found');
  50. }
  51. const gunzip = zlib.createGunzip();
  52. const extract = tarStream.extract();
  53. pipeline(
  54. Readable.fromWeb(resp.body),
  55. gunzip,
  56. extract
  57. );
  58. const pathPrefix = `ruleset.skk.moe-master${path.sep}`;
  59. for await (const entry of extract) {
  60. if (entry.header.type !== 'file') {
  61. entry.resume(); // Drain the entry
  62. continue;
  63. }
  64. // filter entry
  65. if (!filesList.some(f => entry.header.name.startsWith(f))) {
  66. entry.resume(); // Drain the entry
  67. continue;
  68. }
  69. const relativeEntryPath = entry.header.name.replace(pathPrefix, '');
  70. const targetPath = path.join(__dirname, '..', relativeEntryPath);
  71. await mkdir(path.dirname(targetPath), { recursive: true });
  72. await pipeline(entry, createWriteStream(targetPath));
  73. }
  74. });
  75. });