SOURCE

// 深度优先(非递归), 遍历所有节点
function deepTraversal(node) {
    if (!node) return nodeList

    const stack = []
    const nodeList = []

    stack.push(node)

    while(stack.length) {
        // 栈,后进先出
        const currentNode = stack.pop()
        if (currentNode != null) {
            nodeList.push(currentNode)
            const children = currentNode.children = []
            // 尾部先入栈
            for (let i = children.length - 1; i >= 0; i--) {
                stack.push(children[i])
            }
        }
    } 

    return nodeList
}


function fs() {}
function path() {}

// 深度优先(非递归),遍历读取文件列表
function deepReadFile(dirPath) {
  // 栈
  const stack = []
  const fileList = []

  stack.push(dirPath)
  
  while(stack.length) {
    const currentDirPath = stack.pop()

    // 读取文件状态
    const stats = fs.statSync(currentDirPath)

    //描述此文件/文件夹的对象
    const fileObj = {}
    fileObj.name = currentDirPath

    // 如果是文件夹
    if (stats.isDirectory()) {
      fileObj.type = 'dir'
      fileList.push(fileObj)

      const children = fs.readdirSync(currentDirPath)

      // 尾部先入栈
      for (let i = children.length - 1; i >= 0; i--) {
        // 拼接上 上个文件夹的路径 
        const childPath = path.join(currentDirPath, children[i])
        stack.push(childPath)
      }

    } else {
      // 设置type为后缀
      fileObj.type = path.extname(currentDirPath).substring(1)
      fileList.push(fileObj)
    }
  }

  return fileList
}
console 命令行工具 X clear

                    
>
console