download-previous-build.js 2.9 KB

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