From 49beae9decad371bb07bb99fe63c5afe2938ab83 Mon Sep 17 00:00:00 2001 From: w2xi <43wangxi@gmail.com> Date: Sat, 14 Dec 2024 14:45:55 +0800 Subject: [PATCH] refactor: drop dfs search strategy --- src/config.ts | 5 ----- src/handleOptions.ts | 7 ++----- src/index.ts | 2 +- src/toTree.ts | 49 ++++---------------------------------------- src/type.ts | 1 - test/toTree.spec.ts | 5 ++--- 6 files changed, 9 insertions(+), 60 deletions(-) diff --git a/src/config.ts b/src/config.ts index a67e5f1..3dad435 100644 --- a/src/config.ts +++ b/src/config.ts @@ -10,11 +10,6 @@ export const characters = { last: '└', } -export const defaultOptions = { - // strategy of finding tree structure, bfs by default - strategy: 'bfs', -} - export const NodeTypes = { ROOT: 'root', DIRECTORY: 'directory', diff --git a/src/handleOptions.ts b/src/handleOptions.ts index 7725d4a..d685efe 100644 --- a/src/handleOptions.ts +++ b/src/handleOptions.ts @@ -1,7 +1,6 @@ import fs from 'fs' import { Options } from './type' import { fileExistSync } from './utils' -import { defaultOptions } from './config' export const onExits: ((output: string) => void)[] = [] @@ -29,8 +28,6 @@ export function handleOptions(options: Options) { }) }) } - return { - ...defaultOptions, - ...options - } + + return options } diff --git a/src/index.ts b/src/index.ts index d802017..8ef59e4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -36,5 +36,5 @@ const result = generate(root.children, options) onExits.forEach((onExit) => onExit(result)) -console.log(root.name) +console.log(root.path) console.log(result) diff --git a/src/toTree.ts b/src/toTree.ts index 92ddd68..b91777d 100644 --- a/src/toTree.ts +++ b/src/toTree.ts @@ -1,56 +1,15 @@ import fs from 'fs' -import { resolve } from 'path' +import { basename, resolve } from 'path' import { isDirectory } from './utils' import { NodeTypes } from './config' import { sort } from './sort' import type { Options, TreeNode } from './type' -// The difference between statSync and lstatSync -// https://stackoverflow.com/questions/32478698/what-is-the-different-between-stat-fstat-and-lstat-functions-in-node-js - -// strategy: dfs or bfs, but bfs by default export function toTree(options: Options) { - const { strategy, directory } = options - - if (strategy === 'bfs') { - return bfs(directory, options) - } else { - return dfs(directory, options) - } -} - -function dfs(path: string, options: Options): TreeNode { - const dirName = path.split('/').pop() - if (isDirectory(path)) { - let dir = fs.readdirSync(path) - const ignore = options.ignore || [] - - if (ignore.length) { - dir = dir.filter((child) => !ignore.includes(child)) - } - if (options.onlyFolder) { - dir = dir.filter((child) => isDirectory(resolve(path, child))) - } - const children = dir.map((child) => dfs(resolve(path, child), options)) - - return { - type: NodeTypes.DIRECTORY, - name: dirName!, - children, - } - } else { - return { - type: NodeTypes.FILE, - name: dirName!, - } - } -} - -function bfs(path: string, options: Options) { - const { ignore, onlyFolder, layer } = options + const { directory, ignore, onlyFolder, layer } = options const root: TreeNode = { - path, - name: path, + path: directory, + name: basename(directory), type: NodeTypes.ROOT, children: [], } diff --git a/src/type.ts b/src/type.ts index 7606086..6b4a293 100644 --- a/src/type.ts +++ b/src/type.ts @@ -5,7 +5,6 @@ export interface Options { ignore?: string | string[] onlyFolder?: boolean layer?: number - strategy?: string icon?: boolean output?: string } diff --git a/test/toTree.spec.ts b/test/toTree.spec.ts index cd7a424..01f1350 100644 --- a/test/toTree.spec.ts +++ b/test/toTree.spec.ts @@ -5,11 +5,10 @@ import { NodeTypes } from '../src/config' import { getMaxLayer } from '../src/utils' describe('toTree', () => { - const options = { + const options: Options = { directory: process.cwd(), ignore: '.git,node_modules', - strategy: 'bfs' - } as Options + } test('root type', () => { const result = toTree(options) expect(result.type).toBe(NodeTypes.ROOT)