-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.js
214 lines (192 loc) · 6.41 KB
/
main.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/* eslint-disable no-console*/
// Modules to control application life and create native browser window
const { app, BrowserWindow, ipcMain, dialog } = require("electron");
const path = require("path");
const bioParsers = require("bio-parsers");
const fs = require("fs");
const createMenu = require("./src/main_utils/menu");
const windowStateKeeper = require("electron-window-state");
const { autoUpdater } = require("electron-updater");
let isAppReady = false;
let isMacOpenTriggered = false;
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
const windows = [];
createMenu({ windows, createWindow, getSeqJsonFromPath });
async function getSeqJsonFromPath(_filePath) {
const filePath = _filePath || process.argv[1];
// const filePath = _filePath || process.argv[2] || process.argv[1];
if (filePath === ".") return;
const data = fs.readFileSync(path.resolve(filePath));
//open, read, handle file
if (!data) return;
const fileName = filePath.replace(/^.*[\\/]/, "");
try {
if (fileName.endsWith(".json") && (data.sequence || data.proteinSequence)) {
return data;
}
const res = await bioParsers.anyToJson(data, { fileName });
return res[0].parsedSequence;
} catch (error) {
console.error(`error:`, error);
return {};
}
}
function waitTillAppReady() {
return new Promise((resolve, reject) => {
const waitTillReadyInterval = setInterval(() => {
if (isAppReady) {
resolve();
clearInterval(waitTillReadyInterval);
}
}, 100);
});
}
async function createWindow({ initialSeqJson, filePath, windowToUse } = {}) {
await waitTillAppReady();
//if no windowVars are passed then we should
// Create the browser window.
if (filePath) {
let alreadyOpen = false;
windows.forEach((w) => {
if (w.__filePath === filePath) {
w.bw.show();
alreadyOpen = true;
}
});
if (alreadyOpen) {
return;
}
}
const mainWindowState = windowStateKeeper({
defaultWidth: 1000,
defaultHeight: 800,
});
let newWindow =
windowToUse ||
new BrowserWindow({
x: mainWindowState.x,
y: mainWindowState.y,
width: mainWindowState.width,
height: mainWindowState.height,
show: false,
webPreferences: {
contextIsolation: true,
// nodeIntegration: true, //we don't want to enable this because it is a security risk and slows down the app
preload: path.join(__dirname, "src/preload.js"),
},
});
newWindow.once("ready-to-show", () => {
newWindow.show();
});
// Let us register listeners on the window, so we can update the state
// automatically (the listeners will be removed when the window is closed)
// and restore the maximized or full screen state
mainWindowState.manage(newWindow);
!windowToUse &&
windows.push({
bw: newWindow,
//set a __filePath property so we can reference this if a user tries to open the same file multiple times
__filePath: filePath,
});
console.log(`initialSeqJson:`,initialSeqJson)
newWindow.loadFile("index.html", {
query: { initialSeqJson: JSON.stringify(initialSeqJson), filePath },
});
// Open the DevTools.
// newWindow.webContents.openDevTools()
// Emitted when the window is closed.
newWindow.on("closed", function () {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
let indexToSplice;
windows.forEach((w, i) => {
if (w.bw === newWindow) {
indexToSplice = i;
}
});
windows.splice(indexToSplice, 1);
newWindow = null;
});
}
app.on("open-file", async (event, path) => {
isMacOpenTriggered = true;
//mac only
event.preventDefault();
console.log(`open-file`)
try {
console.log("trying to open gb file");
const initialSeqJson = await getSeqJsonFromPath(path);
createWindow({ filePath: path, initialSeqJson });
} catch (e) {
console.error(`e73562891230:`, e);
}
});
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on("ready", async () => {
console.info(`App Starting Up`);
autoUpdater.checkForUpdatesAndNotify();
isAppReady = true;
if (!windows.length && !isMacOpenTriggered) {
let initialSeqJson;
if ( process.argv.length >= 2) {
initialSeqJson = await getSeqJsonFromPath();
}
createWindow({ filePath: path, initialSeqJson });
}
});
// Quit when all windows are closed.
app.on("window-all-closed", function () {
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== "darwin") app.quit();
});
app.on("activate", function () {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (!windows.length) {
console.log(`onActivate`);
createWindow();
}
});
// ipcMain.on("restart_app", () => {
// setImmediate(() => {
// autoUpdater.quitAndInstall();
// });
// });
/* HANDLE THE API CALLS FROM THE RENDERER PROCESS */
ipcMain.handle(
"ove_saveFile",
(event, { sequenceDataToSave, filePath, isSaveAs }) => {
const browserWindow = BrowserWindow.fromWebContents(event.sender);
const ext = path.extname(filePath);
let formattedSeqString;
if (ext === ".fasta") {
formattedSeqString = bioParsers.jsonToFasta(sequenceDataToSave);
} else if (ext === ".bed") {
formattedSeqString = bioParsers.jsonToBed(sequenceDataToSave);
} else if (ext === ".json") {
formattedSeqString = JSON.stringify(sequenceDataToSave, null, 2);
} else {
formattedSeqString = bioParsers.jsonToGenbank(sequenceDataToSave);
}
fs.writeFileSync(filePath, formattedSeqString);
!isSaveAs &&
windows.forEach((w) => {
if (w.bw === browserWindow) {
//update the __filePath prop we're saving on the window to prevent opening the same file twice
w.__filePath = filePath;
}
});
}
);
ipcMain.handle("ove_showSaveDialog", async (event, opts) => {
return dialog.showSaveDialogSync(
BrowserWindow.fromWebContents(event.sender),
opts
);
});
/* ************************************************** */