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