download-previous-build.js 2.9 KB

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