Browse Source

Perf: avoid string comparision

SukkaW 1 year ago
parent
commit
af04018be9
3 changed files with 15 additions and 16 deletions
  1. 0 7
      Build/build-common.ts
  2. 2 2
      Build/build-public.ts
  3. 13 7
      Build/lib/tree-dir.ts

+ 0 - 7
Build/build-common.ts

@@ -23,13 +23,6 @@ export const buildCommon = task(require.main === module, __filename)(async (span
 
 
   const paths = await new Fdir()
   const paths = await new Fdir()
     .withRelativePaths()
     .withRelativePaths()
-    // .exclude((dirName, dirPath) => {
-    //   if (dirName === 'domainset' || dirName === 'ip' || dirName === 'non_ip') {
-    //     return false;
-    //   }
-    //   console.error(picocolors.red(`[build-comman] Unknown dir: ${dirPath}`));
-    //   return true;
-    // })
     .filter((filepath, isDirectory) => {
     .filter((filepath, isDirectory) => {
       if (isDirectory) return true;
       if (isDirectory) return true;
 
 

+ 2 - 2
Build/build-public.ts

@@ -3,7 +3,7 @@ import fs from 'node:fs';
 import fsp from 'node:fs/promises';
 import fsp from 'node:fs/promises';
 
 
 import { task } from './trace';
 import { task } from './trace';
-import { treeDir } from './lib/tree-dir';
+import { treeDir, TreeFileType } from './lib/tree-dir';
 import type { TreeType, TreeTypeArray } from './lib/tree-dir';
 import type { TreeType, TreeTypeArray } from './lib/tree-dir';
 
 
 import { OUTPUT_MOCK_DIR, OUTPUT_MODULES_DIR, PUBLIC_DIR, ROOT_DIR } from './constants/dir';
 import { OUTPUT_MOCK_DIR, OUTPUT_MODULES_DIR, PUBLIC_DIR, ROOT_DIR } from './constants/dir';
@@ -101,7 +101,7 @@ function walk(tree: TreeTypeArray) {
   tree.sort(prioritySorter);
   tree.sort(prioritySorter);
   for (let i = 0, len = tree.length; i < len; i++) {
   for (let i = 0, len = tree.length; i < len; i++) {
     const entry = tree[i];
     const entry = tree[i];
-    if (entry.type === 'directory') {
+    if (entry.type === TreeFileType.DIRECTORY) {
       result += html`
       result += html`
         <li class="folder">
         <li class="folder">
           ${entry.name}
           ${entry.name}

+ 13 - 7
Build/lib/tree-dir.ts

@@ -1,20 +1,26 @@
 import fsp from 'node:fs/promises';
 import fsp from 'node:fs/promises';
 import { sep } from 'node:path';
 import { sep } from 'node:path';
 
 
-interface TreeFileType {
-  type: 'file',
+// eslint-disable-next-line sukka/no-export-const-enum -- TODO: fix this in the future
+export const enum TreeFileType {
+  FILE = 1,
+  DIRECTORY = 2
+}
+
+interface TreeFile {
+  type: TreeFileType.FILE,
   name: string,
   name: string,
   path: string
   path: string
 }
 }
 
 
 interface TreeDirectoryType {
 interface TreeDirectoryType {
-  type: 'directory',
+  type: TreeFileType.DIRECTORY,
   name: string,
   name: string,
   path: string,
   path: string,
   children: TreeTypeArray
   children: TreeTypeArray
 }
 }
 
 
-export type TreeType = TreeDirectoryType | TreeFileType;
+export type TreeType = TreeDirectoryType | TreeFile;
 export type TreeTypeArray = TreeType[];
 export type TreeTypeArray = TreeType[];
 
 
 type VoidOrVoidArray = void | VoidOrVoidArray[];
 type VoidOrVoidArray = void | VoidOrVoidArray[];
@@ -35,7 +41,7 @@ export async function treeDir(rootPath: string): Promise<TreeTypeArray> {
 
 
       if (child.isDirectory()) {
       if (child.isDirectory()) {
         const newNode: TreeDirectoryType = {
         const newNode: TreeDirectoryType = {
-          type: 'directory',
+          type: TreeFileType.DIRECTORY,
           name: child.name,
           name: child.name,
           path: childRelativeToRoot,
           path: childRelativeToRoot,
           children: []
           children: []
@@ -45,8 +51,8 @@ export async function treeDir(rootPath: string): Promise<TreeTypeArray> {
         continue;
         continue;
       }
       }
       if (child.isFile()) {
       if (child.isFile()) {
-        const newNode: TreeFileType = {
-          type: 'file',
+        const newNode: TreeFile = {
+          type: TreeFileType.FILE,
           name: child.name,
           name: child.name,
           path: childRelativeToRoot
           path: childRelativeToRoot
         };
         };