Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

fix: move workspaces to Ganache/ui/workspaces #5151

Merged
merged 5 commits into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ if (process.platform === "win32") {
return spawn("cmd.exe", ["/c", "mkdir", path.join(USERDATA_PATH, "extras")])
})
.then(()=> {
return spawn("cmd.exe", ["/c", "mkdir", path.join(USERDATA_PATH, "workspaces")])
return spawn("cmd.exe", ["/c", "mkdir", path.join(USERDATA_PATH, "ui/workspaces")])
})
.then(()=> {
return spawn("cmd.exe", ["/c", "mkdir", path.join(USERDATA_PATH, "default")])
Comment on lines -100 to 103
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This magic "spawn" logic is here to ensure that real directories are created (required to be able to execute "extras" [such as postgres] in correct context). It may be removed in a subsequent change.

Expand All @@ -120,7 +120,7 @@ if (process.platform === "win32") {
}
});
} else {
migrationPromise = Promise.resolve();
migrationPromise = migration.migrate(USERDATA_PATH);

// https://github.com/sindresorhus/fix-path
// GUI apps on macOS don't inherit the $PATH defined in your dotfiles (.bashrc/.bash_profile/.zshrc/etc)
Expand Down
33 changes: 28 additions & 5 deletions src/main/init/migration.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
import { join } from "path";
import { copy, pathExists as exists, readdir } from "fs-extra";
import { copy, pathExists as exists, readdir, symlink, existsSync } from "fs-extra";
import { exec } from "child_process";
import * as pkg from "../../../package.json";
import {readdirSync} from "fs";

let migrate, uninstallOld;

const linkLegacyWorkspaces = async (configRoot) => {
const legacyWorkspacesDirectory = join(configRoot, "workspaces");
const newWorkspacesDirectory = join(configRoot, "ui/workspaces");

if (existsSync(legacyWorkspacesDirectory)) {
const legacyWorkspaces = readdirSync(legacyWorkspacesDirectory, { withFileTypes: true });
const linkingWorkspaces = legacyWorkspaces.map(legacyWorkspace => {
const fullPath = join(legacyWorkspacesDirectory, legacyWorkspace.name);
const linkPath = join(newWorkspacesDirectory, legacyWorkspace.name);
if (legacyWorkspace.isDirectory && !existsSync(linkPath)) {
return symlink(fullPath, linkPath);
}
});

return Promise.all(linkingWorkspaces);
}
};


if (process.platform == "win32") {
const APP_DATA = process.env.APPDATA;
const COPY_SETTINGS = {
Expand Down Expand Up @@ -40,7 +61,7 @@ if (process.platform == "win32") {
await Promise.all(promises);
}

const getOldGanachePath = ()=>{
const getOldGanachePath = () => {
return join(APP_DATA, "/../Local/Packages/Ganache_zh355ej5cj694/LocalCache/Roaming/Ganache");
}

Expand All @@ -62,7 +83,8 @@ if (process.platform == "win32") {
if (!(await ganacheExists())) return;

const newGanacheVirtualized = join(APP_DATA, `/../Local/Packages/${pkg.build.appx.identityName}_5dg5pnz03psnj/LocalCache/Roaming/Ganache`);
return Promise.all([moveWorkspaces(oldGanache, newGanache), moveGlobalSettings(oldGanache, newGanache), moveWorkspaces(newGanacheVirtualized, newGanache), moveGlobalSettings(newGanacheVirtualized, newGanache)]);
await Promise.all([moveWorkspaces(oldGanache, newGanache), moveGlobalSettings(oldGanache, newGanache), moveWorkspaces(newGanacheVirtualized, newGanache), moveGlobalSettings(newGanacheVirtualized, newGanache)]);
return linkLegacyWorkspaces(newGanache);
};

uninstallOld = async () => {
Expand All @@ -79,10 +101,11 @@ if (process.platform == "win32") {
}
} else {
const noop = () => Promise.resolve();
migrate = uninstallOld = noop;
migrate = linkLegacyWorkspaces;
uninstallOld = noop;
}

export default {
migrate,
uninstallOld
};
}
3 changes: 1 addition & 2 deletions src/main/types/workspaces/Workspace.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import path from "path";
import fse from "fs-extra";

import WorkspaceSettings from "../settings/WorkspaceSettings";
import ContractCache from "../../../integrations/ethereum/main/types/contracts/ContractCache";

Expand Down Expand Up @@ -54,7 +53,7 @@ class Workspace {
return path.join(configDirectory, `default_${flavor}`);
}
} else {
return path.join(configDirectory, "workspaces", sanitizedName);
return path.join(configDirectory, "ui/workspaces", sanitizedName);
}
}

Expand Down
24 changes: 14 additions & 10 deletions src/main/types/workspaces/WorkspaceManager.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from "path";
import fse from "fs-extra";
import { readdirSync } from "fs";
import Workspace from "./Workspace";
import WorkspaceSettings from "../settings/WorkspaceSettings";

Expand All @@ -9,30 +10,33 @@ class WorkspaceManager {
this.workspaces = [];
}

enumerateWorkspaces() {
const workspacesDirectory = path.join(this.directory, "workspaces");
enumerateWorkspaces() {
const workspacesDirectory = path.join(this.directory, "ui/workspaces");

if (fse.existsSync(workspacesDirectory)) {
this.workspaces = fse.readdirSync(workspacesDirectory).flatMap((file) => {
this.workspaces = readdirSync(workspacesDirectory, { withFileTypes: true }).flatMap((file) => {
// if an osx user navigates to the workspaces directory osx will put a
// .DS_Store folder there, ignore and delete these. If the file isn't
// a directory, also delete it.

if (
file === ".DS_Store" ||
!fse.lstatSync(path.join(workspacesDirectory, file)).isDirectory()
file.name === ".DS_Store" ||
!file.isDirectory()
&& !file.isSymbolicLink()
) {
try {
// remove files and folders that aren't allow in the workspaces
// directory
fse.removeSync(path.join(workspacesDirectory, file));
fse.removeSync(path.join(workspacesDirectory, file.name));
} catch {
// ignore
}
return [];
}

let settings = new WorkspaceSettings(
path.join(workspacesDirectory, file),
path.join(workspacesDirectory, file, "chaindata")
path.join(workspacesDirectory, file.name),
path.join(workspacesDirectory, file.name, "chaindata")
);

const isQuickstart = settings.get("isDefault");
Expand All @@ -46,12 +50,12 @@ class WorkspaceManager {

const name = settings.get("name");
const sanitizedName = Workspace.getSanitizedName(name);
if (sanitizedName !== file) {
if (sanitizedName !== file.name) {
// apparently the Settings file has a name that is not equal to the directory,
// we need to move the directory
try {
fse.moveSync(
path.join(workspacesDirectory, file),
path.join(workspacesDirectory, file.name),
path.join(workspacesDirectory, sanitizedName)
);
} catch (e) {
Expand Down