From af04018be9116b1c965dcd7789db18a9495a2a33 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Sun, 22 Dec 2024 00:54:08 +0800 Subject: [PATCH] Perf: avoid string comparision --- Build/build-common.ts | 7 ------- Build/build-public.ts | 4 ++-- Build/lib/tree-dir.ts | 20 +++++++++++++------- 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/Build/build-common.ts b/Build/build-common.ts index 393f9f8f..2fc0984f 100644 --- a/Build/build-common.ts +++ b/Build/build-common.ts @@ -23,13 +23,6 @@ export const buildCommon = task(require.main === module, __filename)(async (span const paths = await new Fdir() .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) => { if (isDirectory) return true; diff --git a/Build/build-public.ts b/Build/build-public.ts index ac750a51..e93b6fe6 100644 --- a/Build/build-public.ts +++ b/Build/build-public.ts @@ -3,7 +3,7 @@ import fs from 'node:fs'; import fsp from 'node:fs/promises'; 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 { OUTPUT_MOCK_DIR, OUTPUT_MODULES_DIR, PUBLIC_DIR, ROOT_DIR } from './constants/dir'; @@ -101,7 +101,7 @@ function walk(tree: TreeTypeArray) { tree.sort(prioritySorter); for (let i = 0, len = tree.length; i < len; i++) { const entry = tree[i]; - if (entry.type === 'directory') { + if (entry.type === TreeFileType.DIRECTORY) { result += html`
  • ${entry.name} diff --git a/Build/lib/tree-dir.ts b/Build/lib/tree-dir.ts index 545baf6d..3e24928e 100644 --- a/Build/lib/tree-dir.ts +++ b/Build/lib/tree-dir.ts @@ -1,20 +1,26 @@ import fsp from 'node:fs/promises'; 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, path: string } interface TreeDirectoryType { - type: 'directory', + type: TreeFileType.DIRECTORY, name: string, path: string, children: TreeTypeArray } -export type TreeType = TreeDirectoryType | TreeFileType; +export type TreeType = TreeDirectoryType | TreeFile; export type TreeTypeArray = TreeType[]; type VoidOrVoidArray = void | VoidOrVoidArray[]; @@ -35,7 +41,7 @@ export async function treeDir(rootPath: string): Promise { if (child.isDirectory()) { const newNode: TreeDirectoryType = { - type: 'directory', + type: TreeFileType.DIRECTORY, name: child.name, path: childRelativeToRoot, children: [] @@ -45,8 +51,8 @@ export async function treeDir(rootPath: string): Promise { continue; } if (child.isFile()) { - const newNode: TreeFileType = { - type: 'file', + const newNode: TreeFile = { + type: TreeFileType.FILE, name: child.name, path: childRelativeToRoot };