浏览代码

Perf: use `fdir` for crawling

SukkaW 1 年之前
父节点
当前提交
b820b4be1c
共有 3 个文件被更改,包括 13 次插入10 次删除
  1. 12 10
      Build/build-common.ts
  2. 二进制
      bun.lockb
  3. 1 0
      package.json

+ 12 - 10
Build/build-common.ts

@@ -1,7 +1,6 @@
 // @ts-check
 
 import * as path from 'path';
-import { PathScurry } from 'path-scurry';
 import { readFileByLine } from './lib/fetch-text-by-line';
 import { processLine } from './lib/process-line';
 import { createRuleset } from './lib/create-file';
@@ -10,6 +9,7 @@ import type { Span } from './trace';
 import { task } from './trace';
 import { SHARED_DESCRIPTION } from './lib/constants';
 import picocolors from 'picocolors';
+import { fdir as Fdir } from 'fdir';
 
 const MAGIC_COMMAND_SKIP = '# $ custom_build_script';
 const MAGIC_COMMAND_TITLE = '# $ meta_title ';
@@ -22,27 +22,29 @@ const outputClashDir = path.resolve(import.meta.dir, '../Clash');
 export const buildCommon = task(import.meta.path, async (span) => {
   const promises: Array<Promise<unknown>> = [];
 
-  const pw = new PathScurry(sourceDir);
-  for await (const entry of pw) {
-    if (!entry.isFile()) {
-      continue;
-    }
+  const paths = await new Fdir()
+    .withRelativePaths()
+    .crawl(sourceDir)
+    .withPromise();
+
+  for (let i = 0, len = paths.length; i < len; i++) {
+    const relativePath = paths[i];
 
-    const extname = path.extname(entry.name);
+    const extname = path.extname(relativePath);
     if (extname === '.js' || extname === '.ts') {
       continue;
     }
+    const fullPath = sourceDir + path.sep + relativePath;
 
-    const relativePath = entry.relative();
     if (relativePath.startsWith('domainset/')) {
-      promises.push(transformDomainset(span, entry.fullpath(), relativePath));
+      promises.push(transformDomainset(span, fullPath, relativePath));
       continue;
     }
     if (
       relativePath.startsWith('ip/')
       || relativePath.startsWith('non_ip/')
     ) {
-      promises.push(transformRuleset(span, entry.fullpath(), relativePath));
+      promises.push(transformRuleset(span, fullPath, relativePath));
       continue;
     }
 

二进制
bun.lockb


+ 1 - 0
package.json

@@ -23,6 +23,7 @@
     "ci-info": "^4.0.0",
     "csv-parse": "^5.5.6",
     "fast-cidr-tools": "^0.2.5",
+    "fdir": "^6.1.1",
     "mnemonist": "^0.39.8",
     "path-scurry": "^1.11.1",
     "picocolors": "^1.0.1",