将文件夹结构加载到NodeJS中的树视图JSON对象中

问题描述:

我想将文件夹结构(包括文件夹,json文件及其内容)映射到树视图(如JSON对象)中。我想问问是否有人会为我提供了一个功能,即loadFolder(“/ A”)会加载以下文件夹结构...将文件夹结构加载到NodeJS中的树视图JSON对象中

/A

/a/file1.json

/a/file2.json

/a/b/file1.json

/A/b/C

...将导致进入这个JSON对象:

{ 
     path: "/a", 
     name: "a", 
     type: "folder", 
     subnodes: [ 
      { 
       path: "https://stackoverflow.com/a/file1.json", 
       name: "file1.json", 
       type: "file", 
       content: { 
        // file content here 
       } 
      }, 
      { 
       path: "https://stackoverflow.com/a/file2.json", 
       name: "file2.json", 
       type: "file", 
       content: { 
        // file content here 
       } 
      }, 
      { 
       path: "https://stackoverflow.com/a/b", 
       name: "b", 
       type: "folder", 
       subnodes: [ 
        { 
         path: "https://stackoverflow.com/a/b/file1.json", 
         name: "file1.json", 
         type: "file", 
         content: { 
          // file content here 

         } 
        }, 
        { 
         path: "https://stackoverflow.com/a/b/c", 
         name: "c", 
         type: "folder", 
         subnodes: [] 
        }   
       ] 
      } 
     ] 
    } 

使用例如this模块,你应该很容易得到你的文件夹结构。因此,只需遍历数组仅查找文件,并使用内置的fs模块打开其中的每一个。