Newer
Older
// Copyright 2015-2018 Parity Technologies (UK) Ltd.
// This file is part of Parity.
//
// SPDX-License-Identifier: BSD-3-Clause
import { app } from 'electron';
import commandExists from 'command-exists';
import fs from 'fs';
import promiseAny from 'promise-any';
import { promisify } from 'util';
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
const fsStat = promisify(fs.stat);
// The default path to install parity, in case there's no other instance found
// on the machine.
export const defaultParityPath = () =>
Promise.resolve(
`${app.getPath('userData')}/parity${
process.platform === 'win32' ? '.exe' : ''
}`
);
let parityPath; // The real parity path, will be populated after doesParityExist Promise resolves
/**
* Test if `parity` command is in $PATH.
*/
const isParityInPath = async () => {
const parityCommandExists = await commandExists('parity');
if (parityCommandExists) {
// If yes, return `parity` as command to launch parity
return 'parity';
}
};
/**
* Test if Parity is in the common OS locations.
*/
const isParityInOs = async () => {
// OS locations to test if parity binary exists
const locations = {
linux: ['/bin/parity', '/usr/bin/parity', '/usr/local/bin/parity'],
darwin: ['/Applications/Parity Ethereum.app/Contents/MacOS/parity'],
win32: ['C:\\Program Files\\Parity Technologies\\Parity\\parity.exe']
};
return promiseAny(
locations[process.platform].map(location =>
fsStat(location).then(() => location)
)
);
};
/**
* Test is Parity is already downloaded in electron app's userData folder.
*/
const isParityInUserData = async () => {
const parityPath = await defaultParityPath();
await fsStat(parityPath);
return parityPath;
};
/**
* This function checks if parity has been installed on the local machine:
* - first check if the program is in $PATH, using `command-exists`
* - then check the OS default installation dir if a parity folder exists
* - finally check parity-ui's own userData folder
* This function should run in node env.
*
* @return Promise<String> - Resolves to a string which is the command to run parity.
*/
const doesParityExist = async () => {
try {
// First test if `parity` command exists
return await isParityInPath();
} catch (e) {}
try {
// Then test if parity is in OS
return await isParityInOs();
} catch (e) {}
try {
// Finally test userData folder
return await isParityInUserData();
} catch (e) {
throw new Error('Parity not found.');
}
};
export const getParityPath = async () => {
if (parityPath) {
return parityPath;
}
try {
const path = await doesParityExist();
parityPath = path; // Save the final result in module variable
debug('main')(`Parity found on machine, can be run with "${path}".`);
return path;
} catch (err) {