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

@@ -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) {