#!/bin/bash
source ~/.nvm/nvm.sh
set -e

workspace=$(cd $(dirname $0) && pwd -P)
cd $workspace

echo -e "workspace: $workspace"

BRANCH_NAME=$OE_BRANCH_NAME

function build() {
  export NODE_PATH=/home/xiaoju/node-v8.9.1-linux-x64:/home/xiaoju/node-v8.9.1-linux-x64/lib/node_modules
  export PATH=/home/xiaoju/node-v8.9.1-linux-x64/bin:$PATH
  export NODE_OPTIONS=--max_old_space_size=12000

  echo -e "node: `node -v`, npm: `npm -v`"

  yarn --ignore-engines
  yarn run build

  ret=$?
  if [ $ret -ne 0 ];then
    echo "===== node build failure ====="
    exit $ret
  else
    echo "===== node build successfully! ====="
  fi
}

function make_output() {
  local output="output"

  if [ -d $output ];then
    rm -rf $output
  fi

  cp -r ./dist ${output}
  cp control.sh $output

  ret=$?
  if [ $ret -ne 0 ];then
    echo "===== Generate output failure ====="
    exit $ret
  else
    echo "===== Generate output ok ====="
  fi
}

# 1.进行编译
build

# 2.生成部署包output
make_output

# 编译成功
echo -e "build done"
exit 0
