1#!/bin/bash 2# Copyright (c) 2025 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 15set -e 16 17find_root() { 18 local target="$1" 19 local current_dir=$(pwd) 20 local found_dir="" 21 22 while [[ "$current_dir" != "/" ]]; do 23 if [[ -e "$current_dir/$target" ]]; then 24 found_dir="$current_dir" 25 break 26 fi 27 current_dir=$(dirname "$current_dir") 28 done 29 30 if [[ -n "$found_dir" ]]; then 31 echo "$found_dir" 32 return 0 33 else 34 echo "$target directory not found" >&2 35 return 1 36 fi 37} 38 39target_path="build/prebuilts_service/shell/init_ohpm.sh" 40result_dir=$(find_root "$target_path") 41 42if [[ $? -eq 0 ]]; then 43 code_dir="$result_dir" 44else 45 echo "$target directory not found" >&2 46 exit 1 47fi 48# set nodejs and ohpm 49EXPECTED_NODE_VERSION="14.21.1" 50NODE_PLATFORM="linux-x64" 51 52export PATH=${code_dir}/prebuilts/build-tools/common/nodejs/node-v${EXPECTED_NODE_VERSION}-${NODE_PLATFORM}/bin:$PATH 53echo ${code_dir}/prebuilts/build-tools/common/nodejs/node-v${EXPECTED_NODE_VERSION}-${NODE_PLATFORM}/bin 54export NODE_HOME=${code_dir}/prebuilts/build-tools/common/nodejs/node-v${EXPECTED_NODE_VERSION}-${NODE_PLATFORM} 55export PATH=${code_dir}/prebuilts/build-tools/common/oh-command-line-tools/ohpm/bin:$PATH 56 57echo -e "\033[32m[OHOS INFO] Node.js version check passed!\033[0m" 58npm config set registry https://repo.huaweicloud.com/repository/npm/ 59npm config set @ohos:registry https://repo.harmonyos.com/npm/ 60npm config set strict-ssl false 61npm config set lockfile false 62cat $HOME/.npmrc | grep 'lockfile=false' > /dev/null || echo 'lockfile=false' >> $HOME/.npmrc > /dev/null 63 64function init_ohpm() { 65 TOOLS_INSTALL_DIR="${code_dir}/prebuilts/build-tools/common" 66 pushd ${TOOLS_INSTALL_DIR} > /dev/null 67 OHPM_HOME=${TOOLS_INSTALL_DIR}/../../tool/command-line-tools/ohpm/bin 68 chmod +x ${OHPM_HOME}/ohpm 69 export PATH=${OHPM_HOME}:$PATH 70 chmod +x ${OHPM_HOME}/init 71 ${OHPM_HOME}/init > /dev/null 72 echo "[OHOS INFO] Current ohpm version is $(ohpm -v)" 73 ohpm config set registry https://repo.harmonyos.com/ohpm/ 74 ohpm config set strict_ssl false 75 ohpm config set log_level debug 76 popd > /dev/null 77 if [[ -d "$HOME/.hvigor" ]]; then 78 rm -rf $HOME/.hvigor/daemon $HOME/.hvigor/wrapper 79 fi 80 mkdir -p $HOME/.hvigor/wrapper/tools 81 echo '{"dependencies": {"pnpm": "7.30.0"}}' > $HOME/.hvigor/wrapper/tools/package.json 82 pushd $HOME/.hvigor/wrapper/tools > /dev/null 83 echo "[OHOS INFO] installing pnpm..." 84 npm install --silent > /dev/null 85 popd > /dev/null 86 HVIGORW_HOME=${TOOLS_INSTALL_DIR}/../../tool/command-line-tools/hvigor/bin/hvigorw 87 chmod +x ${HVIGORW_HOME} 88 mkdir -p $HOME/.ohpm 89 echo '{"devDependencies":{"@ohos/hypium":"1.0.6"}}' > $HOME/.ohpm/oh-package.json5 90 pushd $HOME/.ohpm > /dev/null 91 echo "[OHOS INFO] installing hypium..." 92 ohpm install > /dev/null 93 popd > /dev/null 94} 95 96init_ohpm 97echo "======init_ohpm finished!======"