code: def install_npm(
      path=None,
      build_dir=None,
      source_dir=None,
      build_cmd="build",
      force=False,
      npm=None,
  ):
      """Return a Command for managing an npm installation.
  
      Note: The command is skipped if the `--skip-npm` flag is used.
  
      Parameters
      ----------
      path: str, optional
          The base path of the node package.  Defaults to the repo root.
      build_dir: str, optional
          The target build directory.  If this and source_dir are given,
          the JavaScript will only be build if necessary.
      source_dir: str, optional
          The source code directory.
      build_cmd: str, optional
          The npm command to build assets to the build_dir.
      npm: str or list, optional.
          The npm executable name, or a tuple of ['node', executable].
      """
  
      class NPM(BaseCommand):
          description = "install package.json dependencies using npm"
  
          def run(self):
              if skip_npm:
                  log.info("Skipping npm-installation")
                  return
              node_package = path or HERE
              node_modules = pjoin(node_package, "node_modules")
              is_yarn = os.path.exists(pjoin(node_package, "yarn.lock"))
  
              npm_cmd = npm
  
              if npm is None:
                  if is_yarn:
                      npm_cmd = ["yarn"]
                  else:
                      npm_cmd = ["npm"]
  
              if not which(npm_cmd[0]):
                  log.error(
                      "`{0}` unavailable.  If you're running this command "
                      "using sudo, make sure `{0}` is availble to sudo".format(
                          npm_cmd[0]
                      )
                  )
                  return
  
              if force or is_stale(
                  node_modules, pjoin(node_package, "package.json")
              ):
                  log.info(
                      "Installing build dependencies with npm.  This may "
                      "take a while..."
                  )
                  run(npm_cmd + ["install"], cwd=node_package)
              else:
                  log.info(
                      "node_modules are up to date, skipping npm install!"
                  )
              if build_dir and source_dir and not force:
                  should_build = is_stale(build_dir, source_dir)
              else:
                  should_build = True
              if should_build:
                  run(npm_cmd + ["run", build_cmd], cwd=node_package)
              else:
                  log.info(
                      "build is up to date, skipping npm build command!"
                  )
  
      return NPM
  
imports: from collections import defaultdict
  from os.path import join
  import io
  import os
  import functools
  import pipes
  import re
  import shlex
  import subprocess
  import sys
  from setuptools import Command
  from setuptools.command.build_py import build_py
  from setuptools.command.sdist import sdist
  from distutils import log
  from setuptools.command.develop import develop
  from setuptools.command.bdist_egg import bdist_egg
label_desc: Use image node_id 13 for calling a global function with example usage: install_npm(path, build_dir, source_dir, build_cmd, force, npm) and returns: NPM