Skip to content

Commit 45562de

Browse files
committed
Implemented disk to Board tree structures copy.
1 parent 9514627 commit 45562de

File tree

5 files changed

+310
-17
lines changed

5 files changed

+310
-17
lines changed

index.js

+21-13
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const path = require('path')
33
const fs = require('fs')
44
const openAboutWindow = require('about-window').default
55

6+
const { dir } = require('console')
67
let win = null // main window
78

89
// HELPERS
@@ -23,20 +24,21 @@ function listFolder(folder) {
2324
}
2425

2526
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+
}
3531

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+
});
4042
}
4143

4244
function ilistFolder(folder) {
@@ -74,6 +76,12 @@ ipcMain.handle('list-files', async (event, folder) => {
7476
return listFolder(folder)
7577
})
7678

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+
7785
ipcMain.handle('ilist-files', async (event, folder) => {
7886
console.log('ipcMain', 'ilist-files', folder)
7987
if (!folder) return []

package-lock.json

+222
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
},
3838
"license": "MIT",
3939
"dependencies": {
40+
"directory-tree": "*",
4041
"about-window": "^1.15.2",
4142
"micropython.js": "github:arduino/micropython.js#v1.4.4"
4243
},

preload.js

+3
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ const Disk = {
9696
ilistFiles: async (folder) => {
9797
return ipcRenderer.invoke('ilist-files', folder)
9898
},
99+
getFolderTree: async (folder) => {
100+
return ipcRenderer.invoke('get-folder-tree', folder)
101+
},
99102
loadFile: async (filePath) => {
100103
let content = await ipcRenderer.invoke('load-file', filePath)
101104
return new TextDecoder().decode(content)

0 commit comments

Comments
 (0)