All files / src/manifest generateManifest.js

100% Statements 24/24
100% Branches 0/0
100% Functions 3/3
100% Lines 24/24
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 912x           2x 2x       2x 2x 2x                       2x           12x 12x 12x   12x         12x 12x   12x               12x   10x         10x   10x           2x 12x 12x 11x                             12x     2x      
const path = require('path')
const {
  config: {
    selectors: { getApps },
    validateConfig
  }
} = require('interbit-covenant-tools')
const hashObject = require('./hash')
const {
  resolveGenesisBlocks,
  resolveChainIdsFromGenesis
} = require('../genesisBlock')
const log = require('../log')
const { createManifestTree } = require('./createManifestTree')
 
/**
 * Generates a manifest file based on the configuration and optional original
 * manifest passed into parameters, resolving config specifications to chain Ids,
 * covenant hashes, and genesis blocks.
 * @param {String} location - The file location to work from, commonly `process.cwd()`.
 * @param {Object} interbitConfig - The Interbit configuration file as JSON.
 * @param {Object} covenants - A map of covenant aliases to their hashes.
 * @param {Object} [originalManifest] - An existing Interbit manifest file as JSON, if available.
 * @returns {Object} The newly generated Interbit manifest.
 */
const generateManifest = (
  location,
  interbitConfig,
  covenants,
  originalManifest
) => {
  log.info('GENERATING A MANIFEST')
  log.info({ location, interbitConfig, covenants, originalManifest })
  const config = validateConfig(interbitConfig)
 
  const genesisBlocks = resolveGenesisBlocks(
    config,
    originalManifest,
    covenants
  )
  const chains = resolveChainIdsFromGenesis(genesisBlocks)
  const apps = generateAppsManifest(location, config)
 
  const manifestTemplate = {
    peers: config.peers,
    apps,
    covenants,
    chains,
    genesisBlocks
  }
 
  const manifestTree = createManifestTree(config, manifestTemplate)
 
  const manifest = {
    ...manifestTemplate,
    manifest: manifestTree
  }
 
  const hash = hashObject(manifest)
 
  return {
    ...manifest,
    hash
  }
}
 
const generateAppsManifest = (location, interbitConfig) => {
  const configApps = getApps(interbitConfig)
  const apps = Object.keys(configApps).reduce(
    (prev, appAlias) => ({
      ...prev,
      [appAlias]: {
        appChain: configApps[appAlias].appChain,
        // TODO: Update this build location with implementation of #9
        buildLocation: path.relative(
          location,
          configApps[appAlias].buildLocation
        ),
        browserChains: configApps[appAlias].chains
      }
    }),
    {}
  )
 
  return apps
}
 
module.exports = {
  generateManifest
}