All files / scripts/deploy-service index.js

2.04% Statements 1/49
0% Branches 0/14
0% Functions 0/9
2.08% Lines 1/48

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 1291x                                                                                                                                                                                                                                                                
module.exports = async function(options) {
  options = require('../utils/injectConfig')(options);
  const { checkServiceFileExist } = require('../../bin/error');
  checkServiceFileExist();
 
  const { printOrSilent } = require('@haechi-labs/vvisp-utils');
  const { writeState } = require('./utils');
  const DeployState = require('./DeployState');
  const preProcess = require('./preProcess');
  const {
    PROJECT_NAME,
    SERVICE_FILE,
    STATE_FILE
  } = require('../../config/Constant');
  const {
    deployContracts,
    initContracts,
    injectVar,
    reflectState
  } = require('./processes');
  const fs = require('fs-extra');
  const path = require('path');
  const chk = require('chalk');
  global.chalk = {
    success: chk.green.bold,
    address: chk.cyan,
    tx: chk.cyan,
    head: chk.bold,
    error: chk.red.bold,
    keyWord: chk.blue.bold,
    notImportant: chk.gray,
    warning: chk.yellow
  };
 
  if (options.force) {
    fs.removeSync(path.join('./', STATE_FILE));
  }
 
  await main();
 
  async function main() {
    const deployState = new DeployState();
 
    await preProcess(deployState, options);
 
    const processes = [
      {
        name: 'injectVar',
        process: async function() {
          injectVar(deployState);
        }
      },
      {
        name: 'deployContracts',
        process: async function() {
          await deployContracts(deployState, options);
        }
      },
      {
        name: 'initContracts',
        process: async function() {
          await initContracts(deployState, options);
        }
      },
      {
        name: 'reflectState',
        process: function() {
          reflectState(deployState, options);
        }
      }
    ];
 
    printOrSilent(chalk.head('Now Start Deploying Contracts...\n'), options);
 
    await runProcess(deployState, processes, options).catch(e =>
      writePausedState(e, deployState, options)
    );
  }
 
  async function runProcess(deployState, processes, options) {
    let stateClone = deployState.getState();
    const { notUpgrading } = stateClone;
    for (let i = 0; i < processes.length; i++) {
      if (stateClone.paused.stage === processes[i].name) {
        await processes[i].process();
        stateClone = deployState.getState();
        if (i === processes.length - 1) {
          const processState = notUpgrading ? 'Deploying' : 'Upgrading';
          printOrSilent(
            chalk.success(`${processState} ${stateClone.serviceName} Finished`),
            options
          );
          printOrSilent(
            `You can see result in ${chalk.keyWord(STATE_FILE)}`,
            options
          );
        } else {
          stateClone.paused = { stage: processes[i + 1].name };
          stateClone = deployState.updateState(stateClone).getState();
        }
      }
    }
  }
 
  function writePausedState(error, deployState, options) {
    const stateClone = deployState.getState();
    if (error) {
      printOrSilent(error, options);
    }
    if (!stateClone || !stateClone.paused) {
      return;
    }
    printOrSilent(chalk.error(`Service State Paused!`), options);
    printOrSilent(
      `Resume Process by Running ${chalk.keyWord(
        `${PROJECT_NAME} deploy-service`
      )} Again.`,
      options
    );
    printOrSilent(
      `If you want to deploy a new service, command ${chalk.keyWord(
        `${PROJECT_NAME} deploy-service -f`
      )}`,
      options
    );
    writeState(stateClone, options);
  }
};