electron app - auto find port

This commit is contained in:
Jan Prochazka
2020-04-10 21:21:57 +02:00
parent ac97344c1e
commit 5030c673e6
12 changed files with 88 additions and 56 deletions

View File

@@ -25,9 +25,10 @@
"homepage": "./",
"scripts": {
"start": "cross-env ELECTRON_START_URL=http://localhost:5000 electron .",
"start:local": "cross-env electron .",
"dist": "electron-builder",
"build": "cd ../packages/api && yarn build && cd ../web && yarn build:app && cd ../../app && yarn dist",
"wait-electron": "node src/electron-wait-react",
"build:local": "cd ../packages/api && yarn build && cd ../web && yarn build:app && cd ../../app && yarn predist",
"postinstall": "electron-builder install-app-deps",
"predist": "copyfiles ../packages/api/dist/* packages && copyfiles \"../packages/web/build/*\" packages && copyfiles \"../packages/web/build/**/*\" packages"
},

View File

@@ -1,27 +0,0 @@
const net = require('net');
const port = 5000;
process.env.ELECTRON_START_URL = `http://localhost:${port}`;
const client = new net.Socket();
let startedElectron = false;
const tryConnection = () => client.connect({port: port}, () => {
client.end();
if(!startedElectron) {
console.log('starting electron');
startedElectron = true;
const exec = require('child_process').exec;
const electron = exec('yarn electron');
electron.stdout.on("data", function(data) {
console.log("stdout: " + data.toString());
});
}
}
);
tryConnection();
client.on('error', (error) => {
setTimeout(tryConnection, 1000);
});

View File

@@ -14,18 +14,37 @@ let mainWindow;
function createWindow() {
// Create the browser window.
mainWindow = new BrowserWindow({ width: 800, height: 600 });
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
},
});
const apiProcess = fork(path.join(__dirname, '../packages/api/dist/bundle.js'));
function loadMainWindow() {
const startUrl =
process.env.ELECTRON_START_URL ||
url.format({
pathname: path.join(__dirname, '../packages/web/build/index.html'),
protocol: 'file:',
slashes: true,
});
mainWindow.loadURL(startUrl);
}
const startUrl =
process.env.ELECTRON_START_URL ||
url.format({
pathname: path.join(__dirname, '../packages/web/build/index.html'),
protocol: 'file:',
slashes: true,
if (process.env.ELECTRON_START_URL) {
loadMainWindow();
} else {
const apiProcess = fork(path.join(__dirname, '../packages/api/dist/bundle.js'), ['--dynport']);
apiProcess.on('message', (msg) => {
if (msg.msgtype == 'listening') {
const { port } = msg;
global['port'] = port;
loadMainWindow();
}
});
mainWindow.loadURL(startUrl);
}
// and load the index.html of the app.
// mainWindow.loadURL('http://localhost:3000');
@@ -34,7 +53,7 @@ function createWindow() {
// mainWindow.webContents.openDevTools();
// Emitted when the window is closed.
mainWindow.on('closed', function() {
mainWindow.on('closed', function () {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
@@ -48,7 +67,7 @@ function createWindow() {
app.on('ready', createWindow);
// Quit when all windows are closed.
app.on('window-all-closed', function() {
app.on('window-all-closed', function () {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
@@ -56,7 +75,7 @@ app.on('window-all-closed', function() {
}
});
app.on('activate', function() {
app.on('activate', function () {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {