if [ -d "${HOME}/.nvm/" ]
then
  . ${HOME}/.nvm/nvm.sh
fi

mock=0
noSkipNpmInstall=1
node_version="v16.18.0"

checkNode(){
  for line in `node -v` 
  do
  if [ $line != $node_version ]
  then
    echo "change Node from $line to $node_version"
    nvm use $node_version
    ret=$?
    if [ $ret != 0 ]
    then
      echo "Please run nvm install $node_version"
      exit 3
    else
      return 0
    fi
  else
    return 0
  fi
  done
}


checkKnCli(){
  if ! [ -x "$(command -v kn-cli)" ]
  then
    npm i kn-cli -g
    return 0
  fi
  return 0
}


checkResult(){
  ret=$?
  if [ $ret != 0 ]
  then
    echo "[$1]执行失败"
    exit 2
    return 0
  else
    echo "[$1]执行成功"
    return 0
  fi
}

build(){
  npm config delete registry -g
  npm config delete registry

  rm -rf release
  checkResult "清理release"

  checkKnCli
  checkResult "检查kn-cli"

  # npm i kn-cli@1.0.2 -g
  # checkResult "npm kn-cli"

  kn-cli --build
  checkResult "kn-cli --build"

  mv public/dist release/
  checkResult "打包结果同步至release"
}

checkNode
checkResult "检查node"


build
checkResult "build"



