Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright 2015-2018 Parity Technologies (UK) Ltd.
// This file is part of Parity.
//
// SPDX-License-Identifier: MIT
const { app, dialog, webContents } = require('electron');
const flatten = require('lodash/flatten');
const fs = require('fs');
const { spawn } = require('child_process');
const util = require('util');
const cli = require('../cli');
const handleError = require('./handleError');
const parityPath = require('../utils/parityPath');
const [, parityArgv] = cli();
const fsExists = util.promisify(fs.stat);
const fsReadFile = util.promisify(fs.readFile);
const fsUnlink = util.promisify(fs.unlink);
let parity = null; // Will hold the running parity instance
module.exports = {
runParity(mainWindow) {
// Create a logStream to save logs
const logFile = `${parityPath()}.log`;
fsExists(logFile)
.then(() => fsUnlink(logFile))
.catch(() => {})
.then(() => {
var logStream = fs.createWriteStream(logFile, { flags: 'a' });
// Run an instance of parity if we receive the `run-parity` message
parity = spawn(
parityPath(),
flatten(
Object.keys(parityArgv).map(key => [`--${key}`, parityArgv[key]]) // Transform {arg: value} into [--arg, value]
)
.concat('--light')
.filter(value => value !== true) // --arg true is equivalent to --arg
);
parity.stdout.pipe(logStream);
parity.stderr.pipe(logStream);
parity.on('error', err => {
throw new Error(err);
});
parity.on('close', (exitCode, signal) => {
if (exitCode === 0) {
return;
}
// If the exit code is not 0, then we show some error message
if (Object.keys(parityArgv).length) {
// If parity has been launched with some args, then most likely the
// args are wrong, so we show the output of parity.
return fsReadFile(logFile).then(data =>
console.log(data.toString())
);
} else {
throw new Error(`Exit code ${exitCode}, with signal ${signal}.`);
}
});
})
.then(() => mainWindow.webContents.send('parity-running', true)) // Notify the renderers
.catch(err => {
handleError(err, 'An error occured while running parity.');
});
},
killParity() {
if (parity) {
console.log('Stopping parity.');
parity.kill();
parity = null;
}
}
};