All files / src/scripts start.js

90% Statements 18/20
50% Branches 3/6
100% Functions 1/1
90% Lines 18/20
1 2 3 4 5 6 7 8 9 10 11 12 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            1x 1x     1x   1x                 1x 1x   1x       1x   1x           1x         1x       1x   1x       1x       1x                             1x   1x     1x  
// © 2018 BTL GROUP LTD -  This package is licensed under the MIT license https://opensource.org/licenses/MIT
const {
  createChains: { createChainsFromConfig },
  generateDeploymentDetails,
  startInterbit,
  setRootChainManifest
} = require('../chainManagement')
const { updateIndexHtmls, watchCovenants } = require('../file')
const {
  config: { validateConfig }
} = require('interbit-covenant-tools')
 
const log = require('../log')
 
/**
 * Starts an interbit node with chains based on the provided options.
 * @param {Object} options - The Interbit node start options.
 * @param {Object} options.config - Interbit configuration for the node.
 * @param {boolean} options.dev - Whether to run in dev mode and watch covenant files for changes.
 * @param {boolean} options.noWatch - Option to override covenant watch if in dev mode.
 */
const start = async options => {
  const { config, dev, noWatch } = options
 
  validateConfig(config)
 
  // TODO: Use options.manifest as initial variable resolution for generating the new one #262
 
  const { cli, hypervisor, cleanup } = await startInterbit(undefined, options)
  // TODO: This create based on config should only run in dev mode, else run chains based on manifest #276
  const { chainManifest, covenantHashes } = await createChainsFromConfig(
    cli,
    config
  )
 
  // TODO: This manifest mock is no longer required
  const deploymentDetails = generateDeploymentDetails(
    chainManifest,
    covenantHashes
  )
 
  log.info('available chains + covenants: ', deploymentDetails)
 
  // required to trigger SET_MANIFEST action on chains for setting-up PPC config
  // TODO: switch to using a real manifest
  setRootChainManifest(cli, deploymentDetails, config)
 
  Iif (dev && !noWatch) {
    watchCovenants(cli, config, chainManifest)
  }
 
  Eif (!dev) {
    // TODO: We are not in dev mode so output the diff'd manifest
 
    // TODO: Use the manifest to update index.html instead of "deploymentDetails"
    updateIndexHtmls({
      config, // manifest here
      chains: deploymentDetails.chains
    })
  } else {
    updateIndexHtmls({
      config,
      chains: deploymentDetails.chains
    })
  }
 
  // TODO: Watch the chains for manifest changes #267
  // Blocked by #258 #336
  // watchChain(cli, chainInterface)
 
  log.success('Interbit node started')
 
  return { cli, hypervisor, cleanup, chainManifest }
}
 
module.exports = start