| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | */ |
| 7 | |
| 8 | const {app, BrowserWindow, shell} = require('electron'); // Module to create native browser window. |
| 9 | const {join} = require('path'); |
| 10 | const os = require('os'); |
| 11 | |
| 12 | const argv = require('minimist')(process.argv.slice(2)); |
| 13 | const projectRoots = argv._; |
| 14 | |
| 15 | let mainWindow = null; |
| 16 | |
| 17 | app.on('window-all-closed', function () { |
| 18 | app.quit(); |
| 19 | }); |
| 20 | |
| 21 | app.on('ready', function () { |
| 22 | // Create the browser window. |
| 23 | mainWindow = new BrowserWindow({ |
| 24 | width: 800, |
| 25 | height: 600, |
| 26 | icon: join(__dirname, 'icons/icon128.png'), |
| 27 | frame: false, |
| 28 | //titleBarStyle: 'customButtonsOnHover', |
| 29 | webPreferences: { |
| 30 | contextIsolation: true, // protect against prototype pollution |
| 31 | enableRemoteModule: false, // turn off remote |
| 32 | sandbox: false, // allow preload script to access file system |
| 33 | preload: join(__dirname, 'preload.js'), // use a preload script to expose node globals |
| 34 | }, |
| 35 | }); |
| 36 | |
| 37 | // set dock icon for macos |
| 38 | if (os.platform() === 'darwin') { |
| 39 | app.dock.setIcon(join(__dirname, 'icons/icon128.png')); |
| 40 | } |
| 41 | |
| 42 | // https://stackoverflow.com/questions/32402327/ |
| 43 | mainWindow.webContents.setWindowOpenHandler(({url}) => { |
| 44 | shell.openExternal(url); |
| 45 | return {action: 'deny'}; |
| 46 | }); |
| 47 | |
| 48 | // and load the index.html of the app. |
| 49 | mainWindow.loadURL('file://' + __dirname + '/app.html'); |
| 50 | // $FlowFixMe[incompatible-use] found when upgrading Flow |
| 51 | mainWindow.webContents.executeJavaScript( |
| 52 | // We use this so that RN can keep relative JSX __source filenames |
| 53 | // but "click to open in editor" still works. js1 passes project roots |
| 54 | // as the argument to DevTools. |
| 55 | 'window.devtools.setProjectRoots(' + JSON.stringify(projectRoots) + ')', |
| 56 | ); |
| 57 | |
| 58 | // Emitted when the window is closed. |
| 59 | mainWindow.on('closed', function () { |
| 60 | mainWindow = null; |
| 61 | }); |
| 62 | }); |