CI: update build infra

This commit is contained in:
SukkaW 2023-11-16 12:50:36 +08:00
parent d1cd4918f1
commit 4be3626580
3 changed files with 32 additions and 28 deletions

6
.gitignore vendored
View File

@ -4,8 +4,8 @@ node_modules
.wireit .wireit
public public
List # $ build output
Clash List/
Clash/
Modules/sukka_local_dns_mapping.sgmodule Modules/sukka_local_dns_mapping.sgmodule
Modules/sukka_url_redirect.sgmodule Modules/sukka_url_redirect.sgmodule

View File

@ -1,4 +1,5 @@
import tar from 'tar'; import tar from 'tar';
import fs from 'fs';
import fsp from 'fs/promises'; import fsp from 'fs/promises';
import path from 'path'; import path from 'path';
import os from 'os'; import os from 'os';
@ -8,39 +9,44 @@ import { readFileByLine } from './lib/fetch-remote-text-by-line';
import { isCI } from 'ci-info'; import { isCI } from 'ci-info';
import { task, traceAsync } from './lib/trace-runner'; import { task, traceAsync } from './lib/trace-runner';
export const downloadPreviousBuild = task(__filename, async () => { const IS_READING_BUILD_OUTPUT = 1 << 2;
const filesList = ['Clash', 'List']; 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'))) { for await (const line of readFileByLine(path.resolve(__dirname, '../.gitignore'))) {
if ( if (line === '# $ build output') {
( flag = flag | IS_READING_BUILD_OUTPUT;
// line.startsWith('List/') continue;
line.startsWith('Modules/') }
) && !line.endsWith('/') if (!(flag & IS_READING_BUILD_OUTPUT)) {
) { continue;
filesList.push(line); }
if (!isCI) { buildOutputList.push(line);
allFileExists = await Bun.file(path.join(__dirname, '..', line)).exists();
if (!allFileExists) { if (!isCI) {
break; // Bun.file().exists() doesn't check directory
} if (!fs.existsSync(path.join(__dirname, '..', line))) {
flag = flag & ~ALL_FILES_EXISTS;
} }
} }
} }
if (isCI) { if (isCI) {
allFileExists = false; flag = flag & ~ALL_FILES_EXISTS;
} }
if (allFileExists) { if (flag & ALL_FILES_EXISTS) {
console.log('All files exists, skip download.'); console.log('All files exists, skip download.');
return; return;
} }
const extractedPath = path.join(os.tmpdir(), `sukka-surge-last-build-extracted-${Date.now()}`); 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( await traceAsync(
'Download and extract previous build', 'Download and extract previous build',
@ -51,21 +57,17 @@ export const downloadPreviousBuild = task(__filename, async () => {
Readable.fromWeb(resp.body!), Readable.fromWeb(resp.body!),
tar.x({ tar.x({
cwd: extractedPath, cwd: extractedPath,
/**
* @param {string} p
*/
filter(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(buildOutputList.map(async p => {
await Promise.all(filesList.map(async p => {
const src = path.join(extractedPath, 'ruleset.skk.moe-master', 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( return fsp.cp(
src, src,
path.join(__dirname, '..', p), path.join(__dirname, '..', p),

View File

@ -108,6 +108,8 @@ const endWorker = async <T>(worker: WithWorker<T>) => {
]); ]);
printStats(stats); printStats(stats);
} catch (e) {
console.error(e)
} finally { } finally {
await endWorker(buildInternalReverseChnCIDRWorker) await endWorker(buildInternalReverseChnCIDRWorker)
} }