Skip to content

Commit

Permalink
Merge pull request #803 from NeurodataWithoutBorders/flatten-electron…
Browse files Browse the repository at this point in the history
…-file

Flatten Electron file
  • Loading branch information
CodyCBakerPhD authored May 30, 2024
2 parents 8dfe3c9 + 8e4b229 commit 8e51691
Show file tree
Hide file tree
Showing 12 changed files with 151 additions and 172 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
"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)\"",
"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\"",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,76 +381,71 @@ 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) => {
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` <h4 style="margin: 0;">
<span style="margin-right: 7px;"></span>Source Files Found for
${interfaceName}
</h4>
<small>${base_directory}</small>
<small
>${new List({
items: resolved.map((path) => {
return { value: path };
}),
editable: false,
})}</small
>`,
type: "info",
message: html`No source files found using the provided information.`,
type: "warning",
},
];
}

return [
{
message: html` <h4 style="margin: 0;">
<span style="margin-right: 7px;"></span>Source Files Found for ${interfaceName}
</h4>
<small>${base_directory}</small>
<small
>${new List({
items: resolved.map((path) => {
return { value: path };
}),
editable: false,
})}</small
>`,
type: "info",
},
];
}
},
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +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,
onUpdateAvailable,
onUpdateProgress,
} from "../../../../utils/electron.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";
Expand Down
27 changes: 16 additions & 11 deletions src/electron/frontend/core/globals.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { app, 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 { isTestEnvironment };

export const joinPath = (...args) => (path ? path.join(...args) : args.filter((str) => str).join("/"));

export let runOnLoad = (fn) => {
Expand All @@ -17,24 +19,27 @@ 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 ? 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
export const ENCRYPTION_KEY = isElectron
? Buffer.concat([Buffer.from(appDirectory), Buffer.alloc(KEY_LENGTH)], KEY_LENGTH)
: null;
: "";

export const ENCRYPTION_IV = crypto ? crypto.randomBytes(IV_LENGTH) : null;
export const ENCRYPTION_IV = isElectron ? crypto.randomBytes(IV_LENGTH) : "";

// Storybook
export const isStorybook = window.location.href.includes("iframe.html");
Expand Down
4 changes: 2 additions & 2 deletions src/electron/frontend/core/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -55,8 +57,6 @@ async function isOnline() {

statusBar.items[1].status = true

const isTestEnvironment = globalThis?.process?.env?.VITEST

if (isTestEnvironment) return

notyf.open({
Expand Down
6 changes: 3 additions & 3 deletions src/electron/frontend/core/progress/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
ENCRYPTION_KEY,
ENCRYPTION_IV,
} from "../globals.js";

import { fs, crypto } from "../../utils/electron.js";

import { joinPath, runOnLoad } from "../globals";
Expand Down Expand Up @@ -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));
}
}

Expand All @@ -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");
};
Expand Down
14 changes: 5 additions & 9 deletions src/electron/frontend/core/progress/operations.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
14 changes: 5 additions & 9 deletions src/electron/frontend/core/progress/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@ 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");
};

Expand Down Expand Up @@ -56,9 +52,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));
};
5 changes: 3 additions & 2 deletions src/electron/frontend/core/server/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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({
Expand Down
25 changes: 25 additions & 0 deletions src/electron/frontend/utils/auto-update.js
Original file line number Diff line number Diff line change
@@ -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));
};
Loading

0 comments on commit 8e51691

Please sign in to comment.