Chore: detail logging of time consumption

This commit is contained in:
SukkaW 2023-12-12 22:25:36 +08:00
parent a8e09e24de
commit dec8952226
3 changed files with 46 additions and 26 deletions

View File

@ -2,11 +2,16 @@
import path from 'path'; import path from 'path';
import { createRuleset } from './lib/create-file'; import { createRuleset } from './lib/create-file';
import { parseFelixDnsmasq } from './lib/parse-dnsmasq'; import { parseFelixDnsmasq } from './lib/parse-dnsmasq';
import { task } from './lib/trace-runner'; import { task, traceAsync } from './lib/trace-runner';
import { SHARED_DESCRIPTION } from './lib/constants'; import { SHARED_DESCRIPTION } from './lib/constants';
import picocolors from 'picocolors';
export const buildAppleCdn = task(import.meta.path, async () => { export const buildAppleCdn = task(import.meta.path, async () => {
const res = await parseFelixDnsmasq('https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/apple.china.conf'); const res = await traceAsync(
picocolors.gray('download dnsmasq-china-list apple.china.conf'),
() => parseFelixDnsmasq('https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/apple.china.conf'),
picocolors.gray
);
const description = [ const description = [
...SHARED_DESCRIPTION, ...SHARED_DESCRIPTION,

View File

@ -2,9 +2,10 @@ import { fetchRemoteTextAndReadByLine } from './lib/fetch-text-by-line';
import { resolve as pathResolve } from 'path'; import { resolve as pathResolve } from 'path';
import { compareAndWriteFile, withBannerArray } from './lib/create-file'; import { compareAndWriteFile, withBannerArray } from './lib/create-file';
import { processLineFromReadline } from './lib/process-line'; import { processLineFromReadline } from './lib/process-line';
import { task } from './lib/trace-runner'; import { task, traceAsync, traceSync } from './lib/trace-runner';
import { exclude } from 'fast-cidr-tools'; import { exclude } from 'fast-cidr-tools';
import picocolors from 'picocolors';
// https://github.com/misakaio/chnroutes2/issues/25 // https://github.com/misakaio/chnroutes2/issues/25
const EXCLUDE_CIDRS = [ const EXCLUDE_CIDRS = [
@ -17,8 +18,16 @@ const INCLUDE_CIDRS = [
]; ];
export const buildChnCidr = task(import.meta.path, async () => { export const buildChnCidr = task(import.meta.path, async () => {
const cidr = await processLineFromReadline(await fetchRemoteTextAndReadByLine('https://raw.githubusercontent.com/misakaio/chnroutes2/master/chnroutes.txt')); const cidr = await traceAsync(
const filteredCidr = exclude([...cidr, ...INCLUDE_CIDRS], EXCLUDE_CIDRS, true); picocolors.gray('download chnroutes2'),
async () => processLineFromReadline(await fetchRemoteTextAndReadByLine('https://raw.githubusercontent.com/misakaio/chnroutes2/master/chnroutes.txt')),
picocolors.gray
);
const filteredCidr = traceSync(
picocolors.gray('processing chnroutes2'),
() => exclude([...cidr, ...INCLUDE_CIDRS], EXCLUDE_CIDRS, true),
picocolors.gray
);
// Can not use SHARED_DESCRIPTION here as different license // Can not use SHARED_DESCRIPTION here as different license
const description = [ const description = [

View File

@ -17,32 +17,38 @@ export async function compareAndWriteFile(linesA: string[], filePath: string) {
console.log(`Nothing to write to ${filePath}...`); console.log(`Nothing to write to ${filePath}...`);
isEqual = false; isEqual = false;
} else { } else {
let index = 0; isEqual = await traceAsync(
picocolors.gray(`Comparing ${filePath}`),
async () => {
let index = 0;
for await (const lineB of readFileByLine(file)) { for await (const lineB of readFileByLine(file)) {
const lineA = linesA[index]; const lineA = linesA[index];
index++; index++;
if (lineA == null) { if (lineA == null) {
// The file becomes smaller // The file becomes smaller
isEqual = false; return false;
break; }
}
if (lineA[0] === '#' && lineB[0] === '#') { if (lineA[0] === '#' && lineB[0] === '#') {
continue; continue;
} }
if (lineA !== lineB) { if (lineA !== lineB) {
isEqual = false; return false;
break; }
} }
}
if (isEqual && index !== linesALen) { if (index !== linesALen) {
// The file becomes larger // The file becomes larger
isEqual = false; return false;
} }
return true;
},
picocolors.gray
);
} }
if (isEqual) { if (isEqual) {