init
This commit is contained in:
3
src/config.json
Normal file
3
src/config.json
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"url": "http://localhost:3000"
|
||||
}
|
||||
22
src/environment.js
Normal file
22
src/environment.js
Normal file
@@ -0,0 +1,22 @@
|
||||
const { app } = require('electron');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const packageJson = require('../package.json');
|
||||
|
||||
let config, loaded = false, prod = app.isPackaged;
|
||||
const configPath = path.join(__dirname,'./config.json');
|
||||
|
||||
try {
|
||||
config = fs.readFileSync(configPath, 'utf-8');
|
||||
config = JSON.parse(config);
|
||||
loaded = true;
|
||||
} catch {
|
||||
config = {};
|
||||
}
|
||||
|
||||
module.exports = Object.assign({
|
||||
loaded,
|
||||
path: configPath,
|
||||
production: prod,
|
||||
version: packageJson.version,
|
||||
}, config);
|
||||
BIN
src/favicon.png
Normal file
BIN
src/favicon.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 11 KiB |
61
src/main.js
Normal file
61
src/main.js
Normal file
@@ -0,0 +1,61 @@
|
||||
const dns = require('dns');
|
||||
const env = require('./environment');
|
||||
const electron = require('electron');
|
||||
const fs = require('fs');
|
||||
const http = require('http');
|
||||
const https = require('https');
|
||||
const path = require('path');
|
||||
|
||||
let win;
|
||||
const iconPath = path.join(__dirname, 'favicon.png');
|
||||
|
||||
// Check internet connectivity
|
||||
async function online() {
|
||||
return new Promise(resolve => {
|
||||
dns.lookup('google.com', err => resolve(!err));
|
||||
});
|
||||
}
|
||||
|
||||
// Download icon
|
||||
async function downloadIcon() {
|
||||
if(!await online()) return Promise.resolve();
|
||||
const iconUrl = `${env.url}/favicon.png`;
|
||||
return new Promise((resolve, reject) => {
|
||||
const file = fs.createWriteStream(iconPath);
|
||||
const proto = env.url.startsWith('https://') ? https : http;
|
||||
proto.get(iconUrl, res => {
|
||||
if(res.statusCode !== 200) return; // Incase we are offline
|
||||
res.pipe(file);
|
||||
file.on('finish', () => file.close(resolve));
|
||||
}).on('error', reject);
|
||||
});
|
||||
}
|
||||
|
||||
async function createWindow() {
|
||||
await downloadIcon();
|
||||
win = new electron.BrowserWindow({
|
||||
width: 1000,
|
||||
height: 800,
|
||||
icon: iconPath,
|
||||
webPreferences: {
|
||||
preload: path.join(__dirname, 'preload.js'),
|
||||
contextIsolation: true,
|
||||
enableRemoteModule: false,
|
||||
nodeIntegration: false
|
||||
}
|
||||
});
|
||||
|
||||
win.setMenu(null);
|
||||
electron.globalShortcut.register('Control+Shift+I', () => {
|
||||
if (win) win.webContents.openDevTools();
|
||||
});
|
||||
|
||||
electron.ipcMain.on('electron-environment', (event) => event.returnValue = env);
|
||||
|
||||
win.loadURL(env.url);
|
||||
|
||||
win.on('closed', () => win = null);
|
||||
}
|
||||
|
||||
electron.app.on('ready', createWindow);
|
||||
electron.app.on('activate', () => { if(win === null) createWindow(); });
|
||||
3
src/preload.js
Normal file
3
src/preload.js
Normal file
@@ -0,0 +1,3 @@
|
||||
const {contextBridge, ipcRenderer} = require('electron');
|
||||
|
||||
contextBridge.exposeInMainWorld('electronEnvironment', () => ipcRenderer.sendSync('electron-environment'));
|
||||
Reference in New Issue
Block a user