[WIP] feature: add zcashd-child-process spawn on startup

This commit is contained in:
George Lima 2018-11-29 19:58:05 -03:00
parent d74fec7162
commit 854bd2e507
2 changed files with 53 additions and 4 deletions

View File

@ -8,9 +8,11 @@ import isDev from 'electron-is-dev';
/* eslint-enable import/no-extraneous-dependencies */
import type { BrowserWindow as BrowserWindowType } from 'electron';
import { registerDebugShortcut } from '../utils/debug-shortcut';
import { runDaemon, log as zcashLog } from './zcashd-child-process';
let mainWindow: BrowserWindowType;
let updateAvailable: boolean = false;
let zcashDaemon;
const showStatus = (text) => {
if (text === 'Update downloaded') updateAvailable = true;
@ -55,17 +57,29 @@ const createWindow = () => {
mainWindow.setVisibleOnAllWorkspaces(true);
registerDebugShortcut(app, mainWindow);
mainWindow.loadURL(isDev
? 'http://0.0.0.0:8080/'
: `file://${path.join(__dirname, '../build/index.html')}`);
mainWindow.loadURL(isDev ? 'http://0.0.0.0:8080/' : `file://${path.join(__dirname, '../build/index.html')}`);
exports.app = app;
};
app.on('ready', createWindow);
app.on('ready', () => {
createWindow();
runDaemon()
.then((proc) => {
if (proc) {
zcashDaemon = proc;
}
})
.catch(zcashLog);
});
app.on('activate', () => {
if (mainWindow === null) createWindow();
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit();
});
app.on('quit', () => {
if (zcashDaemon) {
zcashDaemon.kill('SIGINT');
}
});

View File

@ -0,0 +1,35 @@
// @flow
import cp from 'child_process';
import processExists from 'process-exists';
import type { ChildProcess } from 'child_process';
const getProcessName = () => 'zcashd';
/* eslint-disable no-console */
export const log = (...message: Array<*>) => console.log('[ZCash Daemon]', ...message);
/* eslint-enable no-console */
export const runDaemon: () => Promise<?ChildProcess> = () => new Promise((resolve, reject) => {
const process = getProcessName();
processExists(process).then((isRunning) => {
if (isRunning) {
log('Already is running!');
resolve();
} else {
const childProcess = cp.spawn(process, ['-daemon']);
childProcess.stdout.on('data', (data) => {
log(data.toString());
resolve(childProcess);
});
childProcess.stderr.on('data', (data) => {
log(data.toString());
reject(new Error(data.toString()));
});
childProcess.on('error', reject);
}
});
});