Skip to content

Commit

Permalink
refactor: drop dfs search strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
w2xi committed Dec 14, 2024
1 parent a1f00cc commit 49beae9
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 60 deletions.
5 changes: 0 additions & 5 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
7 changes: 2 additions & 5 deletions src/handleOptions.ts
Original file line number Diff line number Diff line change
@@ -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)[] = []

Expand Down Expand Up @@ -29,8 +28,6 @@ export function handleOptions(options: Options) {
})
})
}
return {
...defaultOptions,
...options
}

return options
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
49 changes: 4 additions & 45 deletions src/toTree.ts
Original file line number Diff line number Diff line change
@@ -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: [],
}
Expand Down
1 change: 0 additions & 1 deletion src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export interface Options {
ignore?: string | string[]
onlyFolder?: boolean
layer?: number
strategy?: string
icon?: boolean
output?: string
}
Expand Down
5 changes: 2 additions & 3 deletions test/toTree.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 49beae9

Please sign in to comment.