forked from arduino/lab-micropython-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreload.js
166 lines (160 loc) · 4.51 KB
/
preload.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
console.log('preload')
const { contextBridge, ipcRenderer } = require('electron')
const path = require('path')
const MicroPython = require('micropython.js')
const board = new MicroPython()
board.chunk_size = 192
board.chunk_sleep = 200
const Serial = {
loadPorts: async () => {
let ports = await board.list_ports()
return ports.filter(p => p.vendorId && p.productId)
},
connect: async (path) => {
return board.open(path)
},
disconnect: async () => {
return board.close()
},
run: async (code) => {
return board.run(code)
},
execFile: async (path) => {
return board.execfile(path)
},
getPrompt: async () => {
return board.get_prompt()
},
keyboardInterrupt: async () => {
await board.stop()
return Promise.resolve()
},
reset: async () => {
await board.stop()
await board.exit_raw_repl()
await board.reset()
return Promise.resolve()
},
eval: (d) => {
return board.eval(d)
},
onData: (fn) => {
board.serial.on('data', fn)
},
listFiles: async (folder) => {
return board.fs_ls(folder)
},
ilistFiles: async (folder) => {
return board.fs_ils(folder)
},
loadFile: async (file) => {
const output = await board.fs_cat(file)
return output || ''
},
removeFile: async (file) => {
return board.fs_rm(file)
},
saveFileContent: async (filename, content, dataConsumer) => {
return board.fs_save(content || ' ', filename, dataConsumer)
},
uploadFile: async (src, dest, dataConsumer) => {
return board.fs_put(src, dest, dataConsumer)
},
downloadFile: async (src, dest) => {
let contents = await Serial.loadFile(src)
return ipcRenderer.invoke('save-file', dest, contents)
},
renameFile: async (oldName, newName) => {
return board.fs_rename(oldName, newName)
},
onDisconnect: async (fn) => {
board.serial.on('close', fn)
},
createFolder: async (folder) => {
return await board.fs_mkdir(folder)
},
removeFolder: async (folder) => {
return await board.fs_rmdir(folder)
},
getNavigationPath: (navigation, target) => {
return path.posix.join(navigation, target)
},
getFullPath: (root, navigation, file) => {
return path.posix.join(root, navigation, file)
},
getParentPath: (navigation) => {
return path.posix.dirname(navigation)
},
fileExists: async (filePath) => {
// !!!: Fix this on micropython.js level
// ???: Check if file exists is not part of mpremote specs
const output = await board.run(`
import os
try:
os.stat("${filePath}")
print(0)
except OSError:
print(1)
`)
return output[2] === '0'
}
}
const Disk = {
openFolder: async () => {
return ipcRenderer.invoke('open-folder')
},
listFiles: async (folder) => {
return ipcRenderer.invoke('list-files', folder)
},
ilistFiles: async (folder) => {
return ipcRenderer.invoke('ilist-files', folder)
},
ilistAllFiles: async (folder) => {
return ipcRenderer.invoke('ilist-all-files', folder)
},
loadFile: async (filePath) => {
let content = await ipcRenderer.invoke('load-file', filePath)
return new TextDecoder().decode(content)
},
removeFile: async (filePath) => {
return ipcRenderer.invoke('remove-file', filePath)
},
saveFileContent: async (filePath, content) => {
return ipcRenderer.invoke('save-file', filePath, content)
},
renameFile: async (oldName, newName) => {
return ipcRenderer.invoke('rename-file', oldName, newName)
},
createFolder: async (folderPath) => {
return ipcRenderer.invoke('create-folder', folderPath)
},
removeFolder: async (folderPath) => {
return ipcRenderer.invoke('remove-folder', folderPath)
},
getNavigationPath: (navigation, target) => {
return path.join(navigation, target)
},
getFullPath: (root, navigation, file) => {
return path.resolve(path.join(root, navigation, file))
},
getParentPath: (navigation) => {
return path.dirname(navigation)
},
fileExists: async (filePath) => {
return ipcRenderer.invoke('file-exists', filePath)
},
getAppPath: () => {
return ipcRenderer.invoke('get-app-path')
}
}
const Window = {
setWindowSize: (minWidth, minHeight) => {
ipcRenderer.invoke('set-window-size', minWidth, minHeight)
},
beforeClose: (callback) => ipcRenderer.on('check-before-close', callback),
confirmClose: () => ipcRenderer.invoke('confirm-close'),
isPackaged: () => ipcRenderer.invoke('is-packaged')
}
contextBridge.exposeInMainWorld('BridgeSerial', Serial)
contextBridge.exposeInMainWorld('BridgeDisk', Disk)
contextBridge.exposeInMainWorld('BridgeWindow', Window)