Skip to content

Commit

Permalink
Feature: Add support for rndebugger: uri scheme on Linux (#726)
Browse files Browse the repository at this point in the history
* support xdg-open via requestSingleInstanceLock

* fix eslint errors
  • Loading branch information
crloz authored Jul 16, 2023
1 parent a6fd6a3 commit 3580468
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 12 deletions.
46 changes: 41 additions & 5 deletions electron/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { app, ipcMain, session, BrowserWindow, Menu } from 'electron';
import normalizeHeaderCase from 'header-case-normalizer';
import installExtensions from './extensions';
import { checkWindowInfo, createWindow } from './window';
import { startListeningHandleURL } from './url-handle';
import { startListeningHandleURL, handleURL, parseUrl } from './url-handle';
import { createMenuTemplate } from './menu';
import { readConfig } from './config';
import { sendSyncState } from './sync-state';
Expand All @@ -15,17 +15,45 @@ process.env.ELECTRON_DISABLE_SECURITY_WARNINGS = 1;
const iconPath = path.resolve(__dirname, 'logo.png');
const defaultOptions = { iconPath };

startListeningHandleURL(async (host, port) => {

const findWindow = async (host, port) => {
const wins = BrowserWindow.getAllWindows();
for (const win of wins) {
const { isWorkerRunning, isPortSettingRequired, location } = await checkWindowInfo(win);
if ((!isWorkerRunning || location.port === port) && !isPortSettingRequired) {
const { isWorkerRunning, isPortSettingRequired, location } =
await checkWindowInfo(win);
if (
(!isWorkerRunning || location.port === port) &&
!isPortSettingRequired
) {
if (win.isMinimized()) win.restore();
win.focus();
return win;
}
}
createWindow(defaultOptions);
return null;
});
};

const handleCommandLine = async (commandLine) => {
const url = commandLine.find((arg) => arg.startsWith('rndebugger://'));
if (!url) {
return;
}
await handleURL(findWindow, url);
};

if (process.platform === 'linux') {
const singleInstanceLock = app.requestSingleInstanceLock();
if (!singleInstanceLock) {
process.exit();
} else {
app.on('second-instance', async (event, commandLine) => {
await handleCommandLine(commandLine);
});
}
}

startListeningHandleURL(findWindow);

ipcMain.on('check-port-available', async (event, arg) => {
const port = Number(arg);
Expand Down Expand Up @@ -79,6 +107,14 @@ app.on('ready', async () => {
defaultRNPackagerPorts = [8081];
}

if (process.platform === 'linux') {
const url = process.argv.find((arg) => arg.startsWith('rndebugger://'));
const query = url ? parseUrl(url) : undefined;
if (query && query.port) {
defaultRNPackagerPorts = [query.port];
}
}

defaultRNPackagerPorts.forEach(port => {
createWindow({ port, ...defaultOptions });
});
Expand Down
14 changes: 10 additions & 4 deletions electron/url-handle/handleURL.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,23 @@ const filterPaths = list => {
return filteredList;
};

const handleURL = async (getWindow, path) => {
const route = url.parse(path);

export const parseUrl = _url => {
const route = url.parse(_url);
if (route.host !== 'set-debugger-loc') return;

const { host, port, projectRoots } = qs.parse(route.query);
const query = {
host: host || 'localhost',
port: Number(port) || 8081,
projectRoots: Array.isArray(projectRoots) ? filterPaths(projectRoots.split(',')) : undefined,
};
return query;
};

export const handleURL = async (getWindow, path) => {
const query = parseUrl(path);
if (!query) {
return;
}
const payload = JSON.stringify(query);

// This env will be get by new debugger window
Expand Down
4 changes: 2 additions & 2 deletions electron/url-handle/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import startListeningHandleURL from './handleURL';
import startListeningHandleURL, { handleURL, parseUrl } from './handleURL';
import * as port from './port';

export { startListeningHandleURL, port };
export { startListeningHandleURL, handleURL, parseUrl, port };
3 changes: 2 additions & 1 deletion scripts/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"dest": "release/",
"icon": "electron/logo.ico",
"categories": ["Utility"],
"lintianOverrides": ["changelog-file-missing-in-native-package"]
"lintianOverrides": ["changelog-file-missing-in-native-package"],
"mimeType": ["x-scheme-handler/rndebugger"]
}

0 comments on commit 3580468

Please sign in to comment.