download-previous-build.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import tar from 'tar';
  2. import fsp from 'fs/promises';
  3. import path from 'path';
  4. import os from 'os';
  5. import { Readable } from 'stream';
  6. import { pipeline } from 'stream/promises';
  7. import { readFileByLine } from './lib/fetch-remote-text-by-line';
  8. import { isCI } from 'ci-info';
  9. import { task, traceAsync } from './lib/trace-runner';
  10. export const downloadPreviousBuild = task(__filename, async () => {
  11. const filesList = ['Clash', 'List'];
  12. let allFileExists = true;
  13. for await (const line of readFileByLine(path.resolve(__dirname, '../.gitignore'))) {
  14. if (
  15. (
  16. // line.startsWith('List/')
  17. line.startsWith('Modules/')
  18. ) && !line.endsWith('/')
  19. ) {
  20. filesList.push(line);
  21. if (!isCI) {
  22. allFileExists = await Bun.file(path.join(__dirname, '..', line)).exists();
  23. if (!allFileExists) {
  24. break;
  25. }
  26. }
  27. }
  28. }
  29. if (isCI) {
  30. allFileExists = false;
  31. }
  32. if (allFileExists) {
  33. console.log('All files exists, skip download.');
  34. return;
  35. }
  36. const extractedPath = path.join(os.tmpdir(), `sukka-surge-last-build-extracted-${Date.now()}`);
  37. await traceAsync(
  38. 'Download and extract previous build',
  39. () => Promise.all([
  40. fetch('https://codeload.github.com/sukkalab/ruleset.skk.moe/tar.gz/master'),
  41. fsp.mkdir(extractedPath, { recursive: true })
  42. ]).then(([resp]) => pipeline(
  43. Readable.fromWeb(resp.body!),
  44. tar.x({
  45. cwd: extractedPath,
  46. /**
  47. * @param {string} p
  48. */
  49. filter(p) {
  50. return p.includes('/List/') || p.includes('/Modules/') || p.includes('/Clash/');
  51. }
  52. })
  53. ))
  54. );
  55. console.log('Files list:', filesList);
  56. await Promise.all(filesList.map(async p => {
  57. const src = path.join(extractedPath, 'ruleset.skk.moe-master', p);
  58. if (await Bun.file(src).exists()) {
  59. return fsp.cp(
  60. src,
  61. path.join(__dirname, '..', p),
  62. { force: true, recursive: true }
  63. );
  64. }
  65. }));
  66. // return fsp.unlink(extractedPath).catch(() => { });
  67. });
  68. export const downloadPublicSuffixList = task(__filename, async () => {
  69. const publicSuffixDir = path.resolve(__dirname, '../node_modules/.cache');
  70. const publicSuffixPath = path.join(publicSuffixDir, 'public_suffix_list_dat.txt');
  71. const [resp] = await Promise.all([
  72. fetch('https://publicsuffix.org/list/public_suffix_list.dat'),
  73. fsp.mkdir(publicSuffixDir, { recursive: true })
  74. ]);
  75. return Bun.write(publicSuffixPath, resp);
  76. }, 'download-publicsuffixlist');
  77. if (import.meta.main) {
  78. Promise.all([
  79. downloadPreviousBuild(),
  80. downloadPublicSuffixList()
  81. ]);
  82. }