-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
113 lines (88 loc) · 2.96 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
const { app, BrowserWindow, ipcMain, dialog } = require("electron");
const spawn = require("cross-spawn");
const path = require("path");
const EXPORT_PDF_NAME = "FilledExport.pdf";
let exportDirectoryPath = null;
let inputCSVPath = null;
let inputPDFPath = null;
/*************************************************************
* Helpers
*************************************************************/
function getPyExePath() {
return path.resolve(app.getAppPath(), `./pyapi/dist/program/program`).replace("app.asar", "app.asar.unpacked");
}
function selectDirectory() {
// Show a dialog box to select a directory
exportDirectoryPath = dialog.showOpenDialogSync({ properties: ['openDirectory'] })
}
/*************************************************************
* IPC/API Handlers
*************************************************************/
function postPDF(event, filepath) {
inputPDFPath = filepath;
}
function postCSV(event, filepath) {
inputCSVPath = filepath;
}
function log(event, message) {
console.log(message);
}
function getCSV(event) {
return inputCSVPath;
}
function getPDF(event) {
return inputPDFPath;
}
function processPDF(event) {
try {
if (inputCSVPath == null || inputCSVPath == undefined || inputPDFPath == null || inputPDFPath == undefined) {
return "Please upload both a CSV and a PDF";
}
selectDirectory();
if (exportDirectoryPath == null || exportDirectoryPath == "" || exportDirectoryPath == "undefined" || exportDirectoryPath == "null" || exportDirectoryPath == "NaN" || exportDirectoryPath == undefined) {
return "Must select a directory/path to export too";
}
const exportPath = path.join(exportDirectoryPath.toString(), EXPORT_PDF_NAME);
const result = spawn.sync(getPyExePath(), [inputCSVPath, inputPDFPath, exportPath]);
if (result.stderr == null || result.stderr == undefined || result.stderr.toString() == "" || result.stderr.toString() == " " || result.stderr.toString() == "\n") {
return "Successfully Filled PDF";
}
return result.stderr.toString();
} catch (err) {
return err.message;
}
}
/*************************************************************
* window management
*************************************************************/
function createWindow() {
const win = new BrowserWindow({
width: 600,
height: 450,
webPreferences: {
preload: path.join(__dirname, "preload.js"),
},
});
// api invokation handlers
ipcMain.on("postCSV", postCSV);
ipcMain.on("postPDF", postPDF);
ipcMain.on("log", log);
win.loadFile("index.html");
}
app.whenReady().then(() => {
// api return handlers
ipcMain.handle("getCSV", getCSV);
ipcMain.handle("getPDF", getPDF);
ipcMain.handle("processPDF", processPDF);
createWindow();
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
});
app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit();
}
});