-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.js
207 lines (171 loc) · 5.98 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
// Modules to control application life and create native browser window
const {app, BrowserWindow, ipcMain} = require('electron')
const url = require('url')
const path = require('path')
var ipc = require('electron').ipcMain;
const { dialog } = require('electron')
const fs = require('fs');
// https://www.npmjs.com/package/find
var find = require('find');
var selectedSearchPath = "";
var selectedCopyPath = "";
var cards = [];
ipc.on('getFilePath', function(event, data){
//DIALOG DOCS: https://www.electronjs.org/docs/api/dialog
var path = dialog.showOpenDialog({
properties: ['openDirectory']
}).then(result => {
selectedSearchPath = result.filePaths[0];
event.sender.send('actionReply', selectedSearchPath);
}).catch(err => {
console.log(err);
});
});
ipc.on('getCopyPath', function(event, data){
//DIALOG DOCS: https://www.electronjs.org/docs/api/dialog
var path = dialog.showOpenDialog({
properties: ['openDirectory']
}).then(result => {
selectedCopyPath = result.filePaths[0];
event.sender.send('actionReply', selectedCopyPath);
}).catch(err => {
console.log(err);
});
});
ipc.on('findImages', function(event, data){
var allFiles = walk(selectedSearchPath);
cards = [];
//Start of the search for files.
var cardCount = 0;
data.forEach(inputLine => {
inputLine = inputLine.trim();
// Different sites have different sideboard formats.
// Look for the word "sideboard" or lines that start with a double slash and skip them.
if (/Sideboard/i.test(inputLine) || /^\/\//.test(inputLine) || inputLine === '') {
return;
}
card = {searchTerm:inputLine, filePath:"", alternateFiles:[]};
// Extract the quantity and card name.
// Cockatrice prefixes lines with "SB:" for sideboard cards, so optionally matching that.
// MTGA's export format puts the set and collector number in the line. ex. Arid Mesa (ZEN) 211
let extract = /^(?:SB:\s)?(?:(\d+)?x?\s)?([^(]+)(?:\s\(.+\) .+)?$/i.exec(inputLine);
if (extract === null) {
console.warn(`Failed to parse line: ${inputLine}`);
return;
}
let [, quantity, inputCardName] = extract;
// Search for a file.
var foundFileName = "";
allFiles.forEach(fileName => {
var imageName = "" + getFileNameFromPath(fileName);
if (normalizeCardName(imageName).includes(normalizeCardName(inputCardName))) {
foundFileName = fileName;
card.filePath = fileName;
// Add a number to the end of each ID to make them unique for multiples of the same card name.
card.styleID = normalizeCardName(inputLine).replace(/\s/g, '') + "----" + cardCount; //remove empty space from search term to use as id.
card.alternateFiles.push(fileName);
return;
}
});
cards.push(card);
cardCount++;
});
event.sender.send('actionReply', cards);
});
ipc.on('copyImages', function(event, data){
var errors = "";
var cardCount = 0;
for (let i = 0; i < cards.length; i++) {
card = cards[i];
var imageName = getFileNameFromPath(card.filePath);
var copyLocation = selectedCopyPath + "\\" + cardCount + "_" + imageName;
fs.copyFile(card.filePath, copyLocation, (err) => {
if (err) {
console.log("Error Found:", err);
errors += (""+err);
}
else {
//console.log("\nFile copied");
}
});
cardCount++;
}
event.sender.send('actionReply', errors);
});
ipc.on('updateCards', function(event, data){
console.log("cards data: " + data);
cards = data;
});
var getFileNameFromPath = function(path) {
var fileName = path.split('\\')[path.split('\\').length-1];
fileName = fileName.split('/')[fileName.split('/').length-1];
return fileName;
}
var walk = function(dir) {
var results = [];
var list = fs.readdirSync(dir);
list.forEach(function(file) {
file = dir + '/' + file;
var stat = fs.statSync(file);
if (stat && stat.isDirectory()) {
/* Recurse into a subdirectory */
results = results.concat(walk(file));
} else {
/* Is a file */
results.push(file);
}
});
return results;
}
/**
* There are a number of issues with card name input.
* The below tries to tackle some of the obvious ones, but it'll probably depend on the drive's format too.
*/
var normalizeCardName = function(cardName) {
return cardName
// Convert diacritics down.
.normalize('NFD')
.replace(/[\u0300-\u036f]/g, '')
// Fix those dumb apostrophes.
.replace(/’/g, `'`)
// Remove some common characters that are or aren't in some drives.
// Split cards are a recurring issue, so you might have to be more aggressive with spaces and slashes.
.replaceAll('_', '')
.replaceAll(`'`, '')
.replaceAll('/', '')
.toLowerCase();
}
function createWindow () {
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 1100,
height: 800,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
enableRemoteModule: true,
preload: path.join(__dirname, 'preload.js')
}
})
// and load the index.html of the app.
mainWindow.loadFile('index.html')
// Open the DevTools.
mainWindow.webContents.openDevTools()
}
// 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.whenReady().then(() => {
createWindow()
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 (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
})