1# Copyright (c) 2024 Huawei Device Co., Ltd.
2# Licensed under the Apache License, Version 2.0 (the "License");
3# you may not use this file except in compliance with the License.
4# You may obtain a copy of the License at
5#
6#     http://www.apache.org/licenses/LICENSE-2.0
7#
8# Unless required by applicable law or agreed to in writing, software
9# distributed under the License is distributed on an "AS IS" BASIS,
10# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11# See the License for the specific language governing permissions and
12# limitations under the License.
13
14#!/bin/bash
15
16# ----------------------------------------------------------------------------
17#  Hvigor startup script, version 1.0.0
18#
19#  Required ENV vars:
20#  ------------------
21#    NODE_HOME - location of a Node home dir
22#    or
23#    Add /usr/local/nodejs/bin to the PATH environment variable
24# ----------------------------------------------------------------------------
25
26HVIGOR_APP_HOME="`pwd -P`"
27HVIGOR_WRAPPER_SCRIPT=${HVIGOR_APP_HOME}/hvigor/hvigor-wrapper.js
28#NODE_OPTS="--max-old-space-size=4096"
29
30fail() {
31  echo "$*"
32  exit 1
33}
34
35set_executable_node() {
36  EXECUTABLE_NODE="${NODE_HOME}/bin/node"
37  if [ -x "$EXECUTABLE_NODE" ]; then
38    return
39  fi
40
41  EXECUTABLE_NODE="${NODE_HOME}/node"
42  if [ -x "$EXECUTABLE_NODE" ]; then
43    return
44  fi
45  fail "ERROR: NODE_HOME is set to an invalid directory,check $NODE_HOME\n\nPlease set NODE_HOME in your environment to the location where your nodejs installed"
46}
47
48# Determine node to start hvigor wrapper script
49if [ -n "${NODE_HOME}" ]; then
50  set_executable_node
51else
52  EXECUTABLE_NODE="node"
53  command -v ${EXECUTABLE_NODE} &> /dev/null || fail "ERROR: NODE_HOME not set and 'node' command not found"
54fi
55
56# Check hvigor wrapper script
57if [ ! -r "$HVIGOR_WRAPPER_SCRIPT" ]; then
58  fail "ERROR: Couldn't find hvigor/hvigor-wrapper.js in ${HVIGOR_APP_HOME}"
59fi
60
61if [ -z "${NODE_OPTS}" ]; then
62  NODE_OPTS="--"
63fi
64
65# start hvigor-wrapper script
66exec "${EXECUTABLE_NODE}" "${NODE_OPTS}" \
67  "${HVIGOR_WRAPPER_SCRIPT}" "$@"
68