Skip to content

Commit

Permalink
Fix temporary project startup regression (#196)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartolomej authored Sep 29, 2023
1 parent 9278224 commit b740dff
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 27 deletions.
26 changes: 16 additions & 10 deletions frontend/src/targets/electron/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from "@flowser/backend";
import { INestApplication } from "@nestjs/common";
import { Logger } from "./services/logger.service";
import crypto from "crypto";

export class FlowserBackend {
private static instance: FlowserBackend;
Expand Down Expand Up @@ -55,20 +56,25 @@ export class FlowserBackend {
this.app?.close();
}

public getDefaultProject(): ProjectEntity {
public async startTemporaryProject(options: {
filesystemPath: string;
}): Promise<ProjectEntity> {
const projectService = this.app?.get(ProjectsService);
if (!projectService) {
throw new Error("App not initialized");
}
const defaultProjectProto = projectService?.getDefaultProject();
return ProjectEntity.create(defaultProjectProto);
}

public async startTemporaryProject(project: ProjectEntity): Promise<void> {
const projectService = this.app?.get(ProjectsService);
if (!projectService) {
throw new Error("App not initialized");
}
await projectService?.useProject(project);
const defaultProjectProto = projectService.getDefaultProject();

const temporaryProject = ProjectEntity.create({
id: crypto.randomBytes(20).toString("utf-8"),
name: "Flow CLI project",
filesystemPath: options.filesystemPath,
gateway: defaultProjectProto.gateway,
emulator: defaultProjectProto.emulator,
startBlockHeight: 0,
});

return projectService.useProject(temporaryProject);
}
}
42 changes: 25 additions & 17 deletions frontend/src/targets/electron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { SentryMainService } from "./services/sentry-main.service";
import { setupMenu } from "./menu";
import { ServiceRegistry } from "./services/service-registry";
import { FlowserBackend } from "./backend";
import { ProjectEntity } from "@flowser/backend";

fixPath();

Expand Down Expand Up @@ -43,7 +44,15 @@ async function createWindow() {

win.loadURL(getClientAppUrl());

await handleBackendStart();
await startBackend();

const temporaryProject = await maybeStartTemporaryProject();

if (temporaryProject !== undefined) {
// Our react-router instance is configured to use hash-based navigation:
// https://reactrouter.com/en/main/routers/create-hash-router.
win.loadURL(`${getClientAppUrl()}#/projects/${temporaryProject.id}`);
}
}

app.on("ready", () => {
Expand Down Expand Up @@ -103,7 +112,7 @@ app.on("before-quit", async function (e) {
}
});

async function handleBackendStart() {
async function startBackend() {
const backend = FlowserBackend.getInstance();

try {
Expand All @@ -115,31 +124,28 @@ async function handleBackendStart() {
await handleBackendError({
error,
window: win,
onRestart: handleBackendStart,
onRestart: startBackend,
onQuit: app.quit,
});
}
}

async function maybeStartTemporaryProject(): Promise<
ProjectEntity | undefined
> {
const backend = FlowserBackend.getInstance();

try {
const { hasSwitch, getSwitchValue } = app.commandLine;

const temporaryProjectFlags = {
projectPath: "project-path",
};
const temporaryProjectPathFlag = "project-path";

const shouldStartTemporaryProject = hasSwitch(
temporaryProjectFlags.projectPath
);
const shouldStartTemporaryProject = hasSwitch(temporaryProjectPathFlag);

if (shouldStartTemporaryProject) {
const project = backend.getDefaultProject();

project.name = "Flow CLI project";
project.filesystemPath = getSwitchValue(
temporaryProjectFlags.projectPath
);

await backend.startTemporaryProject(project);
return backend.startTemporaryProject({
filesystemPath: getSwitchValue(temporaryProjectPathFlag),
});
}
} catch (e: unknown) {
const result = await dialog.showMessageBox(win, {
Expand All @@ -153,6 +159,8 @@ async function handleBackendStart() {
app.exit(1);
}
}

return undefined;
}

async function showDirectoryPicker() {
Expand Down

0 comments on commit b740dff

Please sign in to comment.