From a055dd998a88d449ad4517278039da3902d8cf00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrian=20Du=C8=99a?= Date: Tue, 4 Feb 2025 23:06:12 +0200 Subject: [PATCH] second attempt converting using the local directories --- src/communication.ts | 1 - src/library/interfaces.ts | 5 + src/main.ts | 201 +++++++++++--------------------------- src/preload.ts | 29 +++--- 4 files changed, 80 insertions(+), 156 deletions(-) diff --git a/src/communication.ts b/src/communication.ts index 5442f6c..2f9b9a4 100644 --- a/src/communication.ts +++ b/src/communication.ts @@ -10,4 +10,3 @@ export const showMessage = (obj: interfaces.ShowMessage) => { export const showError = (message: string) => { ipcRenderer.send('showError', message); } -`` \ No newline at end of file diff --git a/src/library/interfaces.ts b/src/library/interfaces.ts index c65f7c9..7b9824a 100644 --- a/src/library/interfaces.ts +++ b/src/library/interfaces.ts @@ -49,4 +49,9 @@ export interface ValidationMessage { export interface ErrorTippy { // eslint-disable-next-line @typescript-eslint/no-explicit-any [element: string]: Array +} + +export interface CommandArgs { + command: string; + variables: boolean; } \ No newline at end of file diff --git a/src/main.ts b/src/main.ts index 942974e..d72f0f1 100644 --- a/src/main.ts +++ b/src/main.ts @@ -20,12 +20,28 @@ import * as fs from "fs"; import * as webr from "webr"; import * as interfaces from './library/interfaces'; import { util } from "./library/helpers"; +import * as comm from "./communication"; const webR = new webr.WebR({ interactive: false }); let mainWindow: BrowserWindow; const root = production ? "../../" : "../"; +async function mount(dir: string) { + webR.FS.unmount("/host"); + await webR.FS.mkdir("/host"); + try { + await webR.FS.mount( + "NODEFS", + { root: dir }, + "/host" + ); + return(true) + } catch (error) { + return false; + } +} + async function initWebR() { await webR.init(); @@ -57,14 +73,6 @@ async function initWebR() { '/my-library' ); - // mount a directory from the host filesystem, to save various objects - await webR.FS.mkdir("/host"); - await webR.FS.mount( - "NODEFS", - { root: path.join(__dirname, root) }, - "/host" - ); - await webR.evalR(`.libPaths(c(.libPaths(), "/my-library"))`); await webR.evalR(`library(DDIwR)`); } @@ -120,24 +128,13 @@ app.on("activate", () => { } }); -app.on("before-quit", () => { - if (inputOutput.fileFromExt !== "") { - deleteFile( - path.join(__dirname, root, 'dataset' + inputOutput.fileFromExt) - ).then((result) => { - if (!result.success) { - dialog.showErrorBox("Error", result.error as string); - } - }); - } -}); app.on("window-all-closed", () => { app.quit(); }); -ipcMain.on('showDialogMessage', (event, args) => { +ipcMain.on('showMessage', (event, args) => { dialog.showMessageBox(mainWindow, { type: args.type, title: args.title, @@ -145,7 +142,9 @@ ipcMain.on('showDialogMessage', (event, args) => { }) }); - +ipcMain.on('showError', (event, message) => { + dialog.showErrorBox("Error", message) +}); ipcMain.on("gotoRODA", () => { @@ -159,7 +158,7 @@ ipcMain.on("declared", () => { ipcMain.on("selectFileFrom", async (event, args) => { if (args.inputType === "Select file type") { - dialog.showErrorBox("Error", "Select input type"); + comm.showError("Select input type"); } else { const info = util.fileFromInfo(args.inputType); @@ -183,44 +182,28 @@ ipcMain.on("selectFileFrom", async (event, args) => { inputOutput.fileFromName = path.basename(inputOutput.fileFrom, ext); inputOutput.fileFromDir = path.dirname(inputOutput.fileFrom); - if (inputOutput.fileFromExt !== "" && inputOutput.fileFromExt !== ext) { - deleteFile( - path.join(__dirname, root, 'dataset' + inputOutput.fileFromExt) - ).then((result) => { - if (!result.success) { - dialog.showErrorBox("Error", result.error as string); - } - }); - } - inputOutput.fileFromExt = ext; - if (OS_Windows) { - inputOutput.fileFrom = inputOutput.fileFrom.replace(/\\/g, '/'); - inputOutput.fileFromDir = inputOutput.fileFromDir.replace(/\\/g, '/'); - } - - - copyFile( - inputOutput.fileFrom, - path.join(__dirname, root, 'dataset' + ext) - ).then((result) => { - if (!result.success) { - dialog.showErrorBox("Error", result.error as string); - } else { + mount(inputOutput.fileFromDir).then((result) => { + if (result) { + // consolog(inputOutput); event.reply("selectFileFrom-reply", inputOutput); + } else { + comm.showError("Could not mount the directory"); } }); + + if (OS_Windows) { + inputOutput.fileFrom = inputOutput.fileFrom.replace(/\\/g, '/'); + inputOutput.fileFromDir = inputOutput.fileFromDir.replace(/\\/g, '/'); + } } }); } }); -ipcMain.on("inputType", (event, args) => { - inputOutput.fileFromExt = args.extension; -}) ipcMain.on("outputType", (event, args) => { inputOutput.fileToExt = args.extension; @@ -228,7 +211,7 @@ ipcMain.on("outputType", (event, args) => { ipcMain.on("selectFileTo", (event, args) => { if (args.outputType === "Select file type") { - dialog.showErrorBox("Error", "Select output type"); + comm.showError("Select output type"); } else { const ext = util.getExtensionFromType(args.outputType); @@ -266,16 +249,6 @@ ipcMain.on("selectFileTo", (event, args) => { }); - -ipcMain.on("showError", (event, args) => { - dialog.showMessageBox(mainWindow, { - type: "error", - message: args.message, - }); -}); - - - ipcMain.on("gotoRODA", () => { shell.openExternal("http://www.roda.ro"); }); @@ -286,100 +259,41 @@ ipcMain.on("declared", () => { // Handle the command request -ipcMain.on("sendCommand", async (event, command) => { +ipcMain.on("sendCommand", async (event, args) => { + const command = args.command + console.log(command); mainWindow.webContents.send("startLoader"); try { await webR.evalR(command); - const result = await webR.evalR(`as.character(jsonlite::toJSON(lapply( - collectRMetadata(dataset), - function(x) { - values <- names(x$labels) - names(values) <- x$labels - x$values <- as.list(values) - return(x) - } - )))`); + if (args.variables) { + + const result = await webR.evalR(`as.character(jsonlite::toJSON(lapply( + collectRMetadata(dataset), + function(x) { + values <- names(x$labels) + names(values) <- x$labels + x$values <- as.list(values) + return(x) + } + )))`); - if (!webr.isRCharacter(result)) throw new Error('Not a character!'); + if (!webr.isRCharacter(result)) { + comm.showError('Not a character!'); + } - const response = await result.toString(); - // mainWindow.webContents.send("consolog", JSON.parse(response[0] as string)); - webR.destroy(result); + const response = await result.toString(); + // consolog(JSON.parse(response[0] as string)); + webR.destroy(result); - event.reply("sendCommand-reply", JSON.parse(response)); - // return { success: true , variables: variables }; + event.reply("sendCommand-reply", JSON.parse(response)); + } } catch (error) { - dialog.showMessageBox(mainWindow, { - type: "error", - message: (error instanceof Error) ? error.message : String(error), - }); - // return { success: false, error: (error instanceof Error) ? error.message : String(error) }; + comm.showError(String(error)); } mainWindow.webContents.send("clearLoader"); }); -// Handle the command request -ipcMain.on("startConvert", async (event, args) => { - const command = args.command; - const embed = args.embed; - mainWindow.webContents.send("startLoader"); - try { - await webR.evalR(command); - - moveFile( - path.join(__dirname, root, inputOutput.fileFromName + inputOutput.fileToExt), - path.join(inputOutput.fileFromDir, inputOutput.fileFromName + inputOutput.fileToExt) - ).then((result) => { - if (result.success) { - if (!embed) { - moveFile( - path.join(__dirname, root, inputOutput.fileFromName + '.csv'), - path.join(inputOutput.fileFromDir, inputOutput.fileFromName + '.csv') - ); - } - } else { - dialog.showErrorBox("Error", result.error as string); - } - }); - } catch (error) { - dialog.showMessageBox(mainWindow, { - type: "error", - message: (error instanceof Error) ? error.message : String(error), - }); - } - mainWindow.webContents.send("clearLoader"); -}); - - -async function copyFile(src: string, dest: string) { - try { - await fs.promises.copyFile(src, dest); - return { success: true }; - } catch (error) { - return { success: false, error: (error instanceof Error) ? error.message : String(error) }; - } -} - -async function moveFile(src: string, dest: string) { - try { - await fs.promises.rename(src, dest); - return { success: true }; - } catch (error) { - return { success: false, error: (error instanceof Error) ? error.message : String(error) }; - } -} - -async function deleteFile(filePath: string) { - try { - await fs.promises.unlink(filePath); - return { success: true }; - } catch (error) { - return { success: false, error: (error instanceof Error) ? error.message : String(error) }; - } -} - - -const inputOutput: interfaces.InputOutputType = { +const inputOutput: interfaces.InputOutput = { inputType: "", fileFrom: "", fileFromDir: "", @@ -393,3 +307,6 @@ const inputOutput: interfaces.InputOutputType = { fileToExt: "" }; +function consolog(x: any) { + mainWindow.webContents.send("consolog", x); +} \ No newline at end of file diff --git a/src/preload.ts b/src/preload.ts index d5bc675..05bf436 100644 --- a/src/preload.ts +++ b/src/preload.ts @@ -29,11 +29,11 @@ window.addEventListener('DOMContentLoaded', () => { // } document.getElementById('declared')?.addEventListener('click', () => { - ipcRenderer.send('declared', {}); + ipcRenderer.send('declared'); }); document.getElementById('gotoRODA')?.addEventListener('click', () => { - ipcRenderer.send('gotoRODA', {}); + ipcRenderer.send('gotoRODA'); }); ipcRenderer.on('startLoader', () => { @@ -97,7 +97,7 @@ window.addEventListener('DOMContentLoaded', () => { inputOutput.fileFromName = io.fileFromName; inputOutput.fileFromExt = io.fileFromExt; - let command = "dataset <- convert('/host/dataset" + io.fileFromExt + "', declared = FALSE, n_max = 10"; + let command = "dataset <- convert('/host/" + io.fileFromName + io.fileFromExt + "', declared = FALSE, n_max = 10"; if (fileEncoding.value != 'utf8') { if (fileEncoding.value == "default") { command += ", encoding = NULL"; @@ -113,9 +113,12 @@ window.addEventListener('DOMContentLoaded', () => { } command += ")"; - console.log(command); - ipcRenderer.send('sendCommand', command.replace(/\\/g, '/')); + + ipcRenderer.send('sendCommand', { + command: command.replace(/\\/g, '/'), + variables: true + }); fileFrom.value = io.fileFrom; @@ -139,7 +142,7 @@ window.addEventListener('DOMContentLoaded', () => { if (message == 'Unsupported input type.') { inputType.selectedIndex = 0; - ipcRenderer.send('showError', { message: message }); + ipcRenderer.send('showError', message); } else { fileTo.dispatchEvent(new Event('change')); @@ -166,7 +169,7 @@ window.addEventListener('DOMContentLoaded', () => { outputType.selectedIndex = 0; inputOutput.outputType = ''; outputType.dispatchEvent(new Event('change')); - ipcRenderer.send('showError', { message: message }); + ipcRenderer.send('showError', message); } fileTo.dispatchEvent(new Event('change')); @@ -190,9 +193,9 @@ window.addEventListener('DOMContentLoaded', () => { } if (indices.length == 0 && !all_vars_selected) { - ipcRenderer.send('showError', { message: 'At least one variable has to be selected.' }); + ipcRenderer.send('showError', 'At least one variable has to be selected.'); } else { - let command = "convert('/host/dataset" + inputOutput.fileFromExt + "', to = '/host/" + inputOutput.fileToName + inputOutput.fileToExt + "'"; + let command = "convert('/host/" + inputOutput.fileFromName + inputOutput.fileFromExt + "', to = '/host/" + inputOutput.fileToName + inputOutput.fileToExt + "'"; // let command = "convert('" + inputOutput.fileFrom + "', to = '" + inputOutput.fileTo + "'"; const declaredTRUE = util.htmlElement("declaredTRUE"); @@ -306,9 +309,9 @@ window.addEventListener('DOMContentLoaded', () => { command += ")"; - ipcRenderer.send('startConvert', { + ipcRenderer.send('sendCommand', { command: command.replace(/\\/g, '/'), - embed: !embedFALSE.checked + variables: false }); } }); @@ -621,6 +624,6 @@ function insertAtPosition(areaId: string, text: string) { } -ipcRenderer.on('consolog', (event, message: string) => { - console.log(message); +ipcRenderer.on('consolog', (event, object: any) => { + console.log(object); });