2018-11-26 12:08:52 -08:00
|
|
|
// @flow
|
2018-12-03 18:34:41 -08:00
|
|
|
import '@babel/polyfill';
|
2018-12-15 13:45:48 -08:00
|
|
|
import dotenv from 'dotenv';
|
2018-12-03 18:34:41 -08:00
|
|
|
|
2018-11-26 12:19:12 -08:00
|
|
|
import path from 'path';
|
2018-11-26 12:08:52 -08:00
|
|
|
|
2018-11-26 12:19:12 -08:00
|
|
|
/* eslint-disable import/no-extraneous-dependencies */
|
2019-01-28 16:34:07 -08:00
|
|
|
import { app, BrowserWindow, typeof BrowserWindow as BrowserWindowType } from 'electron';
|
2018-11-26 12:19:12 -08:00
|
|
|
import { autoUpdater } from 'electron-updater';
|
|
|
|
import isDev from 'electron-is-dev';
|
2018-12-03 18:34:41 -08:00
|
|
|
import eres from 'eres';
|
2018-11-26 17:34:14 -08:00
|
|
|
import { registerDebugShortcut } from '../utils/debug-shortcut';
|
2018-11-30 17:33:29 -08:00
|
|
|
import runDaemon from './daemon/zcashd-child-process';
|
|
|
|
import zcashLog from './daemon/logger';
|
2018-12-13 13:36:50 -08:00
|
|
|
import getZecPrice from '../services/zec-price';
|
|
|
|
import store from './electron-store';
|
2018-11-26 12:08:52 -08:00
|
|
|
|
2018-12-15 13:45:48 -08:00
|
|
|
dotenv.config();
|
|
|
|
|
2018-11-26 12:19:12 -08:00
|
|
|
let mainWindow: BrowserWindowType;
|
2018-11-26 17:34:14 -08:00
|
|
|
let updateAvailable: boolean = false;
|
2018-11-29 14:58:05 -08:00
|
|
|
let zcashDaemon;
|
2018-11-26 12:08:52 -08:00
|
|
|
|
2018-11-26 12:19:12 -08:00
|
|
|
const showStatus = (text) => {
|
|
|
|
if (text === 'Update downloaded') updateAvailable = true;
|
2018-11-26 12:08:52 -08:00
|
|
|
|
2019-01-28 16:34:07 -08:00
|
|
|
if (mainWindow) {
|
|
|
|
mainWindow.webContents.send('update', {
|
|
|
|
updateAvailable,
|
|
|
|
updateInfo: text,
|
|
|
|
});
|
|
|
|
}
|
2018-11-26 12:19:12 -08:00
|
|
|
};
|
2018-11-26 12:08:52 -08:00
|
|
|
|
2018-11-26 13:21:25 -08:00
|
|
|
const createWindow = () => {
|
2018-11-26 12:19:12 -08:00
|
|
|
autoUpdater.checkForUpdatesAndNotify();
|
|
|
|
|
|
|
|
autoUpdater.on('checking-for-update', () => showStatus('Checking for update'));
|
|
|
|
autoUpdater.on('update-available', () => showStatus('Update available'));
|
|
|
|
autoUpdater.on('update-not-available', () => showStatus('No updates available'));
|
|
|
|
autoUpdater.on('error', err => showStatus(`Error while updating: ${err}`));
|
|
|
|
|
|
|
|
autoUpdater.on('download-progress', progress => showStatus(
|
|
|
|
/* eslint-disable-next-line max-len */
|
2019-01-28 16:34:07 -08:00
|
|
|
`Download speed: ${progress.bytesPerSecond} - Downloaded ${progress.percent}% (${
|
|
|
|
progress.transferred
|
|
|
|
}/${progress.total})`,
|
2018-11-26 12:19:12 -08:00
|
|
|
));
|
2018-11-26 12:08:52 -08:00
|
|
|
autoUpdater.on('update-downloaded', () => {
|
2018-11-26 12:19:12 -08:00
|
|
|
updateAvailable = true;
|
|
|
|
showStatus('Update downloaded');
|
|
|
|
});
|
2018-11-26 12:08:52 -08:00
|
|
|
|
|
|
|
mainWindow = new BrowserWindow({
|
2019-01-20 09:04:01 -08:00
|
|
|
width: 1000,
|
2019-01-25 13:57:12 -08:00
|
|
|
height: 660,
|
2018-11-26 17:34:14 -08:00
|
|
|
transparent: false,
|
|
|
|
frame: true,
|
2018-11-26 12:08:52 -08:00
|
|
|
resizable: true,
|
|
|
|
webPreferences: {
|
|
|
|
devTools: true,
|
|
|
|
webSecurity: false,
|
|
|
|
},
|
2018-11-26 12:19:12 -08:00
|
|
|
});
|
2018-11-26 12:08:52 -08:00
|
|
|
|
2018-12-13 13:36:50 -08:00
|
|
|
getZecPrice().then((obj) => {
|
2019-01-28 16:34:07 -08:00
|
|
|
store.set('ZEC_DOLLAR_PRICE', String(obj.USD));
|
2018-12-13 13:36:50 -08:00
|
|
|
});
|
|
|
|
|
2018-11-26 12:19:12 -08:00
|
|
|
mainWindow.setVisibleOnAllWorkspaces(true);
|
|
|
|
registerDebugShortcut(app, mainWindow);
|
2018-11-26 12:08:52 -08:00
|
|
|
|
2018-12-15 07:10:39 -08:00
|
|
|
mainWindow.loadURL(
|
2019-01-28 16:34:07 -08:00
|
|
|
isDev ? 'http://0.0.0.0:8080/' : `file://${path.join(__dirname, '../build/index.html')}`,
|
2018-12-15 07:10:39 -08:00
|
|
|
);
|
2018-11-26 12:08:52 -08:00
|
|
|
|
2018-11-26 12:19:12 -08:00
|
|
|
exports.app = app;
|
2018-12-05 08:34:49 -08:00
|
|
|
exports.mainWindow = mainWindow;
|
2018-11-26 13:21:25 -08:00
|
|
|
};
|
2018-11-26 12:08:52 -08:00
|
|
|
|
2018-12-03 18:34:41 -08:00
|
|
|
/* eslint-disable-next-line consistent-return */
|
|
|
|
app.on('ready', async () => {
|
2018-11-29 14:58:05 -08:00
|
|
|
createWindow();
|
2019-01-23 07:41:25 -08:00
|
|
|
|
|
|
|
if (process.env.NODE_ENV === 'test') {
|
|
|
|
zcashLog('Not running daemon, please run the mock API');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-03 18:34:41 -08:00
|
|
|
const [err, proc] = await eres(runDaemon());
|
|
|
|
|
|
|
|
if (err || !proc) return zcashLog(err);
|
|
|
|
|
2018-12-04 08:28:02 -08:00
|
|
|
/* eslint-disable-next-line */
|
2018-12-15 13:28:36 -08:00
|
|
|
zcashLog(`Zcash Daemon running. PID: ${proc.pid}`);
|
2018-12-04 08:28:02 -08:00
|
|
|
|
2018-12-03 18:34:41 -08:00
|
|
|
zcashDaemon = proc;
|
2018-11-29 14:58:05 -08:00
|
|
|
});
|
2018-11-26 12:08:52 -08:00
|
|
|
app.on('activate', () => {
|
2018-11-26 12:19:12 -08:00
|
|
|
if (mainWindow === null) createWindow();
|
|
|
|
});
|
2018-11-26 12:08:52 -08:00
|
|
|
app.on('window-all-closed', () => {
|
2018-11-26 12:19:12 -08:00
|
|
|
if (process.platform !== 'darwin') app.quit();
|
|
|
|
});
|
2018-11-30 21:21:43 -08:00
|
|
|
app.on('before-quit', () => {
|
2018-12-04 08:28:02 -08:00
|
|
|
if (zcashDaemon) {
|
2018-12-15 13:28:36 -08:00
|
|
|
zcashLog('Graceful shutdown Zcash Daemon, this may take a few seconds.');
|
2018-12-04 08:28:02 -08:00
|
|
|
zcashDaemon.kill('SIGINT');
|
|
|
|
}
|
2018-11-29 14:58:05 -08:00
|
|
|
});
|