-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathserial-bridge.js
110 lines (107 loc) · 3.71 KB
/
serial-bridge.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const { ipcRenderer } = require('electron')
const path = require('path')
const SerialBridge = {
loadPorts: async () => {
return await ipcRenderer.invoke('serial', 'loadPorts')
},
connect: async (path) => {
return await ipcRenderer.invoke('serial', 'connect', path)
},
disconnect: async () => {
return await ipcRenderer.invoke('serial', 'disconnect')
},
run: async (code) => {
return await ipcRenderer.invoke('serial', 'run', code)
},
execFile: async (path) => {
return await ipcRenderer.invoke('serial', 'execFile', path)
},
getPrompt: async () => {
return await ipcRenderer.invoke('serial', 'getPrompt')
},
keyboardInterrupt: async () => {
await ipcRenderer.invoke('serial', 'keyboardInterrupt')
return Promise.resolve()
},
reset: async () => {
await ipcRenderer.invoke('serial', 'reset')
return Promise.resolve()
},
eval: (d) => {
return ipcRenderer.invoke('serial', 'eval', d)
},
onData: (callback) => {
// Remove all previous listeners
if (ipcRenderer.listeners("serial-on-data").length > 0) {
ipcRenderer.removeAllListeners("serial-on-data")
}
ipcRenderer.on('serial-on-data', (event, data) => {
callback(data)
})
},
listFiles: async (folder) => {
return await ipcRenderer.invoke('serial', 'listFiles', folder)
},
ilistFiles: async (folder) => {
return await ipcRenderer.invoke('serial', 'ilistFiles', folder)
},
loadFile: async (file) => {
return await ipcRenderer.invoke('serial', 'loadFile', file)
},
removeFile: async (file) => {
return await ipcRenderer.invoke('serial', 'removeFile', file)
},
saveFileContent: async (filename, content, dataConsumer) => {
if (ipcRenderer.listeners("serial-on-file-save-progress").length > 0) {
ipcRenderer.removeAllListeners("serial-on-file-save-progress")
}
ipcRenderer.on('serial-on-file-save-progress', (event, progress) => {
dataConsumer(progress)
})
return await ipcRenderer.invoke('serial', 'saveFileContent', filename, content)
},
uploadFile: async (src, dest, dataConsumer) => {
if (ipcRenderer.listeners("serial-on-upload-progress").length > 0) {
ipcRenderer.removeAllListeners("serial-on-upload-progress")
}
ipcRenderer.on('serial-on-upload-progress', (event, progress) => {
dataConsumer(progress)
})
return await ipcRenderer.invoke('serial', 'uploadFile', src, dest)
},
downloadFile: async (src, dest) => {
let contents = await ipcRenderer.invoke('serial', 'loadFile', src)
return ipcRenderer.invoke('save-file', dest, contents)
},
renameFile: async (oldName, newName) => {
return await ipcRenderer.invoke('serial', 'renameFile', oldName, newName)
},
onConnectionClosed: async (callback) => {
// Remove all previous listeners
if (ipcRenderer.listeners("serial-on-connection-closed").length > 0) {
ipcRenderer.removeAllListeners("serial-on-connection-closed")
}
ipcRenderer.on('serial-on-connection-closed', (event) => {
callback()
})
},
createFolder: async (folder) => {
return await ipcRenderer.invoke('serial', 'createFolder', folder)
},
removeFolder: async (folder) => {
return await ipcRenderer.invoke('serial', 'removeFolder', folder)
},
getNavigationPath: (navigation, target) => {
return path.posix.join(navigation, target)
},
getFullPath: (root, navigation, file) => {
return path.posix.join(root, navigation.replaceAll(path.win32.sep, path.posix.sep), file.replaceAll(path.win32.sep, path.posix.sep))
},
getParentPath: (navigation) => {
return path.posix.dirname(navigation)
},
fileExists: async (filePath) => {
return await ipcRenderer.invoke('serial', 'fileExists', filePath)
}
}
module.exports = SerialBridge