@@ -3,6 +3,7 @@ const path = require('path')
3
3
const fs = require ( 'fs' )
4
4
const openAboutWindow = require ( 'about-window' ) . default
5
5
6
+ const { dir } = require ( 'console' )
6
7
let win = null // main window
7
8
8
9
// HELPERS
@@ -23,20 +24,21 @@ function listFolder(folder) {
23
24
}
24
25
25
26
function getFolderTree ( folder_path , result = [ ] ) {
26
- fs . readdirSync ( folder_path ) . forEach ( ( file ) => {
27
- const fPath = path . resolve ( folder_path , file )
28
- const fileStats = { file, path : fPath }
29
- if ( fs . statSync ( fPath ) . isDirectory ( ) ) {
30
- fileStats . type = 'dir'
31
- fileStats . files = [ ]
32
- result . push ( fileStats )
33
- return traverse ( fPath , fileStats . files )
34
- }
27
+ let fsEntries = [ ]
28
+ traverseDir ( folder_path , fsEntries )
29
+ return fsEntries
30
+ }
35
31
36
- fileStats . type = 'file'
37
- result . push ( fileStats )
38
- } )
39
- return result
32
+ function traverseDir ( dir , list ) {
33
+ fs . readdirSync ( dir ) . forEach ( file => {
34
+ let filePath = path . join ( dir , file ) ;
35
+ let isDirectory = fs . lstatSync ( filePath ) . isDirectory ( )
36
+ let type = isDirectory ? 'dir' : 'file'
37
+ if ( isDirectory ) {
38
+ traverseDir ( filePath , list ) ;
39
+ }
40
+ list . unshift ( { filePath : filePath , parentPath : dir , fileName : file , type : type , isDirectory : isDirectory } ) ;
41
+ } ) ;
40
42
}
41
43
42
44
function ilistFolder ( folder ) {
@@ -74,6 +76,12 @@ ipcMain.handle('list-files', async (event, folder) => {
74
76
return listFolder ( folder )
75
77
} )
76
78
79
+ ipcMain . handle ( 'get-folder-tree' , async ( event , folder ) => {
80
+ console . log ( 'ipcMain' , 'get-folder-tree' , folder )
81
+ if ( ! folder ) return [ ]
82
+ return getFolderTree ( folder )
83
+ } )
84
+
77
85
ipcMain . handle ( 'ilist-files' , async ( event , folder ) => {
78
86
console . log ( 'ipcMain' , 'ilist-files' , folder )
79
87
if ( ! folder ) return [ ]
0 commit comments