-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
152 lines (127 loc) · 3.28 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
'use strict';
const electron = require('electron');
const app = electron.app;
const csv = require('csv');
const detect = require('detect-csv');
const fs = require('fs');
const $q = require('q');
var stringify = require('csv-stringify');
const encoding = require('encoding');
// adds debug features like hotkeys for triggering dev tools and reload
require('electron-debug')();
const ipc = require('electron').ipcMain
const dialog = require('electron').dialog
// prevent window being garbage collected
let mainWindow;
function onClosed() {
// dereference the window
// for multiple windows store them in an array
console.log('close');
mainWindow = null;
}
function createMainWindow() {
const win = new electron.BrowserWindow({
width: 850,
height: 850
});
win.loadURL(`file://${__dirname}/index.html`);
win.on('closed', onClosed);
return win;
}
function asPromise(func) {
let d = $q.defer();
func(d);
return d.promise;
}
function defaultResolver(def) {
return (err, data) => {
if(err) def.reject();
else def.resolve(data);
}
}
function readFile(path) {
return asPromise(d => fs.readFile(path, defaultResolver(d)));
}
function parseCsv(data, options) {
options.relax_column_count = true;
return asPromise(d => csv.parse(data, options, defaultResolver(d)));
}
function getDelimiter(csvData) {
var csv = detect(csvData);
return {delimiter : csv.delimiter};
}
function readCsvFile(path, enc) {
return readFile(path).then(data => {
if(enc) {
data = encoding.convert(data, 'utf8', enc);
}
return parseCsv(data, getDelimiter(data))
});
}
ipc.on('open-tucan-files', function (event) {
dialog.showOpenDialog({
properties: ['openFiles', 'multiSelections']
}, function (files) {
if (files) {
$q.allSettled(files.map(file => readCsvFile(file, 'windows1252'))).spread(function() {
var result = Object.keys(arguments).map(k => arguments[k].value);
console.log('result', result);
mainWindow.webContents.send('registrations', result);
});
}
})
})
ipc.on('open-result-file', function (event) {
dialog.showOpenDialog({
properties: ['openFiles']
}, function (files) {
if (files && files.length === 1) {
readCsvFile(files[0]).then(r => {
console.log(r);
mainWindow.webContents.send('results', r);
});
}
})
})
ipc.on('export', function(event, data) {
if(data) {
dialog.showOpenDialog({
properties: ['openFiles', 'openDirectory']
}, function (files) {
if (files && files.length === 1) {
var dir = files[0];
var errors = [];
data.forEach((f,index) => {
stringify(f, {delimiter : '\t'}, function(err, data) {
var name = dir + '/tucan-' + index + '.txt';
data = encoding.convert(data, 'windows1252', 'utf8');
fs.writeFile(name, data, (err) => {
if(err) {
errors.push(err);
}
});
});
});
if(errors.length > 0) {
mainWindow.webContents.send('exportError', err);
} else {
console.log('export success');
mainWindow.webContents.send('exportSuccess', {dir : dir, numberOfFiles : data.length});
}
}
})
}
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (!mainWindow) {
mainWindow = createMainWindow();
}
});
app.on('ready', () => {
mainWindow = createMainWindow();
});