Skip to content

Commit

Permalink
chore: fix import statement in main.js
Browse files Browse the repository at this point in the history
  • Loading branch information
truemiller committed Jul 23, 2024
1 parent bb7811a commit 53ec8ea
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 184 deletions.
203 changes: 20 additions & 183 deletions electron/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,24 @@ const sudo = require('sudo-prompt');
const process = require('process');
const axios = require('axios');
const { spawnSync } = require('child_process');
const { logger } = require('./logger');

const { paths } = require('./constants');

/**
* current version of the pearl release
* - use "" (nothing as a suffix) for latest release candidate, for example "0.1.0rc26"
* - use "alpha" for alpha release, for example "0.1.0rc26-alpha"
*/
/** @deprecated PyPi is not used any more, this OlasMiddlewareVersion will be removed in the future */
const OlasMiddlewareVersion = '0.1.0rc83';

const Env = {
...process.env,
PATH: `${process.env.PATH}:/opt/homebrew/bin:/usr/local/bin`,
HOMEBREW_NO_AUTO_UPDATE: '1',
};

const SudoOptions = {
name: 'Pearl',
env: Env,
};

const TendermintUrls = {
darwin: {
x64: 'https://github.com/tendermint/tendermint/releases/download/v0.34.19/tendermint_0.34.19_darwin_amd64.tar.gz',
Expand All @@ -48,19 +47,8 @@ function getBinPath(command) {
.trim();
}

function appendInstallationLog(log) {
fs.appendFileSync(paths.OperateInstallationLog, `${log}\n`, {
encoding: 'utf-8',
});
return log;
}

function runCmdUnix(command, options) {
console.log(
appendInstallationLog(
`Running ${command} with options ${JSON.stringify(options)}`,
),
);
logger.electron(`Running ${command} with options ${JSON.stringify(options)}`);
let bin = getBinPath(command);
if (!bin) {
throw new Error(`Command ${command} not found; Path : ${Env.PATH}`);
Expand All @@ -72,9 +60,9 @@ function runCmdUnix(command, options) {
Error: ${output.error}; Stdout: ${output.stdout}; Stderr: ${output.stderr}`,
);
}
console.log(appendInstallationLog(`Executed ${command} ${options} with`));
console.log(appendInstallationLog(`===== stdout ===== \n${output.stdout}`));
console.log(appendInstallationLog(`===== stderr ===== \n${output.stderr}`));
logger.electron(`Executed ${command} ${options} with`);
logger.electron(`===== stdout ===== \n${output.stdout}`);
logger.electron(`===== stderr ===== \n${output.stderr}`);
}

function runSudoUnix(command, options) {
Expand All @@ -98,15 +86,9 @@ function runSudoUnix(command, options) {
Error: ${output.error}; Stdout: ${output.stdout}; Stderr: ${output.stderr}`,
);
}
console.log(
appendInstallationLog(`Executed ${command} ${options} with`),
);
console.log(
appendInstallationLog(`===== stdout ===== \n${output.stdout}`),
);
console.log(
appendInstallationLog(`===== stderr ===== \n${output.stderr}`),
);
logger.electron(`Executed ${command} ${options} with`);
logger.electron(`===== stdout ===== \n${output.stdout}`);
logger.electron(`===== stderr ===== \n${output.stderr}`);
resolve();
},
);
Expand Down Expand Up @@ -140,19 +122,13 @@ async function installTendermintUnix() {
const cwd = process.cwd();
process.chdir(paths.tempDir);

console.log(
appendInstallationLog(
`Installing tendermint for ${os.platform()}-${process.arch}`,
),
);
logger.electron(`Installing tendermint for ${os.platform()}-${process.arch}`);
const url = TendermintUrls[os.platform()][process.arch];

console.log(
appendInstallationLog(`Downloading ${url}, might take a while...`),
);
logger.electron(`Downloading ${url}, might take a while...`);
await downloadFile(url, `${paths.tempDir}/tendermint.tar.gz`);

console.log(appendInstallationLog(`Installing tendermint binary`));
logger.electron(`Installing tendermint binary`);
runCmdUnix('tar', ['-xvf', 'tendermint.tar.gz']);

// TOFIX: Install tendermint in .operate instead of globally
Expand All @@ -165,62 +141,6 @@ async function installTendermintUnix() {
process.chdir(cwd);
}

function createVirtualEnvUnix(path) {
runCmdUnix('python3.10', ['-m', 'venv', path]);
}

function isPythonInstalledUbuntu() {
return Boolean(getBinPath('python3.10'));
}

function isGitInstalledUbuntu() {
return Boolean(getBinPath('git'));
}

function installPythonUbuntu() {
return runSudoUnix('apt', 'install -y python3.10 python3.10-dev python3-pip');
}

function installGitUbuntu() {
return runSudoUnix('apt', 'install -y git');
}

function installOperatePackageUnix(path) {
runCmdUnix(`${path}/venv/bin/python3.10`, [
'-m',
'pip',
'install',
`olas-operate-middleware==${OlasMiddlewareVersion}`,
]);
}

function reInstallOperatePackageUnix(path) {
console.log(appendInstallationLog('Reinstalling pearl CLI'));
runCmdUnix(`${path}/venv/bin/python3.10`, [
'-m',
'pip',
'install',
`olas-operate-middleware==${OlasMiddlewareVersion}`,
'--force-reinstall',
]);
}

function installOperateCli(path) {
let installPath = `${path}/operate`;
if (fs.existsSync(installPath)) {
fs.rmSync(installPath);
}
return new Promise((resolve, _reject) => {
fs.copyFile(
`${paths.dotOperateDirectory}/venv/bin/operate`,
installPath,
function (error, _stdout, _stderr) {
resolve(!error);
},
);
});
}

function createDirectory(path) {
if (fs.existsSync(path)) {
return;
Expand All @@ -232,118 +152,35 @@ function createDirectory(path) {
});
}

function writeVersion() {
fs.writeFileSync(paths.versionFile, OlasMiddlewareVersion);
}

function versionBumpRequired() {
if (!fs.existsSync(paths.versionFile)) {
return true;
}
const olasMiddlewareVersionInFile = fs
.readFileSync(paths.versionFile)
.toString();
return olasMiddlewareVersionInFile != OlasMiddlewareVersion;
}

function removeLogFile() {
if (fs.existsSync(paths.LogFile)) {
fs.rmSync(paths.LogFile);
}
}

function removeInstallationLogFile() {
if (fs.existsSync(paths.OperateInstallationLog)) {
fs.rmSync(paths.OperateInstallationLog);
}
}

/*******************************/
// NOTE: "Installing" is string matched in loading.html to detect installation
/*******************************/

async function setupDarwin(ipcChannel) {
removeInstallationLogFile();
console.log(appendInstallationLog('Creating required directories'));
logger.electron('Creating required directories');
await createDirectory(`${paths.dotOperateDirectory}`);
await createDirectory(`${paths.tempDir}`);

console.log(appendInstallationLog('Checking tendermint installation'));
logger.electron('Checking tendermint installation');
if (!isTendermintInstalledUnix()) {
ipcChannel.send('response', 'Installing Pearl Daemon');
console.log(appendInstallationLog('Installing tendermint'));
logger.electron('Installing tendermint');
await installTendermintUnix();
}

// console.log(appendInstallationLog('Checking if upgrade is required'));
// if (versionBumpRequired()) {
// console.log(
// appendInstallationLog(
// `Upgrading pearl daemon to ${OlasMiddlewareVersion}`,
// ),
// );
// writeVersion();
// removeLogFile();
// // reInstallOperatePackageUnix(OperateDirectory);
// }
}

// TODO: Add Tendermint installation
async function setupUbuntu(ipcChannel) {
removeInstallationLogFile();

// console.log(appendInstallationLog('Checking python installation'));
// if (!isPythonInstalledUbuntu()) {
// ipcChannel.send('response', 'Installing Pearl Daemon');
// console.log(appendInstallationLog('Installing Python'));
// await installPythonUbuntu(paths.dotOperateDirectory);
// }

// console.log(appendInstallationLog('Checking git installation'));
// if (!isGitInstalledUbuntu()) {
// ipcChannel.send('response', 'Installing Pearl Daemon');
// console.log(appendInstallationLog('Installing git'));
// await installGitUbuntu(paths.dotOperateDirectory);
// }

console.log(appendInstallationLog('Creating required directories'));
logger.electron('Creating required directories');
await createDirectory(`${paths.dotOperateDirectory}`);
await createDirectory(`${paths.tempDir}`);

console.log(appendInstallationLog('Checking tendermint installation'));
logger.electron('Checking tendermint installation');
if (!isTendermintInstalledUnix()) {
ipcChannel.send('response', 'Installing Pearl Daemon');
console.log(appendInstallationLog('Installing tendermint'));
logger.electron('Installing tendermint');
await installTendermintUnix();
}

// if (!fs.existsSync(paths.venvDir)) {
// ipcChannel.send('response', 'Installing Pearl Daemon');
// console.log(appendInstallationLog('Creating virtual environment'));
// createVirtualEnvUnix(paths.venvDir);

// console.log(appendInstallationLog('Installing pearl backend'));
// installOperatePackageUnix(paths.dotOperateDirectory);
// }

// console.log(appendInstallationLog('Checking if upgrade is required'));
// if (versionBumpRequired()) {
// console.log(
// appendInstallationLog(
// `Upgrading pearl daemon to ${OlasMiddlewareVersion}`,
// ),
// );
// reInstallOperatePackageUnix(paths.dotOperateDirectory);
// writeVersion();
// removeLogFile();
// }

// if (!fs.existsSync(`${paths.dotOperateDirectory}/venv/bin/operate`)) {
// reInstallOperatePackageUnix(paths.dotOperateDirectory);
// }

// console.log(appendInstallationLog('Installing pearl CLI'));
// await installOperateCli('/usr/local/bin');
}

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion electron/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const http = require('http');
const AdmZip = require('adm-zip');
const { TRAY_ICONS, TRAY_ICONS_PATHS } = require('./icons');

const {setupDarwin, setupUbuntu, Env } = require('./install');
const { setupDarwin, setupUbuntu, Env } = require('./install');

const { paths } = require('./constants');
const { killProcesses } = require('./processes');
Expand Down

0 comments on commit 53ec8ea

Please sign in to comment.