From ed3d7be82f40fded563cff3b31b23d89435ec135 Mon Sep 17 00:00:00 2001 From: Garrett Michael Flynn Date: Wed, 29 May 2024 10:41:15 -0700 Subject: [PATCH 01/12] Flatten Electron file --- package.json | 1 + src/electron/frontend/utils/electron.js | 77 +++++++++++-------------- tests/metadata.test.ts | 3 +- 3 files changed, 35 insertions(+), 46 deletions(-) diff --git a/package.json b/package.json index 81aea6245c..c425a2421f 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ "test:app": "vitest run --exclude \"**/pipelines.test.ts\"", "test:tutorial": "vitest tutorial", "test:pipelines": "vitest pipelines", + "test:metadata": "vitest metadata", "test:server": "pytest src/pyflask/tests/ -s -vv", "wait5s": "node -e \"setTimeout(() => process.exit(0),5000)\"", "test:executable": "concurrently -n EXE,TEST --kill-others --success first \"node tests/testPyinstallerExecutable.js --port 3434 --forever\" \"npm run wait5s && pytest src/pyflask/tests/ -s --target http://localhost:3434\"", diff --git a/src/electron/frontend/utils/electron.js b/src/electron/frontend/utils/electron.js index 7d85eb45fa..ef8ec1bae9 100644 --- a/src/electron/frontend/utils/electron.js +++ b/src/electron/frontend/utils/electron.js @@ -1,51 +1,40 @@ import { updateURLParams } from "./url.js"; -var userAgent = navigator.userAgent.toLowerCase(); +const userAgent = navigator.userAgent.toLowerCase(); export const isElectron = userAgent.indexOf(" electron/") > -1; -export let port = 4242; -export let SERVER_FILE_PATH = ""; export const electron = globalThis.electron ?? {}; // ipcRenderer, remote, shell, etc. -export let fs = null; -export let os = null; -export let remote = {}; -export let app = null; -export let path = null; -export let log = null; -export let crypto = null; - -// Used in tests -try { - crypto = require("crypto"); -} catch {} +// Node Modules +export const fs = require("fs-extra"); // File System +export const os = require("os"); +export const crypto = require("crypto"); +export const path = require("path"); + +// Remote Electron Modules +export const remote = isElectron ? require("@electron/remote") : {} +export const app = remote.app; + +// Electron Information +export const port = electron.ipcRenderer ? electron.ipcRenderer.sendSync("get-port") : 4242 +export const SERVER_FILE_PATH = electron.ipcRenderer ? electron.ipcRenderer.sendSync("get-server-file-path") : "" + + +// Link the renderer to the main process if (isElectron) { - try { - fs = require("fs-extra"); // File System - os = require("os"); - crypto = require("crypto"); - remote = require("@electron/remote"); - app = remote.app; - - electron.ipcRenderer.on("fileOpened", (info, filepath) => { - updateURLParams({ file: filepath }); - const dashboard = document.querySelector("nwb-dashboard"); - const activePage = dashboard.getAttribute("activePage"); - if (activePage === "preview") dashboard.requestUpdate(); - else dashboard.setAttribute("activePage", "preview"); - }); - - ["log", "warn", "error"].forEach((method) => - electron.ipcRenderer.on(`console.${method}`, (_, ...args) => console[method](`[main-process]:`, ...args)) - ); - - port = electron.ipcRenderer.sendSync("get-port"); - console.log("User OS:", os.type(), os.platform(), "version:", os.release()); - - SERVER_FILE_PATH = electron.ipcRenderer.sendSync("get-server-file-path"); - - path = require("path"); - } catch (error) { - console.error("Electron API access failed —", error); - } -} else console.warn("Electron API is blocked for web builds"); + + electron.ipcRenderer.on("fileOpened", (info, filepath) => { + updateURLParams({ file: filepath }); + const dashboard = document.querySelector("nwb-dashboard"); + const activePage = dashboard.getAttribute("activePage"); + if (activePage === "preview") dashboard.requestUpdate(); + else dashboard.setAttribute("activePage", "preview"); + }); + + ["log", "warn", "error"].forEach((method) => + electron.ipcRenderer.on(`console.${method}`, (_, ...args) => console[method](`[main-process]:`, ...args)) + ); + + console.log("User OS:", os.type(), os.platform(), "version:", os.release()); + +} \ No newline at end of file diff --git a/tests/metadata.test.ts b/tests/metadata.test.ts index 0bb326f31a..4773b4d5ed 100644 --- a/tests/metadata.test.ts +++ b/tests/metadata.test.ts @@ -7,13 +7,12 @@ import baseMetadataSchema from '../src/schemas/base-metadata.schema' import { createMockGlobalState } from './utils' import { Validator } from 'jsonschema' -import { tempPropertyKey, textToArray } from '../src/electron/frontend/core/components/forms/utils' +import { textToArray } from '../src/electron/frontend/core/components/forms/utils' import { updateResultsFromSubjects } from '../src/electron/frontend/core/components/pages/guided-mode/setup/utils' import { JSONSchemaForm } from '../src/electron/frontend/core/components/JSONSchemaForm' import { validateOnChange } from "../src/electron/frontend/core/validation/index.js"; import { SimpleTable } from '../src/electron/frontend/core/components/SimpleTable' -import { JSONSchemaInput } from '../src/electron/frontend/core/components/JSONSchemaInput.js' function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); From 9c95fb5b65915a1af31816bbc7aaed75600e5eb3 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 29 May 2024 17:43:43 +0000 Subject: [PATCH 02/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/electron/frontend/utils/electron.js | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/electron/frontend/utils/electron.js b/src/electron/frontend/utils/electron.js index ef8ec1bae9..315402501f 100644 --- a/src/electron/frontend/utils/electron.js +++ b/src/electron/frontend/utils/electron.js @@ -12,17 +12,15 @@ export const crypto = require("crypto"); export const path = require("path"); // Remote Electron Modules -export const remote = isElectron ? require("@electron/remote") : {} +export const remote = isElectron ? require("@electron/remote") : {}; export const app = remote.app; // Electron Information -export const port = electron.ipcRenderer ? electron.ipcRenderer.sendSync("get-port") : 4242 -export const SERVER_FILE_PATH = electron.ipcRenderer ? electron.ipcRenderer.sendSync("get-server-file-path") : "" - +export const port = electron.ipcRenderer ? electron.ipcRenderer.sendSync("get-port") : 4242; +export const SERVER_FILE_PATH = electron.ipcRenderer ? electron.ipcRenderer.sendSync("get-server-file-path") : ""; // Link the renderer to the main process if (isElectron) { - electron.ipcRenderer.on("fileOpened", (info, filepath) => { updateURLParams({ file: filepath }); const dashboard = document.querySelector("nwb-dashboard"); @@ -36,5 +34,4 @@ if (isElectron) { ); console.log("User OS:", os.type(), os.platform(), "version:", os.release()); - -} \ No newline at end of file +} From bae268480898bffce93e3c3176990830486d5c3d Mon Sep 17 00:00:00 2001 From: Garrett Michael Flynn Date: Wed, 29 May 2024 12:51:51 -0700 Subject: [PATCH 03/12] Fix tests and simplify conditional logic --- package.json | 1 + .../guided-mode/data/GuidedPathExpansion.js | 108 +++++++++--------- src/electron/frontend/core/globals.js | 27 +++-- src/electron/frontend/core/index.ts | 4 +- src/electron/frontend/core/progress/index.js | 4 +- .../frontend/core/progress/operations.js | 14 +-- src/electron/frontend/core/progress/update.js | 15 +-- src/electron/frontend/core/server/index.ts | 5 +- 8 files changed, 87 insertions(+), 91 deletions(-) diff --git a/package.json b/package.json index c425a2421f..64f15c4ec8 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ "test:app": "vitest run --exclude \"**/pipelines.test.ts\"", "test:tutorial": "vitest tutorial", "test:pipelines": "vitest pipelines", + "test:progress": "vitest progress", "test:metadata": "vitest metadata", "test:server": "pytest src/pyflask/tests/ -s -vv", "wait5s": "node -e \"setTimeout(() => process.exit(0),5000)\"", diff --git a/src/electron/frontend/core/components/pages/guided-mode/data/GuidedPathExpansion.js b/src/electron/frontend/core/components/pages/guided-mode/data/GuidedPathExpansion.js index e2f8832818..6c6e121d95 100644 --- a/src/electron/frontend/core/components/pages/guided-mode/data/GuidedPathExpansion.js +++ b/src/electron/frontend/core/components/pages/guided-mode/data/GuidedPathExpansion.js @@ -377,75 +377,73 @@ export class GuidedPathExpansionPage extends Page { validateOnChange: async (name, parent, parentPath) => { const value = parent[name]; - if (fs) { - const baseDir = form.getFormElement([...parentPath, "base_directory"]); - if (name === "format_string_path") { - if (value && baseDir && !baseDir.value) { - return [ - { - message: html`A base directory must be provided to locate your files.`, - type: "error", - }, - ]; - } + const baseDir = form.getFormElement([...parentPath, "base_directory"]); + if (name === "format_string_path") { + if (value && baseDir && !baseDir.value) { + return [ + { + message: html`A base directory must be provided to locate your files.`, + type: "error", + }, + ]; + } - const base_directory = [...parentPath, "base_directory"].reduce( - (acc, key) => acc[key], - this.form.resolved - ); + const base_directory = [...parentPath, "base_directory"].reduce( + (acc, key) => acc[key], + this.form.resolved + ); - if (!base_directory) return true; // Do not calculate if base is not found + if (!base_directory) return true; // Do not calculate if base is not found - const entry = { base_directory }; + const entry = { base_directory }; - if (value.split(".").length > 1) entry.file_path = value; - else entry.folder_path = value; + if (value.split(".").length > 1) entry.file_path = value; + else entry.folder_path = value; - const interfaceName = parentPath.slice(-1)[0]; + const interfaceName = parentPath.slice(-1)[0]; - const results = await run( - `neuroconv/locate`, - { [interfaceName]: entry }, - { swal: false } - ).catch((error) => { - this.notify(error.message, "error"); - throw error; - }); + const results = await run( + `neuroconv/locate`, + { [interfaceName]: entry }, + { swal: false } + ).catch((error) => { + this.notify(error.message, "error"); + throw error; + }); - const resolved = []; + const resolved = []; - for (let sub in results) { - for (let ses in results[sub]) { - const source_data = results[sub][ses].source_data[interfaceName]; - const path = source_data.file_path ?? source_data.folder_path; - resolved.push(path.slice(base_directory.length + 1)); - } + for (let sub in results) { + for (let ses in results[sub]) { + const source_data = results[sub][ses].source_data[interfaceName]; + const path = source_data.file_path ?? source_data.folder_path; + resolved.push(path.slice(base_directory.length + 1)); } + } - if (resolved.length === 0) - return [ - { - message: html`No source files found using the provided information.`, - type: "warning", - }, - ]; - + if (resolved.length === 0) return [ { - message: html`

Source Files Found

- ${base_directory} - ${new List({ - items: resolved.map((path) => { - return { value: path }; - }), - editable: false, - })}`, - type: "info", + message: html`No source files found using the provided information.`, + type: "warning", }, ]; - } + + return [ + { + message: html`

Source Files Found

+ ${base_directory} + ${new List({ + items: resolved.map((path) => { + return { value: path }; + }), + editable: false, + })}`, + type: "info", + }, + ]; } }, })); diff --git a/src/electron/frontend/core/globals.js b/src/electron/frontend/core/globals.js index fa3fc0e26e..b36a6e4c69 100644 --- a/src/electron/frontend/core/globals.js +++ b/src/electron/frontend/core/globals.js @@ -1,9 +1,11 @@ -import { app, path, crypto, isElectron } from "../utils/electron.js"; +import { os, path, crypto, isElectron } from "../utils/electron.js"; import paths from "../../../paths.config.json" assert { type: "json" }; import supportedInterfaces from "../../../supported_interfaces.json" assert { type: "json" }; +export const isTestEnvironment = globalThis?.process?.env?.VITEST + export const joinPath = (...args) => (path ? path.join(...args) : args.filter((str) => str).join("/")); export let runOnLoad = (fn) => { @@ -17,24 +19,25 @@ export const reloadPageToHome = () => { }; // Clear all query params // Filesystem Management -const root = globalThis?.process?.env?.VITEST ? joinPath(paths.root, ".test") : paths.root; -export const homeDirectory = app?.getPath("home") ?? ""; -export const appDirectory = homeDirectory ? joinPath(homeDirectory, root) : ""; -export const guidedProgressFilePath = appDirectory ? joinPath(appDirectory, ...paths.subfolders.progress) : ""; +const root = isTestEnvironment ? joinPath(paths.root, ".test") : paths.root; + +export const homeDirectory = os.homedir(); + +export const appDirectory = joinPath(homeDirectory, root) + +export const guidedProgressFilePath = joinPath(appDirectory, ...paths.subfolders.progress) -export const previewSaveFolderPath = appDirectory ? joinPath(appDirectory, ...paths.subfolders.preview) : ""; -export const conversionSaveFolderPath = appDirectory ? joinPath(appDirectory, ...paths.subfolders.conversions) : ""; +export const previewSaveFolderPath = joinPath(appDirectory, ...paths.subfolders.preview) +export const conversionSaveFolderPath = joinPath(appDirectory, ...paths.subfolders.conversions) -export const testDataFolderPath = appDirectory ? joinPath(appDirectory, ...paths.subfolders.testdata) : ""; +export const testDataFolderPath = joinPath(appDirectory, ...paths.subfolders.testdata) // Encryption const IV_LENGTH = 16; const KEY_LENGTH = 32; -export const ENCRYPTION_KEY = appDirectory - ? Buffer.concat([Buffer.from(appDirectory), Buffer.alloc(KEY_LENGTH)], KEY_LENGTH) - : null; +export const ENCRYPTION_KEY = Buffer.concat([Buffer.from(appDirectory), Buffer.alloc(KEY_LENGTH)], KEY_LENGTH) -export const ENCRYPTION_IV = crypto ? crypto.randomBytes(IV_LENGTH) : null; +export const ENCRYPTION_IV = crypto.randomBytes(IV_LENGTH); // Storybook export const isStorybook = window.location.href.includes("iframe.html"); diff --git a/src/electron/frontend/core/index.ts b/src/electron/frontend/core/index.ts index 08415c3e10..c7d0a48973 100644 --- a/src/electron/frontend/core/index.ts +++ b/src/electron/frontend/core/index.ts @@ -1,5 +1,7 @@ import "./pages.js" import { isElectron, electron } from '../utils/electron.js' +import { isTestEnvironment } from './globals.js' + const { ipcRenderer } = electron; import { Dashboard } from './components/Dashboard.js' @@ -55,8 +57,6 @@ async function isOnline() { statusBar.items[1].status = true - const isTestEnvironment = globalThis?.process?.env?.VITEST - if (isTestEnvironment) return notyf.open({ diff --git a/src/electron/frontend/core/progress/index.js b/src/electron/frontend/core/progress/index.js index 761ab85348..ce601970a3 100644 --- a/src/electron/frontend/core/progress/index.js +++ b/src/electron/frontend/core/progress/index.js @@ -8,6 +8,7 @@ import { ENCRYPTION_KEY, ENCRYPTION_IV, } from "../globals.js"; + import { fs, crypto } from "../../utils/electron.js"; import { joinPath, runOnLoad } from "../globals"; @@ -87,8 +88,7 @@ class GlobalAppConfig { save() { const encoded = encodeObject(this.data); - if (fs) fs.writeFileSync(this.path, JSON.stringify(encoded, null, 2)); - else localStorage.setItem(this.path, JSON.stringify(encoded)); + fs.writeFileSync(this.path, JSON.stringify(encoded, null, 2)); } } diff --git a/src/electron/frontend/core/progress/operations.js b/src/electron/frontend/core/progress/operations.js index 8d60e238ef..3154e349d8 100644 --- a/src/electron/frontend/core/progress/operations.js +++ b/src/electron/frontend/core/progress/operations.js @@ -7,17 +7,13 @@ export const remove = (name) => { const progressFilePathToDelete = joinPath(guidedProgressFilePath, name + ".json"); //delete the progress file - if (fs) { - if (fs.existsSync(progressFilePathToDelete)) fs.unlinkSync(progressFilePathToDelete); - } else localStorage.removeItem(progressFilePathToDelete); + if (fs.existsSync(progressFilePathToDelete)) fs.unlinkSync(progressFilePathToDelete); - if (fs) { - // delete default preview location - fs.rmSync(joinPath(previewSaveFolderPath, name), { recursive: true, force: true }); + // delete default preview location + fs.rmSync(joinPath(previewSaveFolderPath, name), { recursive: true, force: true }); - // delete default conversion location - fs.rmSync(joinPath(conversionSaveFolderPath, name), { recursive: true, force: true }); - } + // delete default conversion location + fs.rmSync(joinPath(conversionSaveFolderPath, name), { recursive: true, force: true }); return true; }; diff --git a/src/electron/frontend/core/progress/update.js b/src/electron/frontend/core/progress/update.js index c621427b67..a5ce4a0540 100644 --- a/src/electron/frontend/core/progress/update.js +++ b/src/electron/frontend/core/progress/update.js @@ -15,11 +15,8 @@ export const rename = (newDatasetName, previousDatasetName) => { // update old progress file with new dataset name const oldProgressFilePath = `${guidedProgressFilePath}/${previousDatasetName}.json`; const newProgressFilePath = `${guidedProgressFilePath}/${newDatasetName}.json`; - if (fs) fs.renameSync(oldProgressFilePath, newProgressFilePath); - else { - localStorage.setItem(newProgressFilePath, localStorage.getItem(oldProgressFilePath)); - localStorage.removeItem(oldProgressFilePath); - } + fs.renameSync(oldProgressFilePath, newProgressFilePath); + } else throw new Error("No previous project name provided"); }; @@ -56,9 +53,9 @@ export const updateFile = (projectName, callback) => { var guidedFilePath = joinPath(guidedProgressFilePath, projectName + ".json"); + console.log(guidedProgressFilePath) + // Save the file through the available mechanisms - if (fs) { - if (!fs.existsSync(guidedProgressFilePath)) fs.mkdirSync(guidedProgressFilePath, { recursive: true }); //create progress folder if one does not exist - fs.writeFileSync(guidedFilePath, JSON.stringify(data, null, 2)); - } else localStorage.setItem(guidedFilePath, JSON.stringify(data)); + if (!fs.existsSync(guidedProgressFilePath)) fs.mkdirSync(guidedProgressFilePath, { recursive: true }); //create progress folder if one does not exist + fs.writeFileSync(guidedFilePath, JSON.stringify(data, null, 2)); }; diff --git a/src/electron/frontend/core/server/index.ts b/src/electron/frontend/core/server/index.ts index e8b69b2292..aecc5b3f18 100644 --- a/src/electron/frontend/core/server/index.ts +++ b/src/electron/frontend/core/server/index.ts @@ -1,6 +1,8 @@ -import { isElectron, electron, app, port } from '../../utils/electron.js' +import { isElectron, electron, app } from '../../utils/electron.js' const { ipcRenderer } = electron; +import { isTestEnvironment } from '../globals.js' + import { notyf, } from '../dependencies.js' @@ -35,7 +37,6 @@ export async function pythonServerOpened() { if (openPythonStatusNotyf) notyf.dismiss(openPythonStatusNotyf) - const isTestEnvironment = globalThis?.process?.env?.VITEST if (isTestEnvironment) return openPythonStatusNotyf = notyf.open({ From a3ce1f7800374cebf6afb8c599e0ddfe31fd152a Mon Sep 17 00:00:00 2001 From: Garrett Michael Flynn Date: Wed, 29 May 2024 12:52:13 -0700 Subject: [PATCH 04/12] Update index.js --- src/electron/frontend/core/progress/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/electron/frontend/core/progress/index.js b/src/electron/frontend/core/progress/index.js index ce601970a3..aede8554b0 100644 --- a/src/electron/frontend/core/progress/index.js +++ b/src/electron/frontend/core/progress/index.js @@ -115,7 +115,7 @@ export const save = (page, overrides = {}) => { }; export const getEntries = () => { - if (fs && !fs.existsSync(guidedProgressFilePath)) fs.mkdirSync(guidedProgressFilePath, { recursive: true }); //Check if progress folder exists. If not, create it. + if (!fs.existsSync(guidedProgressFilePath)) fs.mkdirSync(guidedProgressFilePath, { recursive: true }); //Check if progress folder exists. If not, create it. const progressFiles = fs ? fs.readdirSync(guidedProgressFilePath) : Object.keys(localStorage); return progressFiles.filter((path) => path.slice(-5) === ".json"); }; From 895ffac2d9cd4db002370a879c7376c6ed5cc390 Mon Sep 17 00:00:00 2001 From: Garrett Michael Flynn Date: Wed, 29 May 2024 12:55:42 -0700 Subject: [PATCH 05/12] Update GuidedPathExpansion.js --- .../guided-mode/data/GuidedPathExpansion.js | 112 +++++++++--------- 1 file changed, 55 insertions(+), 57 deletions(-) diff --git a/src/electron/frontend/core/components/pages/guided-mode/data/GuidedPathExpansion.js b/src/electron/frontend/core/components/pages/guided-mode/data/GuidedPathExpansion.js index 20e4e07cc6..0680a45c77 100644 --- a/src/electron/frontend/core/components/pages/guided-mode/data/GuidedPathExpansion.js +++ b/src/electron/frontend/core/components/pages/guided-mode/data/GuidedPathExpansion.js @@ -381,76 +381,74 @@ export class GuidedPathExpansionPage extends Page { const interfaceName = parentPath.slice(-1)[0]; - if (fs) { - const baseDir = form.getFormElement([...parentPath, "base_directory"]); - if (name === "format_string_path") { - if (value && baseDir && !baseDir.value) { - return [ - { - message: html`A base directory must be provided to locate your files.`, - type: "error", - }, - ]; - } + const baseDir = form.getFormElement([...parentPath, "base_directory"]); + if (name === "format_string_path") { + if (value && baseDir && !baseDir.value) { + return [ + { + message: html`A base directory must be provided to locate your files.`, + type: "error", + }, + ]; + } - const base_directory = [...parentPath, "base_directory"].reduce( - (acc, key) => acc[key], - this.form.resolved - ); + const base_directory = [...parentPath, "base_directory"].reduce( + (acc, key) => acc[key], + this.form.resolved + ); - if (!base_directory) return true; // Do not calculate if base is not found + if (!base_directory) return true; // Do not calculate if base is not found - const entry = { base_directory }; + const entry = { base_directory }; - if (value.split(".").length > 1) entry.file_path = value; - else entry.folder_path = value; + if (value.split(".").length > 1) entry.file_path = value; + else entry.folder_path = value; - const results = await run( - `neuroconv/locate`, - { [interfaceName]: entry }, - { swal: false } - ).catch((error) => { - this.notify(error.message, "error"); - throw error; - }); + const results = await run( + `neuroconv/locate`, + { [interfaceName]: entry }, + { swal: false } + ).catch((error) => { + this.notify(error.message, "error"); + throw error; + }); - const resolved = []; + const resolved = []; - for (let sub in results) { - for (let ses in results[sub]) { - const source_data = results[sub][ses].source_data[interfaceName]; - const path = source_data.file_path ?? source_data.folder_path; - resolved.push(path.slice(base_directory.length + 1)); - } + for (let sub in results) { + for (let ses in results[sub]) { + const source_data = results[sub][ses].source_data[interfaceName]; + const path = source_data.file_path ?? source_data.folder_path; + resolved.push(path.slice(base_directory.length + 1)); } + } - if (resolved.length === 0) - return [ - { - message: html`No source files found using the provided information.`, - type: "warning", - }, - ]; - + if (resolved.length === 0) return [ { - message: html`

- Source Files Found for - ${interfaceName} -

- ${base_directory} - ${new List({ - items: resolved.map((path) => { - return { value: path }; - }), - editable: false, - })}`, - type: "info", + message: html`No source files found using the provided information.`, + type: "warning", }, ]; - } + + return [ + { + message: html`

+ Source Files Found for + ${interfaceName} +

+ ${base_directory} + ${new List({ + items: resolved.map((path) => { + return { value: path }; + }), + editable: false, + })}`, + type: "info", + }, + ]; } }, })); From 77c877032faba7f4af5769c96da32bff09101156 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 29 May 2024 19:58:13 +0000 Subject: [PATCH 06/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../guided-mode/data/GuidedPathExpansion.js | 17 +++++++---------- src/electron/frontend/core/globals.js | 14 +++++++------- src/electron/frontend/core/progress/update.js | 3 +-- 3 files changed, 15 insertions(+), 19 deletions(-) diff --git a/src/electron/frontend/core/components/pages/guided-mode/data/GuidedPathExpansion.js b/src/electron/frontend/core/components/pages/guided-mode/data/GuidedPathExpansion.js index 0680a45c77..af89aea7da 100644 --- a/src/electron/frontend/core/components/pages/guided-mode/data/GuidedPathExpansion.js +++ b/src/electron/frontend/core/components/pages/guided-mode/data/GuidedPathExpansion.js @@ -404,14 +404,12 @@ export class GuidedPathExpansionPage extends Page { if (value.split(".").length > 1) entry.file_path = value; else entry.folder_path = value; - const results = await run( - `neuroconv/locate`, - { [interfaceName]: entry }, - { swal: false } - ).catch((error) => { - this.notify(error.message, "error"); - throw error; - }); + const results = await run(`neuroconv/locate`, { [interfaceName]: entry }, { swal: false }).catch( + (error) => { + this.notify(error.message, "error"); + throw error; + } + ); const resolved = []; @@ -434,8 +432,7 @@ export class GuidedPathExpansionPage extends Page { return [ { message: html`

- Source Files Found for - ${interfaceName} + Source Files Found for ${interfaceName}

${base_directory} (path ? path.join(...args) : args.filter((str) => str).join("/")); @@ -23,19 +23,19 @@ const root = isTestEnvironment ? joinPath(paths.root, ".test") : paths.root; export const homeDirectory = os.homedir(); -export const appDirectory = joinPath(homeDirectory, root) +export const appDirectory = joinPath(homeDirectory, root); -export const guidedProgressFilePath = joinPath(appDirectory, ...paths.subfolders.progress) +export const guidedProgressFilePath = joinPath(appDirectory, ...paths.subfolders.progress); -export const previewSaveFolderPath = joinPath(appDirectory, ...paths.subfolders.preview) -export const conversionSaveFolderPath = joinPath(appDirectory, ...paths.subfolders.conversions) +export const previewSaveFolderPath = joinPath(appDirectory, ...paths.subfolders.preview); +export const conversionSaveFolderPath = joinPath(appDirectory, ...paths.subfolders.conversions); -export const testDataFolderPath = joinPath(appDirectory, ...paths.subfolders.testdata) +export const testDataFolderPath = joinPath(appDirectory, ...paths.subfolders.testdata); // Encryption const IV_LENGTH = 16; const KEY_LENGTH = 32; -export const ENCRYPTION_KEY = Buffer.concat([Buffer.from(appDirectory), Buffer.alloc(KEY_LENGTH)], KEY_LENGTH) +export const ENCRYPTION_KEY = Buffer.concat([Buffer.from(appDirectory), Buffer.alloc(KEY_LENGTH)], KEY_LENGTH); export const ENCRYPTION_IV = crypto.randomBytes(IV_LENGTH); diff --git a/src/electron/frontend/core/progress/update.js b/src/electron/frontend/core/progress/update.js index a5ce4a0540..f07ea80b19 100644 --- a/src/electron/frontend/core/progress/update.js +++ b/src/electron/frontend/core/progress/update.js @@ -16,7 +16,6 @@ export const rename = (newDatasetName, previousDatasetName) => { const oldProgressFilePath = `${guidedProgressFilePath}/${previousDatasetName}.json`; const newProgressFilePath = `${guidedProgressFilePath}/${newDatasetName}.json`; fs.renameSync(oldProgressFilePath, newProgressFilePath); - } else throw new Error("No previous project name provided"); }; @@ -53,7 +52,7 @@ export const updateFile = (projectName, callback) => { var guidedFilePath = joinPath(guidedProgressFilePath, projectName + ".json"); - console.log(guidedProgressFilePath) + console.log(guidedProgressFilePath); // Save the file through the available mechanisms if (!fs.existsSync(guidedProgressFilePath)) fs.mkdirSync(guidedProgressFilePath, { recursive: true }); //create progress folder if one does not exist From e8ce6a034b10b169d3e02acbacd18f16cf443283 Mon Sep 17 00:00:00 2001 From: Garrett Michael Flynn Date: Wed, 29 May 2024 14:28:27 -0700 Subject: [PATCH 07/12] Fix storybook --- src/electron/frontend/core/globals.js | 6 +++--- src/electron/frontend/utils/electron.js | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/electron/frontend/core/globals.js b/src/electron/frontend/core/globals.js index 941a1e26c6..51af1f821d 100644 --- a/src/electron/frontend/core/globals.js +++ b/src/electron/frontend/core/globals.js @@ -21,7 +21,7 @@ export const reloadPageToHome = () => { // Filesystem Management const root = isTestEnvironment ? joinPath(paths.root, ".test") : paths.root; -export const homeDirectory = os.homedir(); +export const homeDirectory = os ? os.homedir() : "/"; export const appDirectory = joinPath(homeDirectory, root); @@ -35,9 +35,9 @@ export const testDataFolderPath = joinPath(appDirectory, ...paths.subfolders.tes // Encryption const IV_LENGTH = 16; const KEY_LENGTH = 32; -export const ENCRYPTION_KEY = Buffer.concat([Buffer.from(appDirectory), Buffer.alloc(KEY_LENGTH)], KEY_LENGTH); +export const ENCRYPTION_KEY = isElectron ? Buffer.concat([Buffer.from(appDirectory), Buffer.alloc(KEY_LENGTH)], KEY_LENGTH) : "" -export const ENCRYPTION_IV = crypto.randomBytes(IV_LENGTH); +export const ENCRYPTION_IV = isElectron ? crypto.randomBytes(IV_LENGTH) : ""; // Storybook export const isStorybook = window.location.href.includes("iframe.html"); diff --git a/src/electron/frontend/utils/electron.js b/src/electron/frontend/utils/electron.js index 315402501f..b057112b9f 100644 --- a/src/electron/frontend/utils/electron.js +++ b/src/electron/frontend/utils/electron.js @@ -6,18 +6,18 @@ export const isElectron = userAgent.indexOf(" electron/") > -1; export const electron = globalThis.electron ?? {}; // ipcRenderer, remote, shell, etc. // Node Modules -export const fs = require("fs-extra"); // File System -export const os = require("os"); -export const crypto = require("crypto"); -export const path = require("path"); +export const fs = isElectron && require("fs-extra"); // File System +export const os = isElectron && require("os"); +export const crypto = isElectron && require("crypto"); +export const path = isElectron && require("path"); // Remote Electron Modules export const remote = isElectron ? require("@electron/remote") : {}; export const app = remote.app; // Electron Information -export const port = electron.ipcRenderer ? electron.ipcRenderer.sendSync("get-port") : 4242; -export const SERVER_FILE_PATH = electron.ipcRenderer ? electron.ipcRenderer.sendSync("get-server-file-path") : ""; +export const port = isElectron ? electron.ipcRenderer.sendSync("get-port") : 4242; +export const SERVER_FILE_PATH = isElectron ? electron.ipcRenderer.sendSync("get-server-file-path") : ""; // Link the renderer to the main process if (isElectron) { From 71840ddbd4bc4b066043a77c683e8fc939071f53 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 29 May 2024 21:29:37 +0000 Subject: [PATCH 08/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/electron/frontend/core/globals.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/electron/frontend/core/globals.js b/src/electron/frontend/core/globals.js index 51af1f821d..b9eb7eab45 100644 --- a/src/electron/frontend/core/globals.js +++ b/src/electron/frontend/core/globals.js @@ -35,7 +35,9 @@ export const testDataFolderPath = joinPath(appDirectory, ...paths.subfolders.tes // Encryption const IV_LENGTH = 16; const KEY_LENGTH = 32; -export const ENCRYPTION_KEY = isElectron ? Buffer.concat([Buffer.from(appDirectory), Buffer.alloc(KEY_LENGTH)], KEY_LENGTH) : "" +export const ENCRYPTION_KEY = isElectron + ? Buffer.concat([Buffer.from(appDirectory), Buffer.alloc(KEY_LENGTH)], KEY_LENGTH) + : ""; export const ENCRYPTION_IV = isElectron ? crypto.randomBytes(IV_LENGTH) : ""; From 0cda5c2085619bf9386c8e913b258295bf8948f1 Mon Sep 17 00:00:00 2001 From: Garrett Michael Flynn Date: Thu, 30 May 2024 08:41:22 -0700 Subject: [PATCH 09/12] Fix tests --- src/electron/frontend/core/globals.js | 6 ++++-- src/electron/frontend/utils/electron.js | 13 +++++++++---- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/electron/frontend/core/globals.js b/src/electron/frontend/core/globals.js index b9eb7eab45..d85aaa269c 100644 --- a/src/electron/frontend/core/globals.js +++ b/src/electron/frontend/core/globals.js @@ -1,10 +1,12 @@ -import { os, path, crypto, isElectron } from "../utils/electron.js"; +import { os, path, crypto, isElectron, isTestEnvironment } from "../utils/electron.js"; import paths from "../../../paths.config.json" assert { type: "json" }; import supportedInterfaces from "../../../supported_interfaces.json" assert { type: "json" }; -export const isTestEnvironment = globalThis?.process?.env?.VITEST; +export { + isTestEnvironment +} export const joinPath = (...args) => (path ? path.join(...args) : args.filter((str) => str).join("/")); diff --git a/src/electron/frontend/utils/electron.js b/src/electron/frontend/utils/electron.js index b057112b9f..199694302f 100644 --- a/src/electron/frontend/utils/electron.js +++ b/src/electron/frontend/utils/electron.js @@ -1,15 +1,20 @@ import { updateURLParams } from "./url.js"; +export const isTestEnvironment = globalThis?.process?.env?.VITEST; + const userAgent = navigator.userAgent.toLowerCase(); export const isElectron = userAgent.indexOf(" electron/") > -1; + +const hasNodeAccess = isElectron || isTestEnvironment; + export const electron = globalThis.electron ?? {}; // ipcRenderer, remote, shell, etc. // Node Modules -export const fs = isElectron && require("fs-extra"); // File System -export const os = isElectron && require("os"); -export const crypto = isElectron && require("crypto"); -export const path = isElectron && require("path"); +export const fs = hasNodeAccess && require("fs-extra"); // File System +export const os = hasNodeAccess && require("os"); +export const crypto = hasNodeAccess && require("crypto"); +export const path = hasNodeAccess && require("path"); // Remote Electron Modules export const remote = isElectron ? require("@electron/remote") : {}; From 1e711f493a53b5747067148c8e3d6481129a573c Mon Sep 17 00:00:00 2001 From: Garrett Michael Flynn Date: Thu, 30 May 2024 08:47:42 -0700 Subject: [PATCH 10/12] Scope auto-update better --- .../components/pages/settings/SettingsPage.js | 5 +++- src/electron/frontend/utils/auto-update.js | 25 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 src/electron/frontend/utils/auto-update.js diff --git a/src/electron/frontend/core/components/pages/settings/SettingsPage.js b/src/electron/frontend/core/components/pages/settings/SettingsPage.js index 732ba905de..dc0d47f558 100644 --- a/src/electron/frontend/core/components/pages/settings/SettingsPage.js +++ b/src/electron/frontend/core/components/pages/settings/SettingsPage.js @@ -21,9 +21,12 @@ import { path, port, fs, +} from "../../../../utils/electron.js"; + +import { onUpdateAvailable, onUpdateProgress, -} from "../../../../utils/electron.js"; +} from "../../../../utils/auto-update.js"; import saveSVG from "../../../../assets/icons/save.svg?raw"; import folderSVG from "../../../../assets/icons/folder_open.svg?raw"; diff --git a/src/electron/frontend/utils/auto-update.js b/src/electron/frontend/utils/auto-update.js new file mode 100644 index 0000000000..05696269c9 --- /dev/null +++ b/src/electron/frontend/utils/auto-update.js @@ -0,0 +1,25 @@ +let updateAvailable = false; +const updateAvailableCallbacks = []; +export const onUpdateAvailable = (callback) => { + if (updateAvailable) callback(updateAvailable); + else updateAvailableCallbacks.push(callback); +}; + +let updateProgress = null; + +const updateProgressCallbacks = []; +export const onUpdateProgress = (callback) => { + if (updateProgress) callback(updateProgress); + else updateProgressCallbacks.push(callback); +}; + +export const registerUpdateProgress = (info) => { + updateProgress = info; + updateProgressCallbacks.forEach((cb) => cb(info)); +}; + +export const registerUpdate = (info) => { + updateAvailable = info; + document.body.setAttribute("data-update-available", JSON.stringify(info)); + updateAvailableCallbacks.forEach((cb) => cb(info)); +}; From 6070862eb5652ab9963f80a11538ab6ff44ba574 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 30 May 2024 15:47:49 +0000 Subject: [PATCH 11/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/electron/frontend/core/globals.js | 4 +--- src/electron/frontend/utils/electron.js | 2 -- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/src/electron/frontend/core/globals.js b/src/electron/frontend/core/globals.js index d85aaa269c..0ca85c1a23 100644 --- a/src/electron/frontend/core/globals.js +++ b/src/electron/frontend/core/globals.js @@ -4,9 +4,7 @@ import paths from "../../../paths.config.json" assert { type: "json" }; import supportedInterfaces from "../../../supported_interfaces.json" assert { type: "json" }; -export { - isTestEnvironment -} +export { isTestEnvironment }; export const joinPath = (...args) => (path ? path.join(...args) : args.filter((str) => str).join("/")); diff --git a/src/electron/frontend/utils/electron.js b/src/electron/frontend/utils/electron.js index 463ee946b9..8531cfdb0a 100644 --- a/src/electron/frontend/utils/electron.js +++ b/src/electron/frontend/utils/electron.js @@ -6,7 +6,6 @@ export const isTestEnvironment = globalThis?.process?.env?.VITEST; const userAgent = navigator.userAgent.toLowerCase(); export const isElectron = userAgent.indexOf(" electron/") > -1; - const hasNodeAccess = isElectron || isTestEnvironment; export const electron = globalThis.electron ?? {}; // ipcRenderer, remote, shell, etc. @@ -50,5 +49,4 @@ if (isElectron) { electron.ipcRenderer.on(`update-complete`, (_, ...args) => console.log(`[Update]:`, ...args)); electron.ipcRenderer.on(`update-error`, (_, ...args) => console.log(`[Update]:`, ...args)); - } From 8e4b229cfa9e257c4fdc123159cc09f2dfbb3e07 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 30 May 2024 15:54:40 +0000 Subject: [PATCH 12/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../components/pages/settings/SettingsPage.js | 15 ++---- src/electron/frontend/utils/auto-update.js | 50 +++++++++---------- 2 files changed, 28 insertions(+), 37 deletions(-) diff --git a/src/electron/frontend/core/components/pages/settings/SettingsPage.js b/src/electron/frontend/core/components/pages/settings/SettingsPage.js index dc0d47f558..d5ea234eab 100644 --- a/src/electron/frontend/core/components/pages/settings/SettingsPage.js +++ b/src/electron/frontend/core/components/pages/settings/SettingsPage.js @@ -15,18 +15,9 @@ import { merge, setUndefinedIfNotDeclared } from "../utils"; import { notyf } from "../../../dependencies.js"; import { homeDirectory, testDataFolderPath } from "../../../globals.js"; -import { - SERVER_FILE_PATH, - electron, - path, - port, - fs, -} from "../../../../utils/electron.js"; - -import { - onUpdateAvailable, - onUpdateProgress, -} from "../../../../utils/auto-update.js"; +import { SERVER_FILE_PATH, electron, path, port, fs } from "../../../../utils/electron.js"; + +import { onUpdateAvailable, onUpdateProgress } from "../../../../utils/auto-update.js"; import saveSVG from "../../../../assets/icons/save.svg?raw"; import folderSVG from "../../../../assets/icons/folder_open.svg?raw"; diff --git a/src/electron/frontend/utils/auto-update.js b/src/electron/frontend/utils/auto-update.js index 05696269c9..4b4452ebde 100644 --- a/src/electron/frontend/utils/auto-update.js +++ b/src/electron/frontend/utils/auto-update.js @@ -1,25 +1,25 @@ -let updateAvailable = false; -const updateAvailableCallbacks = []; -export const onUpdateAvailable = (callback) => { - if (updateAvailable) callback(updateAvailable); - else updateAvailableCallbacks.push(callback); -}; - -let updateProgress = null; - -const updateProgressCallbacks = []; -export const onUpdateProgress = (callback) => { - if (updateProgress) callback(updateProgress); - else updateProgressCallbacks.push(callback); -}; - -export const registerUpdateProgress = (info) => { - updateProgress = info; - updateProgressCallbacks.forEach((cb) => cb(info)); -}; - -export const registerUpdate = (info) => { - updateAvailable = info; - document.body.setAttribute("data-update-available", JSON.stringify(info)); - updateAvailableCallbacks.forEach((cb) => cb(info)); -}; +let updateAvailable = false; +const updateAvailableCallbacks = []; +export const onUpdateAvailable = (callback) => { + if (updateAvailable) callback(updateAvailable); + else updateAvailableCallbacks.push(callback); +}; + +let updateProgress = null; + +const updateProgressCallbacks = []; +export const onUpdateProgress = (callback) => { + if (updateProgress) callback(updateProgress); + else updateProgressCallbacks.push(callback); +}; + +export const registerUpdateProgress = (info) => { + updateProgress = info; + updateProgressCallbacks.forEach((cb) => cb(info)); +}; + +export const registerUpdate = (info) => { + updateAvailable = info; + document.body.setAttribute("data-update-available", JSON.stringify(info)); + updateAvailableCallbacks.forEach((cb) => cb(info)); +};