download-previous-build.js 2.8 KB

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