Skip to content
This repository was archived by the owner on Feb 26, 2020. It is now read-only.

Commit a0edd59

Browse files
authored
Fix cli beta (#100)
* Fix cli (fix #65) * Reorganize files * Change help messages * Add --no-run-parity * Small tweaks * Util->utils * Fix bug run parity * Fix bug no-run-parity
1 parent ba0396e commit a0edd59

File tree

11 files changed

+197
-120
lines changed

11 files changed

+197
-120
lines changed

electron/cli/helpMessage.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module.exports = `
2+
Parity UI.
3+
Copyright 2015, 2016, 2017, 2018 Parity Technologies (UK) Ltd.
4+
5+
Operating Options:
6+
--no-run-parity
7+
Parity UI will not attempt to run the locally installed parity.
8+
9+
--ui-dev
10+
Parity UI will load http://localhost:3000. WARNING: Only use this is you plan on developing on Parity UI.
11+
12+
--ws-interface=[IP]
13+
Specify the hostname portion of the WebSockets server Parity UI will connect to. IP should be an interface's IP address. (default: 127.0.0.1)
14+
15+
--ws-port=[PORT]
16+
Specify the port portion of the WebSockets server Parity UI will connect to. (default: 8546)
17+
`;

electron/cli/index.js

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,45 +17,23 @@
1717
// eslint-disable-next-line
1818
const dynamicRequire = typeof __non_webpack_require__ === 'undefined' ? require : __non_webpack_require__; // Dynamic require https://github.com/yargs/yargs/issues/781
1919

20-
const { app } = require('electron');
21-
const fs = require('fs');
2220
const omit = require('lodash/omit');
23-
const { spawn } = require('child_process');
2421

2522
const argv = dynamicRequire('yargs').argv;
26-
const parityPath = require('../util/parityPath');
23+
const helpMessage = require('./helpMessage');
2724
const { version } = require('../../package.json');
2825

2926
let parityArgv = null; // Args to pass to `parity` command
3027

31-
/**
32-
* Show output of `parity` command with args. The args are supposed to make
33-
* parity stop, so that the output can be immediately shown on the terminal.
34-
*
35-
* @param {Array<String>} args - The arguments to pass to `parity`.
36-
*/
37-
const showParityOutput = args => {
38-
if (fs.existsSync(parityPath())) {
39-
const parityHelp = spawn(parityPath(), args);
40-
41-
parityHelp.stdout.on('data', data => console.log(data.toString()));
42-
parityHelp.on('close', () => app.quit());
43-
} else {
44-
console.log('Please run Parity UI once to install Parity Ethereum Client. This help message will then show all available commands.');
45-
app.quit();
46-
}
47-
48-
return false;
49-
};
50-
5128
module.exports = () => {
5229
if (argv.help || argv.h) {
53-
return showParityOutput(['--help']);
30+
console.log(helpMessage);
31+
return false;
5432
}
5533

5634
if (argv.version || argv.v) {
5735
console.log(`Parity UI version ${version}.`);
58-
return showParityOutput(['--version']);
36+
return false;
5937
}
6038

6139
// Used cached value if it exists
@@ -64,11 +42,23 @@ module.exports = () => {
6442
}
6543

6644
// Args to pass to `parity` command
67-
parityArgv = omit(argv, '_', '$0');
68-
69-
// Delete all keys starting with --ui* from parityArgv.
70-
// They will be handled directly by the UI.
71-
Object.keys(parityArgv).forEach(key => key.startsWith('ui') && delete parityArgv[key]);
45+
parityArgv = omit(argv, '_', '$0', 'help', 'version');
46+
47+
// Sanitize args to be easily used by parity
48+
Object.keys(parityArgv).forEach(key => {
49+
// Delete all keys starting with --ui* from parityArgv.
50+
// They will be handled directly by the UI.
51+
if (key.startsWith('ui')) {
52+
delete parityArgv[key];
53+
}
54+
55+
// yargs create camelCase keys for each arg, e.g. "--ws-origins all" will
56+
// create { wsOrigins: 'all' }. For parity, we remove all those that have
57+
// a capital letter
58+
if (/[A-Z]/.test(key)) {
59+
delete parityArgv[key];
60+
}
61+
});
7262

7363
return [argv, parityArgv];
7464
};

electron/index.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,34 +20,36 @@ const url = require('url');
2020

2121
const addMenu = require('./menu');
2222
const cli = require('./cli');
23-
const fetchParity = require('./fetchParity');
23+
const doesParityExist = require('./operations/doesParityExist');
24+
const fetchParity = require('./operations/fetchParity');
25+
const handleError = require('./operations/handleError');
2426
const messages = require('./messages');
25-
const { killParity } = require('./messages/runParity');
27+
const { killParity } = require('./operations/runParity');
2628

2729
const { app, BrowserWindow, ipcMain, session } = electron;
2830
let mainWindow;
2931

3032
// Get arguments from cli
31-
const [argv] = cli();
32-
33-
// Will send these variables to renderers via IPC
34-
global.dirName = __dirname;
35-
global.wsInterface = argv['ws-interface'];
36-
global.wsPort = argv['ws-port'];
33+
const argv = cli()[0];
3734

3835
function createWindow () {
3936
// If cli() returns false, then it means that the arguments are stopping the
4037
// app (e.g. --help or --version). We don't do anything more in this case.
41-
if (!argv) { return; }
38+
if (!argv) { return app.quit(); }
39+
40+
// Will send these variables to renderers via IPC
41+
global.dirName = __dirname;
42+
global.wsInterface = argv['ws-interface'];
43+
global.wsPort = argv['ws-port'];
4244

4345
mainWindow = new BrowserWindow({
4446
height: 800,
4547
width: 1200
4648
});
4749

48-
// Fetch parity if not yet installed
49-
fetchParity(mainWindow)
50-
.then(() => { global.parityInstalled = true; });
50+
doesParityExist()
51+
.catch(() => fetchParity(mainWindow)) // Install parity if not present
52+
.catch(handleError); // Errors should be handled before, this is really just in case
5153

5254
if (argv['ui-dev'] === true) {
5355
// Opens http://127.0.0.1:3000 in --ui-dev mode

electron/messages/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
1616

17-
const { runParity } = require('./runParity');
18-
const signerNewToken = require('./signerNewToken');
17+
const { runParity } = require('../operations/runParity');
18+
const signerNewToken = require('../operations/signerNewToken');
1919

2020
/**
2121
* Handle all asynchronous messages from renderer to main.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
2+
// This file is part of Parity.
3+
4+
// Parity is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
9+
// Parity is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
14+
// You should have received a copy of the GNU General Public License
15+
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
16+
17+
const fs = require('fs');
18+
const util = require('util');
19+
20+
const parityPath = require('../utils/parityPath');
21+
22+
const fsExists = util.promisify(fs.stat);
23+
24+
module.exports = () => fsExists(parityPath())
25+
.then(() => { global.isParityInstalled = true; });
Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,18 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
1616

17-
const { app, dialog, webContents } = require('electron');
17+
const { app } = require('electron');
1818
const axios = require('axios');
1919
const { download } = require('electron-dl');
2020
const fs = require('fs');
2121
const util = require('util');
2222

23-
const { parity: { channel } } = require('../../package.json');
24-
const parityPath = require('../util/parityPath');
23+
const handleError = require('./handleError');
24+
const {
25+
parity: { channel }
26+
} = require('../../package.json');
27+
const parityPath = require('../utils/parityPath');
2528

26-
const fsExists = util.promisify(fs.stat);
2729
const fsChmod = util.promisify(fs.chmod);
2830

2931
const getArch = () => {
@@ -57,35 +59,25 @@ const getOs = () => {
5759
}
5860
};
5961

60-
module.exports = (mainWindow) => {
61-
// Download parity if not exist in userData
62-
// Fetching from https://vanity-service.parity.io/parity-binaries
63-
return fsExists(parityPath())
64-
.catch(() => axios.get(`https://vanity-service.parity.io/parity-binaries?version=${channel}&os=${getOs()}&architecture=${getArch()}`)
65-
.then((response) => response.data[0].files.find(({ name }) => name === 'parity' || name === 'parity.exe'))
66-
.then(({ downloadUrl }) => download(
67-
mainWindow,
68-
downloadUrl,
69-
{
70-
directory: app.getPath('userData'),
71-
onProgress: (progress) => webContents.fromId(mainWindow.id).send('parity-download-progress', progress)
72-
}
73-
))
62+
// Fetch parity from https://vanity-service.parity.io/parity-binaries
63+
module.exports = mainWindow =>
64+
axios
65+
.get(
66+
`https://vanity-service.parity.io/parity-binaries?version=${channel}&os=${getOs()}&architecture=${getArch()}`
67+
)
68+
.then(response =>
69+
response.data[0].files.find(
70+
({ name }) => name === 'parity' || name === 'parity.exe'
71+
)
72+
)
73+
.then(({ downloadUrl }) =>
74+
download(mainWindow, downloadUrl, {
75+
directory: app.getPath('userData'),
76+
onProgress: progress =>
77+
mainWindow.webContents.send('parity-download-progress', progress) // Notify the renderers
78+
})
7479
)
7580
.then(() => fsChmod(parityPath(), '755'))
76-
.then(() => parityPath())
77-
.catch((err) => {
78-
console.error(err);
79-
dialog.showMessageBox({
80-
buttons: ['OK'],
81-
detail: `Please attach the following debugging info:
82-
OS: ${process.platform}
83-
Arch: ${process.arch}
84-
Channel: ${channel}
85-
Error: ${err.message}`,
86-
message: 'An error occured while downloading parity. Please file an issue at https://github.com/parity-js/shell/issues.',
87-
title: 'Parity Error',
88-
type: 'error'
89-
}, () => app.exit(1));
81+
.catch(err => {
82+
handleError(err, 'An error occured while fetching parity.');
9083
});
91-
};

electron/operations/handleError.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
2+
// This file is part of Parity.
3+
4+
// Parity is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
9+
// Parity is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
14+
// You should have received a copy of the GNU General Public License
15+
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
16+
17+
const { app, dialog } = require('electron');
18+
19+
const {
20+
parity: { channel }
21+
} = require('../../package.json');
22+
const parityPath = require('../utils/parityPath');
23+
24+
module.exports = (err, message = 'An error occurred.') => {
25+
console.error(err);
26+
dialog.showMessageBox(
27+
{
28+
buttons: ['Quit', 'Cancel'],
29+
detail: `Please attach the following debugging info:
30+
OS: ${process.platform}
31+
Arch: ${process.arch}
32+
Channel: ${channel}
33+
Error: ${err.message}
34+
Please also attach the contents of the following file:
35+
${parityPath()}.log
36+
37+
Please quit the app and retry again. If the error message persists, please file an issue at https://github.com/parity-js/shell/issues.`,
38+
message: `${message}`,
39+
title: 'Parity Error',
40+
type: 'error'
41+
},
42+
(buttonIndex) => { if (buttonIndex === 0) { app.exit(1); } }
43+
);
44+
};

0 commit comments

Comments
 (0)