1#!/bin/bash 2# Copyright (C) 2021 The Android Open Source Project 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16set -e -u 17 18# Note: this script is both executed standalone via tools/node and 19# sourced by tools/npm. 20 21ROOT_DIR="$(dirname $(cd -P ${BASH_SOURCE[0]%/*}; pwd))" 22 23if [ "$(uname -s)" == "Darwin" ]; then 24readonly NODE_DIR="$ROOT_DIR/buildtools/mac/nodejs" 25else 26readonly NODE_DIR="$ROOT_DIR/buildtools/linux64/nodejs" 27fi 28 29if [ ! -e "$NODE_DIR" ]; then 30 echo 'Could not find Node.js hermetic installation. Please run:' 31 echo 'tools/install-build-deps --ui' 32 exit 1 33fi 34 35exec_node() { 36 # Rationale for adding $ROOT_DIR/tools: some packages (notably protobufjs) 37 # have the bad habit of executing "npm install ..." at runtime on the first 38 # run. That will pick whatever "npm" is in the PATH which might be strongly 39 # different than ours in /buildtools. 40 export PATH="$NODE_DIR/bin:$ROOT_DIR/tools:$PATH" 41 exec node "$@" 42} 43 44# If the script is sourced (from //tools/npm) stop here. Otherwise run node. 45if [ "$0" == "${BASH_SOURCE[0]}" ]; then 46 exec_node "$@" 47fi 48