2016-07-26 39 views
-1

我想將文件夾結構(包括文件夾,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: [] 
        }   
       ] 
      } 
     ] 
    } 

回答

0

使用例如this模塊,你應該很容易得到你的文件夾結構。因此,只需遍歷數組僅查找文件,並使用內置的fs模塊打開其中的每一個。

相關問題