| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- // @ts-check
- const tar = require('tar');
- const fsp = require('fs/promises');
- const path = require('path');
- const { tmpdir } = require('os');
- const { Readable } = require('stream');
- const { pipeline } = require('stream/promises');
- const { readFileByLine } = require('./lib/fetch-remote-text-by-line');
- const { isCI } = require('ci-info');
- const { task, traceAsync } = require('./lib/trace-runner');
- const downloadPreviousBuild = task(__filename, async () => {
- const filesList = ['Clash', 'List'];
- let allFileExists = true;
- for await (const line of readFileByLine(path.resolve(__dirname, '../.gitignore'))) {
- if (
- (
- // line.startsWith('List/')
- line.startsWith('Modules/')
- ) && !line.endsWith('/')
- ) {
- filesList.push(line);
- if (!isCI) {
- allFileExists = await Bun.file(path.join(__dirname, '..', line)).exists();
- if (!allFileExists) {
- break;
- }
- }
- }
- }
- if (isCI) {
- allFileExists = false;
- }
- if (allFileExists) {
- console.log('All files exists, skip download.');
- return;
- }
- const extractedPath = path.join(tmpdir(), `sukka-surge-last-build-extracted-${Date.now()}`);
- await traceAsync(
- 'Download and extract previous build',
- () => Promise.all([
- fetch('https://codeload.github.com/sukkalab/ruleset.skk.moe/tar.gz/master'),
- fsp.mkdir(extractedPath, { recursive: true })
- ]).then(([resp]) => pipeline(
- Readable.fromWeb(resp.body),
- tar.x({
- cwd: extractedPath,
- /**
- * @param {string} p
- */
- filter(p) {
- return p.includes('/List/') || p.includes('/Modules/') || p.includes('/Clash/');
- }
- })
- ))
- );
- console.log('Files list:', filesList);
- await Promise.all(filesList.map(async p => {
- const src = path.join(extractedPath, 'ruleset.skk.moe-master', p);
- if (await Bun.file(src).exists()) {
- return fsp.cp(
- src,
- path.join(__dirname, '..', p),
- { force: true, recursive: true }
- );
- }
- }));
- // return fsp.unlink(extractedPath).catch(() => { });
- });
- const downloadPublicSuffixList = task(__filename, async () => {
- const publicSuffixDir = path.resolve(__dirname, '../node_modules/.cache');
- const publicSuffixPath = path.join(publicSuffixDir, 'public_suffix_list_dat.txt');
- const [resp] = await Promise.all([
- fetch('https://publicsuffix.org/list/public_suffix_list.dat'),
- fsp.mkdir(publicSuffixDir, { recursive: true })
- ]);
- return Bun.write(publicSuffixPath, resp);
- }, 'download-publicsuffixlist');
- module.exports.downloadPreviousBuild = downloadPreviousBuild;
- module.exports.downloadPublicSuffixList = downloadPublicSuffixList;
- if (import.meta.main) {
- Promise.all([
- downloadPreviousBuild(),
- downloadPublicSuffixList()
- ]);
- }
|