From 4be36265806cc68cd6bcab6d0f31205a5fb0ef5b Mon Sep 17 00:00:00 2001 From: SukkaW Date: Thu, 16 Nov 2023 12:50:36 +0800 Subject: [PATCH] CI: update build infra --- .gitignore | 6 ++-- Build/download-previous-build.ts | 52 +++++++++++++++++--------------- Build/index.ts | 2 ++ 3 files changed, 32 insertions(+), 28 deletions(-) diff --git a/.gitignore b/.gitignore index bc7cb211..853b0d74 100644 --- a/.gitignore +++ b/.gitignore @@ -4,8 +4,8 @@ node_modules .wireit public -List -Clash - +# $ build output +List/ +Clash/ Modules/sukka_local_dns_mapping.sgmodule Modules/sukka_url_redirect.sgmodule diff --git a/Build/download-previous-build.ts b/Build/download-previous-build.ts index 5e7fd770..65da50cb 100644 --- a/Build/download-previous-build.ts +++ b/Build/download-previous-build.ts @@ -1,4 +1,5 @@ import tar from 'tar'; +import fs from 'fs'; import fsp from 'fs/promises'; import path from 'path'; import os from 'os'; @@ -8,39 +9,44 @@ import { readFileByLine } from './lib/fetch-remote-text-by-line'; import { isCI } from 'ci-info'; import { task, traceAsync } from './lib/trace-runner'; -export const downloadPreviousBuild = task(__filename, async () => { - const filesList = ['Clash', 'List']; +const IS_READING_BUILD_OUTPUT = 1 << 2; +const ALL_FILES_EXISTS = 1 << 3; - let allFileExists = true; +export const downloadPreviousBuild = task(__filename, async () => { + const buildOutputList: string[] = []; + + let flag = 1 | ALL_FILES_EXISTS; for await (const line of readFileByLine(path.resolve(__dirname, '../.gitignore'))) { - if ( - ( - // line.startsWith('List/') - line.startsWith('Modules/') - ) && !line.endsWith('/') - ) { - filesList.push(line); + if (line === '# $ build output') { + flag = flag | IS_READING_BUILD_OUTPUT; + continue; + } + if (!(flag & IS_READING_BUILD_OUTPUT)) { + continue; + } - if (!isCI) { - allFileExists = await Bun.file(path.join(__dirname, '..', line)).exists(); - if (!allFileExists) { - break; - } + buildOutputList.push(line); + + if (!isCI) { + // Bun.file().exists() doesn't check directory + if (!fs.existsSync(path.join(__dirname, '..', line))) { + flag = flag & ~ALL_FILES_EXISTS; } } } if (isCI) { - allFileExists = false; + flag = flag & ~ALL_FILES_EXISTS; } - if (allFileExists) { + if (flag & ALL_FILES_EXISTS) { console.log('All files exists, skip download.'); return; } const extractedPath = path.join(os.tmpdir(), `sukka-surge-last-build-extracted-${Date.now()}`); + const filesList = buildOutputList.map(f => path.join('ruleset.skk.moe-master', f)); await traceAsync( 'Download and extract previous build', @@ -51,21 +57,17 @@ export const downloadPreviousBuild = task(__filename, async () => { Readable.fromWeb(resp.body!), tar.x({ cwd: extractedPath, - /** - * @param {string} p - */ filter(p) { - return p.includes('/List/') || p.includes('/Modules/') || p.includes('/Clash/'); + return filesList.some(f => p.startsWith(f)); } }) )) ); - console.log('Files list:', filesList); - - await Promise.all(filesList.map(async p => { + await Promise.all(buildOutputList.map(async p => { const src = path.join(extractedPath, 'ruleset.skk.moe-master', p); - if (await Bun.file(src).exists()) { + + if (fs.existsSync(src)) { // Bun.file().exists() doesn't check directory return fsp.cp( src, path.join(__dirname, '..', p), diff --git a/Build/index.ts b/Build/index.ts index 8928a5f2..f480b09a 100644 --- a/Build/index.ts +++ b/Build/index.ts @@ -108,6 +108,8 @@ const endWorker = async (worker: WithWorker) => { ]); printStats(stats); + } catch (e) { + console.error(e) } finally { await endWorker(buildInternalReverseChnCIDRWorker) }