All files / src/file watchCovenants.js

21.74% Statements 10/46
0% Branches 0/6
0% Functions 0/10
21.74% Lines 10/46
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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 951x         1x   1x       1x   1x                                                   1x         1x                                             1x                               1x                   1x  
const watch = require('watch')
const {
  config: {
    selectors: { getCovenants, getChainByAlias }
  }
} = require('interbit-covenant-tools')
 
const log = require('../log')
const {
  generateDeploymentDetails,
  deployCovenants
} = require('../chainManagement')
 
const watchCovenants = (cli, interbitConfig, chainManifest) => {
  const covenants = getCovenants(interbitConfig)
  Object.entries(covenants).forEach(([name, config]) => {
    log.info(`Watching for changes in ${config.location}`)
 
    watch.createMonitor(config.location, monitor => {
      try {
        monitor.on('created', (f, stat) => {
          redeployCovenants(cli, interbitConfig, chainManifest)
        })
        monitor.on('changed', (f, curr, prev) => {
          redeployCovenants(cli, interbitConfig, chainManifest)
        })
        monitor.on('removed', (f, stat) => {
          redeployCovenants(cli, interbitConfig, chainManifest)
        })
      } catch (e) {
        monitor.stop()
        log.error(
          `interbit: Problem redeploying covenant ${name} at ${config.location}`
        )
      }
    })
  })
}
 
const redeployBuffer = {
  isDeploying: false,
  isDeployPending: false
}
 
const redeployCovenants = async (cli, interbitConfig, chainManifest) => {
  log.info('attempted to redeploy')
  if (redeployBuffer.isDeploying) {
    redeployBuffer.isDeployPending = true
    return undefined
  } else if (redeployBuffer.isDeployPending) {
    redeployBuffer.isDeployPending = false
  }
  redeployBuffer.isDeploying = true
 
  log.info('Redeploying updated covenants...')
 
  const covenantHashes = await deployCovenants({
    cli,
    covenantConfig: getCovenants(interbitConfig)
  })
 
  applyUpdates(cli, chainManifest, interbitConfig, covenantHashes)
  redeployBuffer.isDeploying = false
 
  return covenantHashes
}
 
const applyUpdates = (cli, chainManifest, interbitConfig, covenantHashes) => {
  Object.entries(chainManifest).forEach(async ([chainAlias, chainEntry]) => {
    const aliasedChain = getChainByAlias(chainAlias, interbitConfig)
    const covenantAlias = aliasedChain.covenant
    const covenantHash = covenantHashes[covenantAlias]
 
    cli.applyCovenant(chainEntry.chainId, covenantHash)
 
    const isDefaultChain = aliasedChain.isDefaultChain
    if (isDefaultChain) {
      const defaultChain = await cli.getChain(chainEntry.chainId)
      updateManifest(chainManifest, covenantHashes, defaultChain)
    }
  })
}
 
const updateManifest = (chainManifest, covenantHashes, defaultChain) => {
  const deploymentDetails = generateDeploymentDetails(
    chainManifest,
    covenantHashes
  )
  log.info('Updated deploymentDetails ', deploymentDetails)
 
  // TODO: dispatch updated deployment deets to the defaultChain
}
 
module.exports = watchCovenants