浏览代码

Chore: detail logging of time consumption

SukkaW 2 年之前
父节点
当前提交
dec8952226
共有 3 个文件被更改,包括 51 次插入31 次删除
  1. 7 2
      Build/build-apple-cdn.ts
  2. 12 3
      Build/build-chn-cidr.ts
  3. 32 26
      Build/lib/create-file.ts

+ 7 - 2
Build/build-apple-cdn.ts

@@ -2,11 +2,16 @@
 import path from 'path';
 import { createRuleset } from './lib/create-file';
 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 picocolors from 'picocolors';
 
 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 = [
     ...SHARED_DESCRIPTION,

+ 12 - 3
Build/build-chn-cidr.ts

@@ -2,9 +2,10 @@ import { fetchRemoteTextAndReadByLine } from './lib/fetch-text-by-line';
 import { resolve as pathResolve } from 'path';
 import { compareAndWriteFile, withBannerArray } from './lib/create-file';
 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 picocolors from 'picocolors';
 
 // https://github.com/misakaio/chnroutes2/issues/25
 const EXCLUDE_CIDRS = [
@@ -17,8 +18,16 @@ const INCLUDE_CIDRS = [
 ];
 
 export const buildChnCidr = task(import.meta.path, async () => {
-  const cidr = await processLineFromReadline(await fetchRemoteTextAndReadByLine('https://raw.githubusercontent.com/misakaio/chnroutes2/master/chnroutes.txt'));
-  const filteredCidr = exclude([...cidr, ...INCLUDE_CIDRS], EXCLUDE_CIDRS, true);
+  const cidr = await traceAsync(
+    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
   const description = [

+ 32 - 26
Build/lib/create-file.ts

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