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

Commit

Permalink
Fix cli beta (#100)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
amaury1093 authored May 11, 2018
1 parent ba0396e commit a0edd59
Show file tree
Hide file tree
Showing 11 changed files with 197 additions and 120 deletions.
17 changes: 17 additions & 0 deletions electron/cli/helpMessage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = `
Parity UI.
Copyright 2015, 2016, 2017, 2018 Parity Technologies (UK) Ltd.
Operating Options:
--no-run-parity
Parity UI will not attempt to run the locally installed parity.
--ui-dev
Parity UI will load http://localhost:3000. WARNING: Only use this is you plan on developing on Parity UI.
--ws-interface=[IP]
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)
--ws-port=[PORT]
Specify the port portion of the WebSockets server Parity UI will connect to. (default: 8546)
`;
52 changes: 21 additions & 31 deletions electron/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,45 +17,23 @@
// eslint-disable-next-line
const dynamicRequire = typeof __non_webpack_require__ === 'undefined' ? require : __non_webpack_require__; // Dynamic require https://github.com/yargs/yargs/issues/781

const { app } = require('electron');
const fs = require('fs');
const omit = require('lodash/omit');
const { spawn } = require('child_process');

const argv = dynamicRequire('yargs').argv;
const parityPath = require('../util/parityPath');
const helpMessage = require('./helpMessage');
const { version } = require('../../package.json');

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

/**
* Show output of `parity` command with args. The args are supposed to make
* parity stop, so that the output can be immediately shown on the terminal.
*
* @param {Array<String>} args - The arguments to pass to `parity`.
*/
const showParityOutput = args => {
if (fs.existsSync(parityPath())) {
const parityHelp = spawn(parityPath(), args);

parityHelp.stdout.on('data', data => console.log(data.toString()));
parityHelp.on('close', () => app.quit());
} else {
console.log('Please run Parity UI once to install Parity Ethereum Client. This help message will then show all available commands.');
app.quit();
}

return false;
};

module.exports = () => {
if (argv.help || argv.h) {
return showParityOutput(['--help']);
console.log(helpMessage);
return false;
}

if (argv.version || argv.v) {
console.log(`Parity UI version ${version}.`);
return showParityOutput(['--version']);
return false;
}

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

// Args to pass to `parity` command
parityArgv = omit(argv, '_', '$0');

// Delete all keys starting with --ui* from parityArgv.
// They will be handled directly by the UI.
Object.keys(parityArgv).forEach(key => key.startsWith('ui') && delete parityArgv[key]);
parityArgv = omit(argv, '_', '$0', 'help', 'version');

// Sanitize args to be easily used by parity
Object.keys(parityArgv).forEach(key => {
// Delete all keys starting with --ui* from parityArgv.
// They will be handled directly by the UI.
if (key.startsWith('ui')) {
delete parityArgv[key];
}

// yargs create camelCase keys for each arg, e.g. "--ws-origins all" will
// create { wsOrigins: 'all' }. For parity, we remove all those that have
// a capital letter
if (/[A-Z]/.test(key)) {
delete parityArgv[key];
}
});

return [argv, parityArgv];
};
26 changes: 14 additions & 12 deletions electron/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,36 @@ const url = require('url');

const addMenu = require('./menu');
const cli = require('./cli');
const fetchParity = require('./fetchParity');
const doesParityExist = require('./operations/doesParityExist');
const fetchParity = require('./operations/fetchParity');
const handleError = require('./operations/handleError');
const messages = require('./messages');
const { killParity } = require('./messages/runParity');
const { killParity } = require('./operations/runParity');

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

// Get arguments from cli
const [argv] = cli();

// Will send these variables to renderers via IPC
global.dirName = __dirname;
global.wsInterface = argv['ws-interface'];
global.wsPort = argv['ws-port'];
const argv = cli()[0];

function createWindow () {
// If cli() returns false, then it means that the arguments are stopping the
// app (e.g. --help or --version). We don't do anything more in this case.
if (!argv) { return; }
if (!argv) { return app.quit(); }

// Will send these variables to renderers via IPC
global.dirName = __dirname;
global.wsInterface = argv['ws-interface'];
global.wsPort = argv['ws-port'];

mainWindow = new BrowserWindow({
height: 800,
width: 1200
});

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

if (argv['ui-dev'] === true) {
// Opens http://127.0.0.1:3000 in --ui-dev mode
Expand Down
4 changes: 2 additions & 2 deletions electron/messages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

const { runParity } = require('./runParity');
const signerNewToken = require('./signerNewToken');
const { runParity } = require('../operations/runParity');
const signerNewToken = require('../operations/signerNewToken');

/**
* Handle all asynchronous messages from renderer to main.
Expand Down
25 changes: 25 additions & 0 deletions electron/operations/doesParityExist.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
// This file is part of Parity.

// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

const fs = require('fs');
const util = require('util');

const parityPath = require('../utils/parityPath');

const fsExists = util.promisify(fs.stat);

module.exports = () => fsExists(parityPath())
.then(() => { global.isParityInstalled = true; });
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

const { app, dialog, webContents } = require('electron');
const { app } = require('electron');
const axios = require('axios');
const { download } = require('electron-dl');
const fs = require('fs');
const util = require('util');

const { parity: { channel } } = require('../../package.json');
const parityPath = require('../util/parityPath');
const handleError = require('./handleError');
const {
parity: { channel }
} = require('../../package.json');
const parityPath = require('../utils/parityPath');

const fsExists = util.promisify(fs.stat);
const fsChmod = util.promisify(fs.chmod);

const getArch = () => {
Expand Down Expand Up @@ -57,35 +59,25 @@ const getOs = () => {
}
};

module.exports = (mainWindow) => {
// Download parity if not exist in userData
// Fetching from https://vanity-service.parity.io/parity-binaries
return fsExists(parityPath())
.catch(() => axios.get(`https://vanity-service.parity.io/parity-binaries?version=${channel}&os=${getOs()}&architecture=${getArch()}`)
.then((response) => response.data[0].files.find(({ name }) => name === 'parity' || name === 'parity.exe'))
.then(({ downloadUrl }) => download(
mainWindow,
downloadUrl,
{
directory: app.getPath('userData'),
onProgress: (progress) => webContents.fromId(mainWindow.id).send('parity-download-progress', progress)
}
))
// Fetch parity from https://vanity-service.parity.io/parity-binaries
module.exports = mainWindow =>
axios
.get(
`https://vanity-service.parity.io/parity-binaries?version=${channel}&os=${getOs()}&architecture=${getArch()}`
)
.then(response =>
response.data[0].files.find(
({ name }) => name === 'parity' || name === 'parity.exe'
)
)
.then(({ downloadUrl }) =>
download(mainWindow, downloadUrl, {
directory: app.getPath('userData'),
onProgress: progress =>
mainWindow.webContents.send('parity-download-progress', progress) // Notify the renderers
})
)
.then(() => fsChmod(parityPath(), '755'))
.then(() => parityPath())
.catch((err) => {
console.error(err);
dialog.showMessageBox({
buttons: ['OK'],
detail: `Please attach the following debugging info:
OS: ${process.platform}
Arch: ${process.arch}
Channel: ${channel}
Error: ${err.message}`,
message: 'An error occured while downloading parity. Please file an issue at https://github.com/parity-js/shell/issues.',
title: 'Parity Error',
type: 'error'
}, () => app.exit(1));
.catch(err => {
handleError(err, 'An error occured while fetching parity.');
});
};
44 changes: 44 additions & 0 deletions electron/operations/handleError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
// This file is part of Parity.

// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

const { app, dialog } = require('electron');

const {
parity: { channel }
} = require('../../package.json');
const parityPath = require('../utils/parityPath');

module.exports = (err, message = 'An error occurred.') => {
console.error(err);
dialog.showMessageBox(
{
buttons: ['Quit', 'Cancel'],
detail: `Please attach the following debugging info:
OS: ${process.platform}
Arch: ${process.arch}
Channel: ${channel}
Error: ${err.message}
Please also attach the contents of the following file:
${parityPath()}.log
Please quit the app and retry again. If the error message persists, please file an issue at https://github.com/parity-js/shell/issues.`,
message: `${message}`,
title: 'Parity Error',
type: 'error'
},
(buttonIndex) => { if (buttonIndex === 0) { app.exit(1); } }
);
};
Loading

0 comments on commit a0edd59

Please sign in to comment.