From a18fb0bd2a13c8d327668275e93c74edcb3e6e08 Mon Sep 17 00:00:00 2001 From: Juan Cazala Date: Fri, 13 Sep 2024 15:37:33 -0300 Subject: [PATCH] feat: preview with explorer alpha (#95) --- packages/main/src/modules/cli.ts | 11 ++--------- packages/renderer/src/hooks/useEditor.ts | 8 ++------ .../renderer/src/modules/store/editor/slice.ts | 18 +----------------- packages/shared/types/ipc.ts | 2 +- 4 files changed, 6 insertions(+), 33 deletions(-) diff --git a/packages/main/src/modules/cli.ts b/packages/main/src/modules/cli.ts index a8474381..3e3b355c 100644 --- a/packages/main/src/modules/cli.ts +++ b/packages/main/src/modules/cli.ts @@ -17,18 +17,11 @@ export async function start(path: string) { } const installCommand = run('npm', 'npm', { args: ['install', '--loglevel', 'error'], cwd: path }); await installCommand.wait(); - const port = await getAvailablePort(); previewServer = run('@dcl/sdk-commands', 'sdk-commands', { - args: ['start', '--port', port.toString(), '--no-browser'], + args: ['start', '--explorer-alpha'], cwd: path, }); - const message = await previewServer.waitFor(/available/i); - const match = message.match(/http:\/\/(\d|\.)+:\d+\?(.*)\n/); // match url printed by success message - if (match) { - return match[0].slice(0, -1); // remove last char because it's a new line '\n' - } else { - return `http://localhost:${port}`; // if match fails fallback to localhost and port, it should never happen unless the message from the CLI is changed, and the regex is not updated - } + await previewServer.waitFor(/decentraland:\/\//i); } export let deployServer: Child | null = null; diff --git a/packages/renderer/src/hooks/useEditor.ts b/packages/renderer/src/hooks/useEditor.ts index f8c014cf..2b75cf49 100644 --- a/packages/renderer/src/hooks/useEditor.ts +++ b/packages/renderer/src/hooks/useEditor.ts @@ -41,13 +41,9 @@ export const useEditor = () => { const openPreview = useCallback(() => { if (project) { - if (editor.previewUrl) { - dispatch(editorActions.openPreview(editor.previewUrl)); - } else { - dispatch(editorActions.runSceneAndOpenPreview(project)); - } + dispatch(editorActions.runScene(project.path)); } - }, [editorActions.openPreview, editorActions.runSceneAndOpenPreview, project, editor.previewUrl]); + }, [project, editorActions.runScene]); const openCode = useCallback(() => { if (project) { diff --git a/packages/renderer/src/modules/store/editor/slice.ts b/packages/renderer/src/modules/store/editor/slice.ts index 5fc93866..4a5c0526 100644 --- a/packages/renderer/src/modules/store/editor/slice.ts +++ b/packages/renderer/src/modules/store/editor/slice.ts @@ -1,8 +1,6 @@ import { editor, misc } from '#preload'; import { createAsyncThunk, createSlice, type PayloadAction } from '@reduxjs/toolkit'; -import { type ThunkAction } from '#store'; - import { type Project } from '/shared/types/projects'; import { actions as workspaceActions } from '../workspace'; @@ -13,12 +11,6 @@ export const startInspector = createAsyncThunk('editor/startInspector', editor.s export const runScene = createAsyncThunk('editor/runScene', editor.runScene); export const publishScene = createAsyncThunk('editor/publishScene', editor.publishScene); export const openPreview = createAsyncThunk('editor/openPreview', misc.openExternal); -export const runSceneAndOpenPreview: (project: Project) => ThunkAction = - project => async dispatch => { - const action = dispatch(runScene(project.path)); - const url = await action.unwrap(); - await dispatch(openPreview(url)); - }; export const openTutorial = createAsyncThunk('editor/openTutorial', editor.openTutorial); // state @@ -27,7 +19,6 @@ export type EditorState = { project?: Project; inspectorPort: number; publishPort: number; - previewUrl: string; loadingInspector: boolean; loadingPublish: boolean; loadingPreview: boolean; @@ -41,7 +32,6 @@ const initialState: EditorState = { version: null, inspectorPort: 0, publishPort: 0, - previewUrl: '', loadingInspector: false, loadingPublish: false, loadingPreview: false, @@ -60,7 +50,6 @@ export const slice = createSlice({ reducers: { setProject: (state, { payload: project }: PayloadAction) => { state.project = project; - state.previewUrl = ''; }, }, extraReducers: builder => { @@ -93,7 +82,6 @@ export const slice = createSlice({ }); builder.addCase(workspaceActions.createProject.fulfilled, (state, action) => { state.project = action.payload; - state.previewUrl = ''; }); builder.addCase(install.pending, state => { state.isInstalling = true; @@ -123,15 +111,12 @@ export const slice = createSlice({ } }); builder.addCase(runScene.pending, state => { - state.previewUrl = ''; state.loadingPreview = true; }); - builder.addCase(runScene.fulfilled, (state, { payload: port }) => { - state.previewUrl = port; + builder.addCase(runScene.fulfilled, state => { state.loadingPreview = false; }); builder.addCase(runScene.rejected, state => { - state.previewUrl = ''; state.loadingPreview = false; }); }, @@ -146,7 +131,6 @@ export const actions = { runScene, publishScene, openPreview, - runSceneAndOpenPreview, openTutorial, }; export const reducer = slice.reducer; diff --git a/packages/shared/types/ipc.ts b/packages/shared/types/ipc.ts index baa0b774..955fb433 100644 --- a/packages/shared/types/ipc.ts +++ b/packages/shared/types/ipc.ts @@ -11,7 +11,7 @@ export interface Ipc { 'bin.install': () => Promise; 'bin.code': (path: string) => Promise; 'cli.init': (path: string, repo?: string) => Promise; - 'cli.start': (path: string) => Promise; + 'cli.start': (path: string) => Promise; 'cli.deploy': (opts: DeployOptions) => Promise; 'analytics.track': (event: string, data?: Record) => void; 'analytics.getUserId': () => Promise;