download-previous-build.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. const { fetch } = require('undici');
  2. const tar = require('tar');
  3. const fs = require('fs');
  4. const fse = require('fs-extra');
  5. const { join, resolve } = 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(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(join(__dirname, '..', line));
  30. if (!allFileExists) {
  31. console.log(`File not exists: ${line}`);
  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 = 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/sukkaw/surge/tar.gz/gh-pages'),
  49. fse.ensureDir(extractedPath)
  50. ]).then(([resp]) => pipeline(
  51. Readable.fromWeb(resp.body),
  52. tar.x({
  53. cwd: extractedPath,
  54. filter(p) {
  55. const dir = p.split('/')[1];
  56. return dir === 'List' || dir === 'Modules' || dir === 'Clash';
  57. }
  58. })
  59. ))
  60. );
  61. console.log('Files list:', filesList);
  62. await Promise.all(filesList.map(async p => {
  63. const src = join(extractedPath, 'Surge-gh-pages', p);
  64. if (await fileExists(src)) {
  65. const dst = join(__dirname, '..', p);
  66. console.log('Copy', { src, dst });
  67. return fse.copy(
  68. src,
  69. join(__dirname, '..', p),
  70. { overwrite: true }
  71. );
  72. }
  73. console.log('File not exists:', src);
  74. }));
  75. await fs.promises.unlink(extractedPath).catch(() => { });
  76. });
  77. const downloadPublicSuffixList = task(__filename, async () => {
  78. const publicSuffixDir = resolve(__dirname, '../node_modules/.cache');
  79. const publicSuffixPath = join(publicSuffixDir, 'public_suffix_list_dat.txt');
  80. console.log('Download public suffix list.');
  81. const [resp] = await Promise.all([
  82. fetch('https://publicsuffix.org/list/public_suffix_list.dat'),
  83. fse.ensureDir(publicSuffixDir)
  84. ]);
  85. await pipeline(
  86. Readable.fromWeb(resp.body),
  87. fs.createWriteStream(publicSuffixPath)
  88. );
  89. }, 'download-publicsuffixlist');
  90. module.exports.downloadPreviousBuild = downloadPreviousBuild;
  91. module.exports.downloadPublicSuffixList = downloadPublicSuffixList;
  92. if (require.main === module) {
  93. Promise.all([
  94. downloadPreviousBuild(),
  95. downloadPublicSuffixList()
  96. ]);
  97. }