text: // Copyright 2015-2019 Parity Technologies (UK) Ltd.
  // This file is part of Parity.
  //
  // SPDX-License-Identifier: BSD-3-Clause
  
  import fs from 'fs';
  
  import { runParity } from '@parity/electron';
  
  import { bundledParityPath, BUNDLED_IPC_PATH } from '../utils/paths';
  import handleError from '../utils/handleError';
  import cli from '../cli';
  import ipcChannel from '../ipcChannel';
  import Pino from '../utils/pino';
  
  const pino = Pino();
  
  function wait (milliseconds) {
    return new Promise(resolve => setTimeout(resolve, milliseconds));
  }
  
  class ParityEthereum {
    constructor () {
      if (cli.ipcPath) {
        pino.info('--ipc-path provided; connecting to', cli.ipcPath);
  
        return ipcChannel.init(cli.ipcPath).catch(handleError);
      }
  
      pino.info('Running Parity Ethereum');
  
      // Run the bundled Parity Ethereum
      return this.run()
        .then(async _ => {
          // wait for the ipc socket file to be set up
          do {
            await wait(200);
          } while (!fs.existsSync(BUNDLED_IPC_PATH));
        })
        .then(() => ipcChannel.init(BUNDLED_IPC_PATH))
        .catch(handleError);
    }
  
    // Run the bundled Parity Ethereum binary
    run = async () => {
      return runParity({
        parityPath: bundledParityPath,
        flags: [
          '--light',
          '--no-jsonrpc',
          '--no-ws',
          '--ipc-path',
          BUNDLED_IPC_PATH,
          '--ipc-apis',
          'all', // we need to enable personal to use personal_signTransaction
          '--chain',
          cli.chain
        ],
        onParityError: err =>
          handleError(err, 'An error occured with Parity Ethereum.')
      });
    };
  }
  
  export default ParityEthereum;
  