1# Copyright 2023 The Pigweed Authors 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); you may not 4# use this file except in compliance with the License. You may obtain a copy of 5# the License at 6# 7# https://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, WITHOUT 11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12# License for the specific language governing permissions and limitations under 13# the License. 14"""NPM pw_env_setup action plugin. 15 16This action triggers `npm install` after CIPD setup. 17""" 18import os 19import shutil 20import subprocess 21 22 23def run_action(env=None): 24 """Project action to run 'npm install'.""" 25 if not env: 26 raise ValueError(f"Missing 'env', got %{env}") 27 28 with env(): 29 npm = shutil.which('npm.cmd' if os.name == 'nt' else 'npm') 30 31 repo_root = os.environ.get('PW_PROJECT_ROOT') or os.environ.get( 32 'PW_ROOT' 33 ) 34 35 # TODO: b/323378974 - Better integrate NPM actions with pw_env_setup so 36 # we don't have to manually set `npm_config_cache` every time we run 37 # npm. 38 # Force npm cache to live inside the environment directory. 39 npm_env = os.environ.copy() 40 npm_env['npm_config_cache'] = os.path.join( 41 npm_env['_PW_ACTUAL_ENVIRONMENT_ROOT'], 'npm-cache' 42 ) 43 44 subprocess.run( 45 [ 46 npm, 47 "install", 48 "--quiet", 49 "--no-progress", 50 "--loglevel=error", 51 ], 52 stdout=subprocess.PIPE, 53 stderr=subprocess.STDOUT, 54 stdin=subprocess.DEVNULL, 55 cwd=repo_root, 56 env=npm_env, 57 ) 58