-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
162 lines (142 loc) · 4.57 KB
/
index.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
const { app, BrowserWindow, ipcMain, dialog, shell } = require('electron');
const fs = require('fs');
const log = require('electron-log');
const path = require('path');
const unhandled = require('electron-unhandled');
const temp = require('temp').track();
unhandled({
showDialog: true,
});
console.log = log.log;
const Board = require('./src/Board');
const MissingMatrixError = require('./src/errors/MissingMatrixError');
const MissingRequiredFileError = require('./src/errors/MissingRequiredFileError');
const NotQMKError = require('./src/errors/NotQMKError');
try {
require('electron-reloader')(module)
} catch (_) { }
let mainWindow;
let board;
let name;
function createWindow() {
mainWindow = new BrowserWindow({
width: 1024,
height: 600,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
enableRemoteModule: true,
}
});
mainWindow.loadFile('public/index.html');
// mainWindow.openDevTools();
}
app.whenReady().then(() => {
createWindow();
console.log('created');
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
});
// app.on('unhandledRejection', err => {
// console.error('Unhandled rejection', err);
// });
// app.on('uncaughtException', err => {
// console.error('Unhandled error', err);
// });
const getRedirectFile = url => {
const contents = `
<script>
location.href = "${url}";
</script>
`;
const tmpPath = temp.mkdirSync('qmktools');
const file = path.join(tmpPath, 'redirect.html');
console.log('writing temp file', file);
fs.writeFileSync(file, contents);
return file;
};
ipcMain.on('select-keyboard', async (event, arg) => {
const result = await dialog.showOpenDialog(mainWindow, {
properties: ['openDirectory'],
});
if (result.canceled) return;
const tryAndRead = (file, error) => {
if (!fs.existsSync(file)) {
const expected = path.basename(file);
throw new MissingRequiredFileError(file);
}
return fs.readFileSync(file, 'utf8');
}
try {
const [mainPath] = result.filePaths;
// name = mainPath.split(path.sep).pop();
// const header = tryAndRead(`${mainPath}/${name}.h`);
// const config = tryAndRead(`${mainPath}/config.h`);
// const info = tryAndRead(`${mainPath}/info.json`);
// console.log('before board');
// board = new Board(header, config, info, { name });
board = Board.fromPath(mainPath);
name = board.name;
const images = {};
const tmpPath = temp.mkdirSync('qmktools');
for (let i = 0; i < Object.values(board.layouts).length; i++) {
const layout = Object.values(board.layouts)[i];
const fileName = path.join(tmpPath, `${layout.name}.png`);
console.log('Creating', fileName);
await layout.toPNG(fileName);
const image = fs.readFileSync(fileName).toString('base64');
images[layout.name] = image;
}
console.log('after board');
event.reply('keyboard-selected', {
config: board.config,
layouts: Object.keys(board.layouts),
images
});
} catch (error) {
console.error(error);
if (error instanceof MissingMatrixError) {
console.log('missing matrix error', { message: error.message });
event.reply('error', { message: error.message });
}
else if (error instanceof NotQMKError) {
console.log(error);
event.reply('error', { message: error.message, details: error.details });
}
else if (error instanceof MissingRequiredFileError) {
console.log('sending', error);
event.reply('error', error);
} else {
console.log('sending', { error });
event.reply('error', { error });
}
}
});
ipcMain.on('convert-keyboard', async (event, arg) => {
const result = await dialog.showOpenDialog(mainWindow, {
properties: ['openDirectory'],
});
if (result.canceled) return;
const [savePath] = result.filePaths;
const file = `${savePath}/${name}.json`;
console.log('file', file);
fs.writeFileSync(file, board.toVia());
event.reply('keyboard-converted', file);
});
ipcMain.on('load-layout', (event, name) => {
console.log('loading layout', name);
const layout = board.layouts[name].toString();
console.log(layout);
event.reply('layout-loaded', layout);
});
ipcMain.on('load-via-preview', event => {
const url = board.toPermalink();
log.log('VIA KLE URL', url);
event.reply('kle-permalink-loaded', `file://${getRedirectFile(url)}`);
});
ipcMain.on('load-kle-permalink', (event, name) => {
const url = board.layouts[name].toPermalink();
log.log('KLE URL', url);
event.reply('kle-permalink-loaded', `file://${getRedirectFile(url)}`);
});