Skip to content

Commit

Permalink
fix: ignore some when using bfs (fix #2)
Browse files Browse the repository at this point in the history
  • Loading branch information
w2xi committed Mar 12, 2024
1 parent aa1f92c commit 103664b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 31 deletions.
46 changes: 25 additions & 21 deletions src/toTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ function dfs(path: string, options: Options): TreeNode {
}

function bfs(path: string, options: Options) {
let deep = 0
const { ignore, onlyFolder, layer } = options
const root = {
path,
Expand All @@ -55,32 +54,37 @@ function bfs(path: string, options: Options) {
} as TreeNode
const queue = [root]

while (queue.length) {
const node = queue.shift()
const { path, children } = node!
const dir = fs.readdirSync(path!)
let deep = 0

while (queue.length) {
if (layer && deep >= layer) break

deep++

for (let i = 0; i < dir.length; i++) {
const item = dir[i]
if (ignore && ignore.includes(item)) continue
const childPath = `${path}/${item}`
const isDir = isDirectory(childPath)
if (onlyFolder && !isDir) continue
let size = queue.length

while (size--) {
const node = queue.shift()
const { path, children } = node!
const dir = fs.readdirSync(path!)

for (let i = 0; i < dir.length; i++) {
const item = dir[i]
if (ignore && ignore.includes(item)) continue
const childPath = `${path}/${item}`
const isDir = isDirectory(childPath)
if (onlyFolder && !isDir) continue

const childItem = {
path: childPath,
name: item,
type: isDir ? NodeTypes.DIRECTORY : NodeTypes.FILE
} as TreeNode
if (isDir) {
queue.push(childItem)
childItem.children = []
const childItem = {
path: childPath,
name: item,
type: isDir ? NodeTypes.DIRECTORY : NodeTypes.FILE
} as TreeNode
if (isDir) {
queue.push(childItem)
childItem.children = []
}
children && children.push(childItem)
}
children && children.push(childItem)
}
}

Expand Down
13 changes: 13 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from 'fs'
import { TreeNode } from './type'

// check if a file or directory exists.
export function fileExistSync(path: string) {
Expand All @@ -14,3 +15,15 @@ export function isDirectory(path: string) {
const stats = fs.lstatSync(path)
return stats.isDirectory()
}

export function getMaxLayer(data: TreeNode) {
if (!data.children) return 0
let max = 0
for (let i = 0; i < data.children.length; i++) {
const child = data.children[i]
if (child.children && child.children.length) {
max = Math.max(max, getMaxLayer(child))
}
}
return max + 1
}
21 changes: 11 additions & 10 deletions test/toTree.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { describe, test, expect } from 'vitest'
import { toTree } from '../src/toTree'
import { Options } from '../src/type'
import { NodeTypes } from '../src/config'
import { getMaxLayer } from '../src/utils'

describe('toTree', () => {
const options = {
Expand All @@ -24,16 +25,16 @@ describe('toTree', () => {

test('1 layer', () => {
const result = toTree({ ...options, layer: 1 })
const layer = 1
let wantedLayer = 1
for (let i = 0; i < result.children!.length; i++) {
if (result.children![i].children) {
wantedLayer = 2
} else {
break
}
}
expect(wantedLayer).toBe(layer)
const wantedLayer = 1
const layer = getMaxLayer(result)
expect(layer).toBe(wantedLayer)
})

test('2 layer', () => {
const result = toTree({ ...options, layer: 2 })
const wantedLayer = 2
const layer = getMaxLayer(result)
expect(layer).toBe(wantedLayer)
})

test('only folder', () => {
Expand Down

0 comments on commit 103664b

Please sign in to comment.