Replaced electron-settings with store. Implemented job upsert logic.
This commit is contained in:
@@ -5,8 +5,6 @@ const _ = require("lodash");
|
||||
async function DecodeEstimate(filePath) {
|
||||
const parsedFilePath = path.parse(filePath);
|
||||
let extensionlessFilePath = `${parsedFilePath.dir}\\${parsedFilePath.name}`;
|
||||
console.log("DecodeEstimate -> extensionlessFilePath", extensionlessFilePath);
|
||||
|
||||
const ret = {
|
||||
...(await DecodeAd1File(extensionlessFilePath)),
|
||||
...(await DecodeVehFile(extensionlessFilePath)),
|
||||
@@ -228,7 +226,6 @@ async function DecodeLinFile(extensionlessFilePath) {
|
||||
]),
|
||||
function (result, val, key) {
|
||||
if (key === "UNQ_SEQ") {
|
||||
console.log("unq");
|
||||
return (result[key.toLowerCase()] = val.toString());
|
||||
}
|
||||
return (result[key.toLowerCase()] = val);
|
||||
|
||||
5
electron/electron-store.js
Normal file
5
electron/electron-store.js
Normal file
@@ -0,0 +1,5 @@
|
||||
const Store = require("electron-store");
|
||||
|
||||
const store = new Store({ defaults: { filePaths: [] } });
|
||||
|
||||
exports.store = store;
|
||||
@@ -1,34 +1,33 @@
|
||||
const { ipcMain, dialog } = require("electron");
|
||||
const { StartWatcher, StopWatcher } = require("./file-watcher");
|
||||
const ipcTypes = require("../../src/ipc.types");
|
||||
const settings = require("electron-settings");
|
||||
const { mainWindow } = require("../main");
|
||||
const _ = require("lodash");
|
||||
const { store } = require("../electron-store");
|
||||
const path = require("path");
|
||||
|
||||
ipcMain.on(
|
||||
ipcTypes.default.fileWatcher.toMain.filepathsGet,
|
||||
async (event, object) => {
|
||||
const filePaths = await settings.get("filePaths");
|
||||
event.reply(
|
||||
ipcTypes.default.fileWatcher.toRenderer.filepathsList,
|
||||
filePaths
|
||||
store.get("filePaths")
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
ipcMain.on(ipcTypes.default.fileWatcher.toMain.start, async (event, arg) => {
|
||||
const filePaths = StartWatcher();
|
||||
// event.sender.send(ipcTypes.default.fileWatcher.toRenderer.startSuccess);
|
||||
StartWatcher();
|
||||
// event.sender.send(ipcTypes.default.fileWatcher.toRenderer.startSuccess);
|
||||
event.sender.send(
|
||||
ipcTypes.default.fileWatcher.toRenderer.filepathsList,
|
||||
filePaths
|
||||
store.get("filePaths")
|
||||
);
|
||||
event.sender.send(ipcTypes.default.fileWatcher.toRenderer.startSuccess);
|
||||
// event.sender.send(ipcTypes.default.fileWatcher.toRenderer.startSuccess);
|
||||
});
|
||||
|
||||
ipcMain.on(ipcTypes.default.fileWatcher.toMain.stop, async (event, arg) => {
|
||||
StopWatcher();
|
||||
event.sender.send(ipcTypes.default.fileWatcher.toRenderer.stopSuccess);
|
||||
});
|
||||
|
||||
ipcMain.on(ipcTypes.default.fileWatcher.toMain.addPath, async (event, arg) => {
|
||||
@@ -36,16 +35,36 @@ ipcMain.on(ipcTypes.default.fileWatcher.toMain.addPath, async (event, arg) => {
|
||||
properties: ["openDirectory"],
|
||||
});
|
||||
|
||||
await settings.set(
|
||||
"filePaths",
|
||||
_.union(result.filePaths, await settings.get("filePaths"))
|
||||
);
|
||||
|
||||
const newFilePaths = await settings.get("filePaths");
|
||||
console.log("newFilePaths", newFilePaths)
|
||||
StopWatcher();
|
||||
store.set("filePaths", _.union(result.filePaths, store.get("filePaths")));
|
||||
const newFilePaths = store.get("filePaths");
|
||||
|
||||
event.sender.send(
|
||||
ipcTypes.default.fileWatcher.toRenderer.filepathsList,
|
||||
newFilePaths
|
||||
);
|
||||
});
|
||||
|
||||
ipcMain.on(
|
||||
ipcTypes.default.fileWatcher.toMain.removePath,
|
||||
async (event, arg) => {
|
||||
StopWatcher();
|
||||
|
||||
store.set(
|
||||
"filePaths",
|
||||
store.get("filePaths").filter((k) => {
|
||||
const kf = path.parse(k);
|
||||
const argf = path.parse(arg);
|
||||
return kf.dir + kf.base !== argf.dir + argf.base;
|
||||
})
|
||||
);
|
||||
|
||||
const newFilePaths = store.get("filePaths");
|
||||
console.log("newFilePaths", newFilePaths);
|
||||
|
||||
event.sender.send(
|
||||
ipcTypes.default.fileWatcher.toRenderer.filepathsList,
|
||||
newFilePaths
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
@@ -1,24 +1,37 @@
|
||||
const chokidar = require("chokidar");
|
||||
const settings = require("electron-settings");
|
||||
const ipcTypes = require("../../src/ipc.types");
|
||||
const path = require("path");
|
||||
const { DecodeEstimate } = require("../decoder/decoder");
|
||||
const { BrowserWindow, Notification } = require("electron");
|
||||
const { BrowserWindow } = require("electron");
|
||||
const _ = require("lodash");
|
||||
const { store } = require("../electron-store");
|
||||
const {
|
||||
NewNotification,
|
||||
} = require("../notification-wrapper/notification-wrapper");
|
||||
|
||||
var watcher;
|
||||
|
||||
function StartWatcher() {
|
||||
const filePaths =
|
||||
settings
|
||||
.getSync("filePaths")
|
||||
.map((fp) => path.join(fp, "**.[eE][nN][vV]")) || [];
|
||||
store.get("filePaths").map((fp) => path.join(fp, "**.[eE][nN][vV]")) || [];
|
||||
console.log("StartWatcher -> filePaths", filePaths);
|
||||
|
||||
Notification({
|
||||
title: "Watcher started!",
|
||||
body: "Watcher has been started..",
|
||||
}).show();
|
||||
if (filePaths.length === 0) {
|
||||
NewNotification({
|
||||
title: "RPS Watcher cannot start",
|
||||
body: "Please set the appropriate file paths and try again.",
|
||||
}).show();
|
||||
return [];
|
||||
}
|
||||
|
||||
if (watcher) {
|
||||
try {
|
||||
console.log("Trying to close watcher - it already existed.");
|
||||
watcher.close().then();
|
||||
} catch (error) {
|
||||
console.log("Error trying to close Watcher.", error);
|
||||
}
|
||||
}
|
||||
|
||||
watcher = chokidar.watch(filePaths, {
|
||||
//ignored: /[\/\\]\./,
|
||||
@@ -29,6 +42,7 @@ function StartWatcher() {
|
||||
stabilityThreshold: 2000,
|
||||
},
|
||||
});
|
||||
|
||||
watcher
|
||||
.on("add", async function (path) {
|
||||
console.log("File", path, "has been added");
|
||||
@@ -62,11 +76,23 @@ function StartWatcher() {
|
||||
|
||||
function onWatcherReady() {
|
||||
console.log("Ready!");
|
||||
const b = BrowserWindow.getAllWindows()[0];
|
||||
b.webContents.send(ipcTypes.default.fileWatcher.toRenderer.startSuccess);
|
||||
NewNotification({
|
||||
title: "RPS Watcher Started",
|
||||
body: "Newly exported estimates will be automatically uploaded.",
|
||||
}).show();
|
||||
}
|
||||
|
||||
async function StopWatcher() {
|
||||
await watcher.close();
|
||||
console.log("Watcher Stopped!");
|
||||
console.log("Watcher stopped.");
|
||||
const b = BrowserWindow.getAllWindows()[0];
|
||||
b.webContents.send(ipcTypes.default.fileWatcher.toRenderer.stopSuccess);
|
||||
NewNotification({
|
||||
title: "RPS Watcher Stopped",
|
||||
body: "Estimates will not be automatically uploaded.",
|
||||
}).show();
|
||||
}
|
||||
|
||||
exports.StartWatcher = StartWatcher;
|
||||
@@ -80,14 +106,13 @@ async function HandleNewFile(path) {
|
||||
const newJobLow = _.transform(newJob, function (result, val, key) {
|
||||
result[key.toLowerCase()] = val;
|
||||
});
|
||||
console.log("HandleNewFile -> newJobLow", newJobLow);
|
||||
|
||||
b.webContents.send(
|
||||
ipcTypes.default.estimate.toRenderer.estimateDecodeSuccess,
|
||||
newJobLow
|
||||
);
|
||||
|
||||
Notification({
|
||||
NewNotification({
|
||||
title: "Job Uploaded",
|
||||
body: "A new job has been uploaded.",
|
||||
}).show();
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
const { ipcMain, dialog } = require("electron");
|
||||
const { ipcMain } = require("electron");
|
||||
const { mainWindow } = require("./main");
|
||||
const settings = require("electron-settings");
|
||||
const { DecodeEstimate } = require("./decoder/decoder");
|
||||
const ipcTypes = require("../src/ipc.types");
|
||||
//Import Ipc Handlers
|
||||
require("./file-watcher/file-watcher-ipc");
|
||||
|
||||
|
||||
@@ -1,16 +1,10 @@
|
||||
const path = require("path");
|
||||
const { app, BrowserWindow, Tray, Menu, ipcMain } = require("electron");
|
||||
const isDev = require("electron-is-dev");
|
||||
const settings = require("electron-settings");
|
||||
const { default: ipcTypes } = require("../src/ipc.types");
|
||||
|
||||
const { store } = require("./electron-store");
|
||||
require("./ipc-main-handler");
|
||||
|
||||
settings.configure({
|
||||
defaults: {
|
||||
foo: "bar",
|
||||
},
|
||||
});
|
||||
// Conditionally include the dev tools installer to load React Dev Tools
|
||||
let installExtension, REACT_DEVELOPER_TOOLS;
|
||||
if (isDev) {
|
||||
@@ -76,6 +70,8 @@ function createWindow() {
|
||||
// mode: "detach"
|
||||
});
|
||||
}
|
||||
|
||||
mainWindow.maximize();
|
||||
}
|
||||
|
||||
exports.mainWindow = mainWindow;
|
||||
@@ -86,7 +82,7 @@ app.whenReady().then(() => {
|
||||
createWindow();
|
||||
|
||||
if (isDev) {
|
||||
console.log(`Path to Settings File: ${settings.file()}`);
|
||||
console.log(`Path to Settings File: ${store.path}`);
|
||||
installExtension(REACT_DEVELOPER_TOOLS)
|
||||
.then((name) => console.log(`Added Extension: ${name}`))
|
||||
.catch((error) => console.log(`An error occurred: , ${error}`));
|
||||
|
||||
10
electron/notification-wrapper/notification-wrapper.js
Normal file
10
electron/notification-wrapper/notification-wrapper.js
Normal file
@@ -0,0 +1,10 @@
|
||||
const { Notification } = require("electron");
|
||||
const path = require("path");
|
||||
|
||||
function NewNotification(config) {
|
||||
return Notification({
|
||||
icon: path.join(__dirname, "../../src/assets/logo512.png"),
|
||||
...config,
|
||||
});
|
||||
}
|
||||
exports.NewNotification = NewNotification;
|
||||
Reference in New Issue
Block a user